Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Controller for user Accounts |
| 3 | * |
| 4 | * @module app/users |
| 5 | * @exports userAccountsController |
| 6 | * @name userAccountsController |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 7 | */ |
| 8 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 9 | window.angular && (function(angular) { |
| 10 | 'use strict'; |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 11 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 12 | angular.module('app.users').controller('userAccountsController', [ |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame^] | 13 | '$scope', 'APIUtils', |
| 14 | function($scope, APIUtils) { |
| 15 | // TODO: Get the roles using Roles redfish URI. |
| 16 | $scope.roles = ['Administrator', 'Operator', 'User', 'Callback']; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 17 | $scope.state = 'none'; |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame^] | 18 | $scope.outMsg = ''; |
| 19 | $scope.loading = true; |
Gunnar Mills | 08744f8 | 2018-03-19 16:24:41 -0500 | [diff] [blame] | 20 | |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame^] | 21 | function loadUserInfo() { |
| 22 | $scope.users = []; |
| 23 | $scope.loading = true; |
| 24 | $scope.isUserSelected = false; |
| 25 | $scope.selectedUser = null; |
Gunnar Mills | 6b97dde | 2018-03-14 17:04:58 -0500 | [diff] [blame] | 26 | |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame^] | 27 | APIUtils.getAllUserAccounts() |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 28 | .then( |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame^] | 29 | function(res) { |
| 30 | $scope.users = res; |
| 31 | }, |
| 32 | function(error) { |
| 33 | console.log(JSON.stringify(error)); |
| 34 | }) |
| 35 | .finally(function() { |
| 36 | $scope.loading = false; |
| 37 | }); |
| 38 | }; |
| 39 | $scope.cancel = function() { |
| 40 | $scope.state = 'none'; |
| 41 | $scope.outMsg = ''; |
| 42 | loadUserInfo(); |
| 43 | }; |
| 44 | $scope.setSelectedUser = function(user) { |
| 45 | $scope.state = 'none'; |
| 46 | $scope.outMsg = ''; |
| 47 | |
| 48 | $scope.isUserSelected = true; |
| 49 | $scope.selectedUser = angular.copy(user); |
| 50 | $scope.selectedUser.VerifyPassword = null; |
| 51 | // Used while renaming the user. |
| 52 | $scope.selectedUser.CurrentUserName = $scope.selectedUser.UserName; |
| 53 | }; |
| 54 | $scope.createNewUser = function() { |
| 55 | $scope.state = 'none'; |
| 56 | $scope.outMsg = ''; |
| 57 | |
| 58 | if (!$scope.selectedUser.UserName || !$scope.selectedUser.Password) { |
| 59 | $scope.state = 'error'; |
| 60 | $scope.outMsg = 'Username or Password can\'t be empty'; |
| 61 | return; |
| 62 | } |
| 63 | if ($scope.selectedUser.Password !== |
| 64 | $scope.selectedUser.VerifyPassword) { |
| 65 | $scope.state = 'error'; |
| 66 | $scope.outMsg = 'Passwords do not match'; |
| 67 | return; |
| 68 | } |
| 69 | var user = $scope.selectedUser.UserName; |
| 70 | var passwd = $scope.selectedUser.Password; |
| 71 | var role = $scope.selectedUser.RoleId; |
| 72 | var enabled = false; |
| 73 | if ($scope.selectedUser.Enabled != null) { |
| 74 | enabled = $scope.selectedUser.Enabled; |
| 75 | } |
| 76 | |
| 77 | $scope.loading = true; |
| 78 | APIUtils.createUser(user, passwd, role, enabled) |
| 79 | .then( |
| 80 | function(response) { |
| 81 | $scope.state = 'success'; |
| 82 | $scope.outMsg = 'User has been created successfully'; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 83 | }, |
| 84 | function(error) { |
| 85 | $scope.state = 'error'; |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame^] | 86 | if ((error.data.error['@Message.ExtendedInfo'] != |
| 87 | undefined) && |
| 88 | (error.data.error['@Message.ExtendedInfo'].length != 0)) { |
| 89 | $scope.outMsg = |
| 90 | error.data.error['@Message.ExtendedInfo'][0].Message; |
| 91 | } else { |
| 92 | $scope.outMsg = 'Failed to create new user.'; |
| 93 | } |
Gunnar Mills | b3d48d4 | 2018-05-25 08:34:35 -0500 | [diff] [blame] | 94 | }) |
| 95 | .finally(function() { |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame^] | 96 | loadUserInfo(); |
| 97 | $scope.loading = false; |
Gunnar Mills | b3d48d4 | 2018-05-25 08:34:35 -0500 | [diff] [blame] | 98 | }); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 99 | }; |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame^] | 100 | $scope.updateUserInfo = function() { |
| 101 | $scope.state = 'none'; |
| 102 | $scope.outMsg = ''; |
| 103 | if ($scope.selectedUser.Password !== |
| 104 | $scope.selectedUser.VerifyPassword) { |
| 105 | $scope.state = 'error'; |
| 106 | $scope.outMsg = 'Passwords do not match'; |
| 107 | return; |
| 108 | } |
| 109 | var data = {}; |
| 110 | if ($scope.selectedUser.UserName !== |
| 111 | $scope.selectedUser.CurrentUserName) { |
| 112 | data['UserName'] = $scope.selectedUser.UserName; |
| 113 | } |
| 114 | $scope.selectedUser.VerifyPassword = null; |
| 115 | if ($scope.selectedUser.Password != null) { |
| 116 | data['Password'] = $scope.selectedUser.Password; |
| 117 | } |
| 118 | data['RoleId'] = $scope.selectedUser.RoleId; |
| 119 | data['Enabled'] = $scope.selectedUser.Enabled; |
| 120 | |
| 121 | $scope.loading = true; |
| 122 | APIUtils |
| 123 | .updateUser( |
| 124 | $scope.selectedUser.CurrentUserName, data['UserName'], |
| 125 | data['Password'], data['RoleId'], data['Enabled']) |
| 126 | .then( |
| 127 | function(response) { |
| 128 | $scope.state = 'success'; |
| 129 | $scope.outMsg = 'User has been updated successfully'; |
| 130 | }, |
| 131 | function(error) { |
| 132 | $scope.state = 'error'; |
| 133 | if ((error.data.error['@Message.ExtendedInfo'] != |
| 134 | undefined) && |
| 135 | (error.data.error['@Message.ExtendedInfo'].length != 0)) { |
| 136 | $scope.outMsg = |
| 137 | error.data.error['@Message.ExtendedInfo'][0].Message; |
| 138 | } else { |
| 139 | $scope.outMsg = 'Updating user failed.'; |
| 140 | } |
| 141 | }) |
| 142 | .finally(function() { |
| 143 | loadUserInfo(); |
| 144 | $scope.loading = false; |
| 145 | }); |
| 146 | }; |
| 147 | $scope.deleteUser = function(userName) { |
| 148 | $scope.state = 'none'; |
| 149 | $scope.outMsg = ''; |
| 150 | |
| 151 | $scope.loading = true; |
| 152 | APIUtils.deleteUser(userName) |
| 153 | .then( |
| 154 | function(response) { |
| 155 | $scope.state = 'success'; |
| 156 | $scope.outMsg = 'User has been deleted successfully'; |
| 157 | }, |
| 158 | function(error) { |
| 159 | $scope.state = 'error'; |
| 160 | if ((error.data.error['@Message.ExtendedInfo'] != |
| 161 | undefined) && |
| 162 | (error.data.error['@Message.ExtendedInfo'].length != 0)) { |
| 163 | $scope.outMsg = |
| 164 | error.data.error['@Message.ExtendedInfo'][0].Message; |
| 165 | } else { |
| 166 | $scope.outMsg = 'Deleting user failed.'; |
| 167 | } |
| 168 | }) |
| 169 | .finally(function() { |
| 170 | loadUserInfo(); |
| 171 | $scope.loading = false; |
| 172 | }); |
| 173 | }; |
| 174 | loadUserInfo(); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 175 | } |
| 176 | ]); |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 177 | })(angular); |