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 | cf7219c | 2018-12-27 16:17:46 +0530 | [diff] [blame] | 13 | '$scope', '$q', 'APIUtils', |
| 14 | function($scope, $q, APIUtils) { |
| 15 | $scope.users = []; |
| 16 | $scope.roles = []; |
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; |
AppaRao Puli | b1e7c86 | 2019-03-12 14:56:40 +0530 | [diff] [blame^] | 20 | $scope.properties = {}; |
| 21 | $scope.origProp = {}; |
Gunnar Mills | 08744f8 | 2018-03-19 16:24:41 -0500 | [diff] [blame] | 22 | |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 23 | function loadUserInfo() { |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 24 | $scope.loading = true; |
| 25 | $scope.isUserSelected = false; |
| 26 | $scope.selectedUser = null; |
AppaRao Puli | a83cd05 | 2019-01-07 23:25:43 +0530 | [diff] [blame] | 27 | $scope.togglePassword = false; |
| 28 | $scope.toggleVerify = false; |
| 29 | |
AppaRao Puli | cf7219c | 2018-12-27 16:17:46 +0530 | [diff] [blame] | 30 | $q.all([ |
| 31 | APIUtils.getAllUserAccounts().then( |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 32 | function(res) { |
| 33 | $scope.users = res; |
| 34 | }, |
| 35 | function(error) { |
| 36 | console.log(JSON.stringify(error)); |
AppaRao Puli | cf7219c | 2018-12-27 16:17:46 +0530 | [diff] [blame] | 37 | }), |
AppaRao Puli | b1e7c86 | 2019-03-12 14:56:40 +0530 | [diff] [blame^] | 38 | |
| 39 | APIUtils.getAllUserAccountProperties().then( |
| 40 | function(res) { |
| 41 | $scope.properties = res; |
| 42 | $scope.origProp = angular.copy($scope.properties); |
| 43 | }, |
| 44 | function(error) { |
| 45 | console.log(JSON.stringify(error)); |
| 46 | }), |
| 47 | |
AppaRao Puli | cf7219c | 2018-12-27 16:17:46 +0530 | [diff] [blame] | 48 | APIUtils.getAccountServiceRoles().then( |
| 49 | function(res) { |
| 50 | $scope.roles = res; |
| 51 | }, |
| 52 | function(error) { |
| 53 | console.log(JSON.stringify(error)); |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 54 | }) |
AppaRao Puli | cf7219c | 2018-12-27 16:17:46 +0530 | [diff] [blame] | 55 | ]).finally(function() { |
| 56 | $scope.loading = false; |
| 57 | }); |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 58 | }; |
AppaRao Puli | cf7219c | 2018-12-27 16:17:46 +0530 | [diff] [blame] | 59 | |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 60 | $scope.cancel = function() { |
| 61 | $scope.state = 'none'; |
| 62 | $scope.outMsg = ''; |
| 63 | loadUserInfo(); |
| 64 | }; |
AppaRao Puli | b1e7c86 | 2019-03-12 14:56:40 +0530 | [diff] [blame^] | 65 | |
| 66 | $scope.saveAllValues = function() { |
| 67 | $scope.state = 'none'; |
| 68 | $scope.outMsg = ''; |
| 69 | $scope.loading = true; |
| 70 | var data = {}; |
| 71 | if ($scope.properties.AccountLockoutDuration != |
| 72 | $scope.origProp.AccountLockoutDuration) { |
| 73 | data['AccountLockoutDuration'] = |
| 74 | $scope.properties.AccountLockoutDuration; |
| 75 | } |
| 76 | if ($scope.properties.AccountLockoutThreshold != |
| 77 | $scope.origProp.AccountLockoutThreshold) { |
| 78 | data['AccountLockoutThreshold'] = |
| 79 | $scope.properties.AccountLockoutThreshold; |
| 80 | } |
| 81 | |
| 82 | if ($scope.properties.AccountLockoutDuration == |
| 83 | $scope.origProp.AccountLockoutDuration && |
| 84 | $scope.properties.AccountLockoutThreshold == |
| 85 | $scope.origProp.AccountLockoutThreshold) { |
| 86 | // No change in properties, just return; |
| 87 | $scope.loading = false; |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | APIUtils |
| 92 | .saveUserAccountProperties( |
| 93 | data['AccountLockoutDuration'], data['AccountLockoutThreshold']) |
| 94 | .then( |
| 95 | function(response) { |
| 96 | $scope.state = 'success'; |
| 97 | $scope.outMsg = |
| 98 | 'User account properties has been updated successfully'; |
| 99 | }, |
| 100 | function(error) { |
| 101 | $scope.outMsg = 'Account Properties Updation failed.'; |
| 102 | }) |
| 103 | .finally(function() { |
| 104 | loadUserInfo(); |
| 105 | $scope.loading = false; |
| 106 | }); |
| 107 | }; |
| 108 | |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 109 | $scope.setSelectedUser = function(user) { |
| 110 | $scope.state = 'none'; |
| 111 | $scope.outMsg = ''; |
| 112 | |
| 113 | $scope.isUserSelected = true; |
| 114 | $scope.selectedUser = angular.copy(user); |
| 115 | $scope.selectedUser.VerifyPassword = null; |
| 116 | // Used while renaming the user. |
| 117 | $scope.selectedUser.CurrentUserName = $scope.selectedUser.UserName; |
| 118 | }; |
| 119 | $scope.createNewUser = function() { |
| 120 | $scope.state = 'none'; |
| 121 | $scope.outMsg = ''; |
| 122 | |
| 123 | if (!$scope.selectedUser.UserName || !$scope.selectedUser.Password) { |
| 124 | $scope.state = 'error'; |
| 125 | $scope.outMsg = 'Username or Password can\'t be empty'; |
| 126 | return; |
| 127 | } |
| 128 | if ($scope.selectedUser.Password !== |
| 129 | $scope.selectedUser.VerifyPassword) { |
| 130 | $scope.state = 'error'; |
| 131 | $scope.outMsg = 'Passwords do not match'; |
| 132 | return; |
| 133 | } |
| 134 | var user = $scope.selectedUser.UserName; |
| 135 | var passwd = $scope.selectedUser.Password; |
| 136 | var role = $scope.selectedUser.RoleId; |
| 137 | var enabled = false; |
| 138 | if ($scope.selectedUser.Enabled != null) { |
| 139 | enabled = $scope.selectedUser.Enabled; |
| 140 | } |
| 141 | |
| 142 | $scope.loading = true; |
| 143 | APIUtils.createUser(user, passwd, role, enabled) |
| 144 | .then( |
| 145 | function(response) { |
| 146 | $scope.state = 'success'; |
| 147 | $scope.outMsg = 'User has been created successfully'; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 148 | }, |
| 149 | function(error) { |
| 150 | $scope.state = 'error'; |
AppaRao Puli | a83cd05 | 2019-01-07 23:25:43 +0530 | [diff] [blame] | 151 | $scope.outMsg = 'Failed to create new user'; |
Gunnar Mills | b3d48d4 | 2018-05-25 08:34:35 -0500 | [diff] [blame] | 152 | }) |
| 153 | .finally(function() { |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 154 | loadUserInfo(); |
| 155 | $scope.loading = false; |
Gunnar Mills | b3d48d4 | 2018-05-25 08:34:35 -0500 | [diff] [blame] | 156 | }); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 157 | }; |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 158 | $scope.updateUserInfo = function() { |
| 159 | $scope.state = 'none'; |
| 160 | $scope.outMsg = ''; |
| 161 | if ($scope.selectedUser.Password !== |
| 162 | $scope.selectedUser.VerifyPassword) { |
| 163 | $scope.state = 'error'; |
| 164 | $scope.outMsg = 'Passwords do not match'; |
| 165 | return; |
| 166 | } |
| 167 | var data = {}; |
| 168 | if ($scope.selectedUser.UserName !== |
| 169 | $scope.selectedUser.CurrentUserName) { |
| 170 | data['UserName'] = $scope.selectedUser.UserName; |
| 171 | } |
| 172 | $scope.selectedUser.VerifyPassword = null; |
| 173 | if ($scope.selectedUser.Password != null) { |
| 174 | data['Password'] = $scope.selectedUser.Password; |
| 175 | } |
| 176 | data['RoleId'] = $scope.selectedUser.RoleId; |
| 177 | data['Enabled'] = $scope.selectedUser.Enabled; |
| 178 | |
| 179 | $scope.loading = true; |
| 180 | APIUtils |
| 181 | .updateUser( |
| 182 | $scope.selectedUser.CurrentUserName, data['UserName'], |
| 183 | data['Password'], data['RoleId'], data['Enabled']) |
| 184 | .then( |
| 185 | function(response) { |
| 186 | $scope.state = 'success'; |
| 187 | $scope.outMsg = 'User has been updated successfully'; |
| 188 | }, |
| 189 | function(error) { |
| 190 | $scope.state = 'error'; |
AppaRao Puli | a83cd05 | 2019-01-07 23:25:43 +0530 | [diff] [blame] | 191 | $scope.outMsg = 'Updating user failed'; |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 192 | }) |
| 193 | .finally(function() { |
| 194 | loadUserInfo(); |
| 195 | $scope.loading = false; |
| 196 | }); |
| 197 | }; |
| 198 | $scope.deleteUser = function(userName) { |
| 199 | $scope.state = 'none'; |
| 200 | $scope.outMsg = ''; |
| 201 | |
| 202 | $scope.loading = true; |
| 203 | APIUtils.deleteUser(userName) |
| 204 | .then( |
| 205 | function(response) { |
| 206 | $scope.state = 'success'; |
| 207 | $scope.outMsg = 'User has been deleted successfully'; |
| 208 | }, |
| 209 | function(error) { |
| 210 | $scope.state = 'error'; |
AppaRao Puli | a83cd05 | 2019-01-07 23:25:43 +0530 | [diff] [blame] | 211 | $scope.outMsg = 'Deleting user failed'; |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 212 | }) |
| 213 | .finally(function() { |
| 214 | loadUserInfo(); |
| 215 | $scope.loading = false; |
| 216 | }); |
| 217 | }; |
AppaRao Puli | cf7219c | 2018-12-27 16:17:46 +0530 | [diff] [blame] | 218 | |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 219 | loadUserInfo(); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 220 | } |
| 221 | ]); |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 222 | })(angular); |