Add UM API to modify local user 71/2371/5
authorAlessandro Boch <aboch@cisco.com>
Tue, 5 Nov 2013 00:10:12 +0000 (16:10 -0800)
committerGerrit Code Review <gerrit@opendaylight.org>
Wed, 6 Nov 2013 15:25:46 +0000 (15:25 +0000)
Change-Id: I6494b30a6375294f63b51762f55ad2398aa3ac29
Signed-off-by: Alessandro Boch <aboch@cisco.com>
opendaylight/usermanager/api/src/main/java/org/opendaylight/controller/usermanager/IUserManager.java
opendaylight/usermanager/implementation/src/main/java/org/opendaylight/controller/usermanager/internal/UserManager.java

index 141f50c..aaf5cbd 100644 (file)
@@ -78,6 +78,17 @@ public interface IUserManager extends UserDetailsService {
      */
     public Status addLocalUser(UserConfig configObject);
 
+    /**
+     * Modify a local user
+     *
+     * @param configObject
+     *            {@link org.opendaylight.controller.usermanager.internal.UserConfig}
+     *            The new User Configuration
+     * @return refer to {@link org.opendaylight.controller.sal.utils.Status}
+     *         status code
+     */
+    public Status modifyLocalUser(UserConfig configObject);
+
     /**
      * Remove a local user
      *
index 8d9e347..670124a 100644 (file)
@@ -93,6 +93,25 @@ public class UserManager implements IUserManager, IObjectReader,
     private IContainerAuthorization containerAuthorizationClient;
     private Set<IResourceAuthorization> applicationAuthorizationClients;
     private ISessionManager sessionMgr = new SessionManager();
+    protected enum Command {
+        ADD("add", "added"),
+        MODIFY("modify", "modified"),
+        REMOVE("remove", "removed");
+        private String action;
+        private String postAction;
+        private Command(String action, String postAction) {
+            this.action = action;
+            this.postAction = postAction;
+        }
+
+        public String getAction() {
+            return action;
+        }
+
+        public String getPostAction() {
+            return postAction;
+        }
+    }
 
     public boolean addAAAProvider(IAAAProvider provider) {
         if (provider == null || provider.getName() == null
@@ -480,7 +499,7 @@ public class UserManager implements IUserManager, IObjectReader,
     /*
      * Interaction with GUI START
      */
-    private Status addRemoveLocalUser(UserConfig AAAconf, boolean delete) {
+    private Status changeLocalUser(UserConfig AAAconf, Command command) {
         // UserConfig Validation check
         Status validCheck = AAAconf.validate();
         if (!validCheck.isSuccess()) {
@@ -491,28 +510,51 @@ public class UserManager implements IUserManager, IObjectReader,
 
         // Check default admin user
         if (user.equals(UserManager.DEFAULT_ADMIN)) {
-            String msg = "Invalid Request: Default Network Admin  User cannot be " + ((delete)? "removed" : "added");
+            String msg = String.format("Invalid Request: Default Network Admin  User cannot be %s", command.getPostAction());
             logger.debug(msg);
             return new Status(StatusCode.NOTALLOWED, msg);
         }
 
         // Check user presence/conflict
+        UserConfig currentAAAconf = localUserConfigList.get(user);
         StatusCode statusCode = null;
         String reason = null;
-        if (delete && !localUserConfigList.containsKey(user)) {
-            reason = "not found";
-            statusCode = StatusCode.NOTFOUND;
-        } else if (!delete && localUserConfigList.containsKey(user)) {
-            reason = "already present";
-            statusCode = StatusCode.CONFLICT;
+        switch (command) {
+        case ADD:
+            if (currentAAAconf != null) {
+                reason = "already present";
+                statusCode = StatusCode.CONFLICT;
+            }
+            break;
+        case MODIFY:
+        case REMOVE:
+            if (currentAAAconf == null) {
+                reason = "not found";
+                statusCode = StatusCode.NOTFOUND;
+            }
+            break;
+        default:
+            break;
+
         }
         if (statusCode != null) {
+            String action = String.format("Failed to %s user %s: ", command.getAction(), user);
             String msg = String.format("User %s %s in configuration database", user, reason);
-            logger.debug(msg);
+            logger.debug(action + msg);
             return new Status(statusCode, msg);
         }
 
-        return addRemoveLocalUserInternal(AAAconf, delete);
+        switch (command) {
+        case ADD:
+            return addRemoveLocalUserInternal(AAAconf, false);
+        case MODIFY:
+            addRemoveLocalUserInternal(currentAAAconf, true);
+            return addRemoveLocalUserInternal(AAAconf, false);
+        case REMOVE:
+            return addRemoveLocalUserInternal(AAAconf, true);
+        default:
+            return new Status(StatusCode.INTERNALERROR, "Unknown action");
+        }
     }
 
     private Status addRemoveLocalUserInternal(UserConfig AAAconf, boolean delete) {
@@ -571,12 +613,17 @@ public class UserManager implements IUserManager, IObjectReader,
 
     @Override
     public Status addLocalUser(UserConfig AAAconf) {
-        return addRemoveLocalUser(AAAconf, false);
+        return changeLocalUser(AAAconf, Command.ADD);
+    }
+
+    @Override
+    public Status modifyLocalUser(UserConfig AAAconf) {
+        return changeLocalUser(AAAconf, Command.MODIFY);
     }
 
     @Override
     public Status removeLocalUser(UserConfig AAAconf) {
-        return addRemoveLocalUser(AAAconf, true);
+        return changeLocalUser(AAAconf, Command.REMOVE);
     }
 
     @Override
@@ -589,7 +636,7 @@ public class UserManager implements IUserManager, IObjectReader,
             return new Status(StatusCode.NOTFOUND, "User does not exist");
         }
 
-        return addRemoveLocalUser(localUserConfigList.get(userName), true);
+        return changeLocalUser(localUserConfigList.get(userName), Command.REMOVE);
     }
 
     @Override