syncope-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cohei...@apache.org
Subject svn commit: r1309368 - in /incubator/syncope/trunk/core/src: main/java/org/syncope/core/rest/controller/UserController.java test/java/org/syncope/core/rest/UserTestITCase.java
Date Wed, 04 Apr 2012 13:24:40 GMT
Author: coheigea
Date: Wed Apr  4 13:24:39 2012
New Revision: 1309368

URL: http://svn.apache.org/viewvc?rev=1309368&view=rev
Log:
[SYNCOPE-42] - Add the ability to delete a user by username via the REST API

Modified:
    incubator/syncope/trunk/core/src/main/java/org/syncope/core/rest/controller/UserController.java
    incubator/syncope/trunk/core/src/test/java/org/syncope/core/rest/UserTestITCase.java

Modified: incubator/syncope/trunk/core/src/main/java/org/syncope/core/rest/controller/UserController.java
URL: http://svn.apache.org/viewvc/incubator/syncope/trunk/core/src/main/java/org/syncope/core/rest/controller/UserController.java?rev=1309368&r1=1309367&r2=1309368&view=diff
==============================================================================
--- incubator/syncope/trunk/core/src/main/java/org/syncope/core/rest/controller/UserController.java
(original)
+++ incubator/syncope/trunk/core/src/main/java/org/syncope/core/rest/controller/UserController.java
Wed Apr  4 13:24:39 2012
@@ -434,51 +434,21 @@ public class UserController {
     @RequestMapping(method = RequestMethod.GET, value = "/delete/{userId}")
     public UserTO delete(@PathVariable("userId") final Long userId)
             throws NotFoundException, WorkflowException, PropagationException, UnauthorizedRoleException
{
-
         LOG.debug("User delete called with {}", userId);
-
-        // Note here that we can only notify about "delete", not any other
-        // task defined in workflow process definition: this because this
-        // information could only be available after wfAdapter.delete(), which
-        // will also effectively remove user from db, thus making virtually
-        // impossible by NotificationManager to fetch required user information
-        notificationManager.createTasks(new WorkflowResult<Long>(userId, null, "delete"));
-
-        List<PropagationTask> tasks = propagationManager.getDeleteTaskIds(userId);
-
-        final UserTO userTO = new UserTO();
-        userTO.setId(userId);
-
-        propagationManager.execute(tasks, new PropagationHandler() {
-
-            @Override
-            public void handle(final String resourceName, final PropagationTaskExecStatus
executionStatus,
-                    final ConnectorObject before, final ConnectorObject after) {
-
-                final PropagationTO propagation = new PropagationTO();
-                propagation.setResourceName(resourceName);
-                propagation.setStatus(executionStatus);
-
-                if (before != null) {
-                    propagation.setBefore(connObjectUtil.getConnObjectTO(before));
-                }
-
-                if (after != null) {
-                    propagation.setAfter(connObjectUtil.getConnObjectTO(after));
-                }
-
-                userTO.addPropagationTO(propagation);
-            }
-        });
-
-        wfAdapter.delete(userId);
-
-        auditManager.audit(Category.user, UserSubCategory.delete, Result.success,
-                "Successfully deleted user: " + userTO.getUsername());
-
-        LOG.debug("User successfully deleted: {}", userId);
-
-        return userTO;
+        
+        return deleteByUserId(userId);
+    }
+    
+    @PreAuthorize("hasRole('USER_DELETE')")
+    @RequestMapping(method = RequestMethod.GET, value = "/delete")
+    public UserTO delete(@RequestParam("username") final String username)
+            throws NotFoundException, WorkflowException, PropagationException, UnauthorizedRoleException
{
+        LOG.debug("User delete called with {}", username);
+        
+        UserTO result = userDataBinder.getUserTO(username);
+        long userId = result.getId();
+        
+        return deleteByUserId(userId);
     }
 
     @PreAuthorize("hasRole('USER_UPDATE')")
@@ -624,4 +594,50 @@ public class UserController {
 
         return savedTO;
     }
+    
+    private UserTO deleteByUserId(final Long userId)
+        throws NotFoundException, WorkflowException, PropagationException, UnauthorizedRoleException
{
+        // Note here that we can only notify about "delete", not any other
+        // task defined in workflow process definition: this because this
+        // information could only be available after wfAdapter.delete(), which
+        // will also effectively remove user from db, thus making virtually
+        // impossible by NotificationManager to fetch required user information
+        notificationManager.createTasks(new WorkflowResult<Long>(userId, null, "delete"));
+
+        List<PropagationTask> tasks = propagationManager.getDeleteTaskIds(userId);
+
+        final UserTO userTO = new UserTO();
+        userTO.setId(userId);
+
+        propagationManager.execute(tasks, new PropagationHandler() {
+
+            @Override
+            public void handle(final String resourceName, final PropagationTaskExecStatus
executionStatus,
+                               final ConnectorObject before, final ConnectorObject after)
{
+
+                final PropagationTO propagation = new PropagationTO();
+                propagation.setResourceName(resourceName);
+                propagation.setStatus(executionStatus);
+
+                if (before != null) {
+                    propagation.setBefore(connObjectUtil.getConnObjectTO(before));
+                }
+
+                if (after != null) {
+                    propagation.setAfter(connObjectUtil.getConnObjectTO(after));
+                }
+
+                userTO.addPropagationTO(propagation);
+            }
+        });
+
+        wfAdapter.delete(userId);
+
+        auditManager.audit(Category.user, UserSubCategory.delete, Result.success,
+                           "Successfully deleted user: " + userTO.getUsername());
+
+        LOG.debug("User successfully deleted: {}", userId);
+
+        return userTO;
+    }
 }

Modified: incubator/syncope/trunk/core/src/test/java/org/syncope/core/rest/UserTestITCase.java
URL: http://svn.apache.org/viewvc/incubator/syncope/trunk/core/src/test/java/org/syncope/core/rest/UserTestITCase.java?rev=1309368&r1=1309367&r2=1309368&view=diff
==============================================================================
--- incubator/syncope/trunk/core/src/test/java/org/syncope/core/rest/UserTestITCase.java (original)
+++ incubator/syncope/trunk/core/src/test/java/org/syncope/core/rest/UserTestITCase.java Wed
Apr  4 13:24:39 2012
@@ -767,6 +767,34 @@ public class UserTestITCase extends Abst
             assertEquals(HttpStatus.NOT_FOUND, e.getStatusCode());
         }
     }
+    
+    @Test
+    public void deleteByUsername() {
+        UserTO userTO = getSampleTO("delete.by.username@apache.org");
+
+        // specify a propagation
+        userTO.addResource("resource-testdb");
+
+        userTO = restTemplate.postForObject(BASE_URL + "user/create", userTO, UserTO.class);
+
+        long id = userTO.getId();
+        userTO = 
+            restTemplate.getForObject(BASE_URL + "user/delete?username=" + userTO.getUsername(),
UserTO.class);
+
+        assertNotNull(userTO);
+        assertEquals(id, userTO.getId());
+        assertTrue(userTO.getAttributes().isEmpty());
+
+        // check for propagation result
+        assertFalse(userTO.getPropagationTOs().isEmpty());
+        assertTrue(userTO.getPropagationTOs().get(0).getStatus().isSuccessful());
+
+        try {
+            restTemplate.getForObject(BASE_URL + "user/read/{userId}.json", UserTO.class,
userTO.getId());
+        } catch (HttpStatusCodeException e) {
+            assertEquals(HttpStatus.NOT_FOUND, e.getStatusCode());
+        }
+    }
 
     @Test
     public void count() {



Mime
View raw message