WebUI: Dynamically get accountService roles

Get the AccountService roles dynamically
by calling redfish/rest API's and use them
in user management webui.

Test:
Loaded web page and checked user management
page for appropriate roles fetched from dbus.

Change-Id: I4f51cb0a622a1be8b0bfec2b2fe3ecdee2538c6a
Signed-off-by: AppaRao Puli <apparao.puli@linux.intel.com>
diff --git a/app/common/services/api-utils.js b/app/common/services/api-utils.js
index beb63ba..908dd27 100644
--- a/app/common/services/api-utils.js
+++ b/app/common/services/api-utils.js
@@ -526,7 +526,62 @@
             return deferred.promise;
           }
         },
-        getAllUserAccounts: function(members) {
+        getAccountServiceRoles: function() {
+          var roles = [];
+
+          if (DataService.configJson.redfishSupportEnabled == true) {
+            return $http({
+                     method: 'GET',
+                     url: DataService.getHost() +
+                         '/redfish/v1/AccountService/Roles',
+                     withCredentials: true
+                   })
+                .then(
+                    function(response) {
+                      var members = response.data['Members'];
+                      angular.forEach(members, function(member) {
+                        roles.push(member['@odata.id'].split('/').pop());
+                      });
+                      return roles;
+                    },
+                    function(error) {
+                      console.log(error);
+                    });
+          } else {
+            return $http({
+                     method: 'GET',
+                     url: DataService.getHost() + '/xyz/openbmc_project/user',
+                     withCredentials: true
+                   })
+                .then(
+                    function(response) {
+                      var json = JSON.stringify(response.data);
+                      var content = JSON.parse(json);
+                      var privList = content.data['AllPrivileges'];
+
+                      function convertPrivToRoleId(priv) {
+                        if (priv == 'priv-admin') {
+                          return 'Administrator';
+                        } else if (priv == 'priv-user') {
+                          return 'User';
+                        } else if (priv == 'priv-operator') {
+                          return 'Operator';
+                        } else if (priv == 'priv-callback') {
+                          return 'Callback';
+                        }
+                        return '';
+                      }
+                      for (var i = 0; i < privList.length; i++) {
+                        roles.push(convertPrivToRoleId(privList[i]));
+                      }
+                      return roles;
+                    },
+                    function(error) {
+                      console.log(error);
+                    });
+          }
+        },
+        getAllUserAccounts: function() {
           var deferred = $q.defer();
           var promises = [];
           var users = [];
diff --git a/app/users/controllers/user-accounts-controller.js b/app/users/controllers/user-accounts-controller.js
index 5f90844..4148f54 100644
--- a/app/users/controllers/user-accounts-controller.js
+++ b/app/users/controllers/user-accounts-controller.js
@@ -10,32 +10,38 @@
   'use strict';
 
   angular.module('app.users').controller('userAccountsController', [
-    '$scope', 'APIUtils',
-    function($scope, APIUtils) {
-      // TODO: Get the roles using Roles redfish URI.
-      $scope.roles = ['Administrator', 'Operator', 'User', 'Callback'];
+    '$scope', '$q', 'APIUtils',
+    function($scope, $q, APIUtils) {
+      $scope.users = [];
+      $scope.roles = [];
       $scope.state = 'none';
       $scope.outMsg = '';
       $scope.loading = true;
 
       function loadUserInfo() {
-        $scope.users = [];
         $scope.loading = true;
         $scope.isUserSelected = false;
         $scope.selectedUser = null;
-
-        APIUtils.getAllUserAccounts()
-            .then(
+        $q.all([
+            APIUtils.getAllUserAccounts().then(
                 function(res) {
                   $scope.users = res;
                 },
                 function(error) {
                   console.log(JSON.stringify(error));
+                }),
+            APIUtils.getAccountServiceRoles().then(
+                function(res) {
+                  $scope.roles = res;
+                },
+                function(error) {
+                  console.log(JSON.stringify(error));
                 })
-            .finally(function() {
-              $scope.loading = false;
-            });
+          ]).finally(function() {
+          $scope.loading = false;
+        });
       };
+
       $scope.cancel = function() {
         $scope.state = 'none';
         $scope.outMsg = '';
@@ -171,6 +177,7 @@
               $scope.loading = false;
             });
       };
+
       loadUserInfo();
     }
   ]);