WebUI: User management full implementation.

Added webui user accounts management. This support
both redfish and rest based backend API calls
depending on redfishSupportEnabled flag.
It does following actions:
 - View all user list and there properties like
   name, privilege, enabled state, Locked etc..
 - Create new user account.
 - Delete existing user account.
 - Update the existing user properties like
   password, privilege, enabled state.

Unit Test:
 - Viewed all user information is proper or not.
 - Created new user and validated.
 - Deleted specific user and checked.
 - Modified user info and validated the change.
All tests are done by enabling and disabling
redfishSupportEnabled flag using conifg.json.

Change-Id: Ifecf63844dc42c44771509958bf75947a92997ac
Signed-off-by: AppaRao Puli <apparao.puli@linux.intel.com>
diff --git a/app/users/controllers/user-accounts-controller.js b/app/users/controllers/user-accounts-controller.js
index 24a93cb..5f90844 100644
--- a/app/users/controllers/user-accounts-controller.js
+++ b/app/users/controllers/user-accounts-controller.js
@@ -10,58 +10,168 @@
   'use strict';
 
   angular.module('app.users').controller('userAccountsController', [
-    '$scope', '$window', 'APIUtils', 'dataService',
-    function($scope, $window, APIUtils, dataService) {
-      $scope.dataService = dataService;
+    '$scope', 'APIUtils',
+    function($scope, APIUtils) {
+      // TODO: Get the roles using Roles redfish URI.
+      $scope.roles = ['Administrator', 'Operator', 'User', 'Callback'];
       $scope.state = 'none';
-      $scope.errorMsg = '';
+      $scope.outMsg = '';
+      $scope.loading = true;
 
-      $scope.changePassword = function(
-          oldPassword, newPassword, confirmNewPassword) {
-        var user = $scope.dataService.getUser();
-        if (!oldPassword || !newPassword || !confirmNewPassword) {
-          $scope.state = 'error';
-          $scope.errorMsg = 'Field is required!';
-          return false;
-        }
-        if (newPassword !== confirmNewPassword) {
-          $scope.state = 'error';
-          $scope.errorMsg = 'New passwords do not match!';
-          return false;
-        }
-        if (newPassword === oldPassword) {
-          $scope.state = 'error';
-          $scope.errorMsg = 'New password and old password match!';
-          return false;
-        }
+      function loadUserInfo() {
+        $scope.users = [];
+        $scope.loading = true;
+        $scope.isUserSelected = false;
+        $scope.selectedUser = null;
 
-        // Verify the oldPassword is correct
-        dataService.ignoreHttpError = true;
-        APIUtils.testPassword(user, oldPassword)
+        APIUtils.getAllUserAccounts()
             .then(
-                function(state) {
-                  APIUtils.changePassword(user, newPassword)
-                      .then(
-                          function(response) {
-                            // Clear the textboxes on a success
-                            $scope.passwordVerify = '';
-                            $scope.password = '';
-                            $scope.oldPassword = '';
-                            $scope.state = 'success';
-                          },
-                          function(error) {
-                            $scope.state = 'error';
-                            $scope.errorMsg = 'Error changing password!';
-                          });
+                function(res) {
+                  $scope.users = res;
+                },
+                function(error) {
+                  console.log(JSON.stringify(error));
+                })
+            .finally(function() {
+              $scope.loading = false;
+            });
+      };
+      $scope.cancel = function() {
+        $scope.state = 'none';
+        $scope.outMsg = '';
+        loadUserInfo();
+      };
+      $scope.setSelectedUser = function(user) {
+        $scope.state = 'none';
+        $scope.outMsg = '';
+
+        $scope.isUserSelected = true;
+        $scope.selectedUser = angular.copy(user);
+        $scope.selectedUser.VerifyPassword = null;
+        // Used while renaming the user.
+        $scope.selectedUser.CurrentUserName = $scope.selectedUser.UserName;
+      };
+      $scope.createNewUser = function() {
+        $scope.state = 'none';
+        $scope.outMsg = '';
+
+        if (!$scope.selectedUser.UserName || !$scope.selectedUser.Password) {
+          $scope.state = 'error';
+          $scope.outMsg = 'Username or Password can\'t be empty';
+          return;
+        }
+        if ($scope.selectedUser.Password !==
+            $scope.selectedUser.VerifyPassword) {
+          $scope.state = 'error';
+          $scope.outMsg = 'Passwords do not match';
+          return;
+        }
+        var user = $scope.selectedUser.UserName;
+        var passwd = $scope.selectedUser.Password;
+        var role = $scope.selectedUser.RoleId;
+        var enabled = false;
+        if ($scope.selectedUser.Enabled != null) {
+          enabled = $scope.selectedUser.Enabled;
+        }
+
+        $scope.loading = true;
+        APIUtils.createUser(user, passwd, role, enabled)
+            .then(
+                function(response) {
+                  $scope.state = 'success';
+                  $scope.outMsg = 'User has been created successfully';
                 },
                 function(error) {
                   $scope.state = 'error';
-                  $scope.errorMsg = 'Old password is not correct!';
+                  if ((error.data.error['@Message.ExtendedInfo'] !=
+                       undefined) &&
+                      (error.data.error['@Message.ExtendedInfo'].length != 0)) {
+                    $scope.outMsg =
+                        error.data.error['@Message.ExtendedInfo'][0].Message;
+                  } else {
+                    $scope.outMsg = 'Failed to create new user.';
+                  }
                 })
             .finally(function() {
-              dataService.ignoreHttpError = false;
+              loadUserInfo();
+              $scope.loading = false;
             });
       };
+      $scope.updateUserInfo = function() {
+        $scope.state = 'none';
+        $scope.outMsg = '';
+        if ($scope.selectedUser.Password !==
+            $scope.selectedUser.VerifyPassword) {
+          $scope.state = 'error';
+          $scope.outMsg = 'Passwords do not match';
+          return;
+        }
+        var data = {};
+        if ($scope.selectedUser.UserName !==
+            $scope.selectedUser.CurrentUserName) {
+          data['UserName'] = $scope.selectedUser.UserName;
+        }
+        $scope.selectedUser.VerifyPassword = null;
+        if ($scope.selectedUser.Password != null) {
+          data['Password'] = $scope.selectedUser.Password;
+        }
+        data['RoleId'] = $scope.selectedUser.RoleId;
+        data['Enabled'] = $scope.selectedUser.Enabled;
+
+        $scope.loading = true;
+        APIUtils
+            .updateUser(
+                $scope.selectedUser.CurrentUserName, data['UserName'],
+                data['Password'], data['RoleId'], data['Enabled'])
+            .then(
+                function(response) {
+                  $scope.state = 'success';
+                  $scope.outMsg = 'User has been updated successfully';
+                },
+                function(error) {
+                  $scope.state = 'error';
+                  if ((error.data.error['@Message.ExtendedInfo'] !=
+                       undefined) &&
+                      (error.data.error['@Message.ExtendedInfo'].length != 0)) {
+                    $scope.outMsg =
+                        error.data.error['@Message.ExtendedInfo'][0].Message;
+                  } else {
+                    $scope.outMsg = 'Updating user failed.';
+                  }
+                })
+            .finally(function() {
+              loadUserInfo();
+              $scope.loading = false;
+            });
+      };
+      $scope.deleteUser = function(userName) {
+        $scope.state = 'none';
+        $scope.outMsg = '';
+
+        $scope.loading = true;
+        APIUtils.deleteUser(userName)
+            .then(
+                function(response) {
+                  $scope.state = 'success';
+                  $scope.outMsg = 'User has been deleted successfully';
+                },
+                function(error) {
+                  $scope.state = 'error';
+                  if ((error.data.error['@Message.ExtendedInfo'] !=
+                       undefined) &&
+                      (error.data.error['@Message.ExtendedInfo'].length != 0)) {
+                    $scope.outMsg =
+                        error.data.error['@Message.ExtendedInfo'][0].Message;
+                  } else {
+                    $scope.outMsg = 'Deleting user failed.';
+                  }
+                })
+            .finally(function() {
+              loadUserInfo();
+              $scope.loading = false;
+            });
+      };
+      loadUserInfo();
     }
   ]);
 })(angular);