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', [ |
beccabroek | a5deeea | 2019-03-22 15:38:49 -0500 | [diff] [blame] | 13 | '$scope', '$q', 'APIUtils', 'toastService', |
| 14 | function($scope, $q, APIUtils, toastService) { |
AppaRao Puli | cf7219c | 2018-12-27 16:17:46 +0530 | [diff] [blame] | 15 | $scope.users = []; |
| 16 | $scope.roles = []; |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 17 | $scope.loading = true; |
AppaRao Puli | b1e7c86 | 2019-03-12 14:56:40 +0530 | [diff] [blame] | 18 | $scope.properties = {}; |
| 19 | $scope.origProp = {}; |
beccabroek | a5deeea | 2019-03-22 15:38:49 -0500 | [diff] [blame] | 20 | $scope.submitted = false; |
Gunnar Mills | 08744f8 | 2018-03-19 16:24:41 -0500 | [diff] [blame] | 21 | |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 22 | function loadUserInfo() { |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 23 | $scope.loading = true; |
beccabroek | a5deeea | 2019-03-22 15:38:49 -0500 | [diff] [blame] | 24 | $scope.submitted = false; |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 25 | $scope.isUserSelected = false; |
beccabroek | a5deeea | 2019-03-22 15:38:49 -0500 | [diff] [blame] | 26 | $scope.selectedUser = {}; |
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() { |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 61 | loadUserInfo(); |
| 62 | }; |
AppaRao Puli | b1e7c86 | 2019-03-12 14:56:40 +0530 | [diff] [blame] | 63 | |
| 64 | $scope.saveAllValues = function() { |
AppaRao Puli | b1e7c86 | 2019-03-12 14:56:40 +0530 | [diff] [blame] | 65 | $scope.loading = true; |
| 66 | var data = {}; |
| 67 | if ($scope.properties.AccountLockoutDuration != |
| 68 | $scope.origProp.AccountLockoutDuration) { |
| 69 | data['AccountLockoutDuration'] = |
| 70 | $scope.properties.AccountLockoutDuration; |
| 71 | } |
| 72 | if ($scope.properties.AccountLockoutThreshold != |
| 73 | $scope.origProp.AccountLockoutThreshold) { |
| 74 | data['AccountLockoutThreshold'] = |
| 75 | $scope.properties.AccountLockoutThreshold; |
| 76 | } |
| 77 | |
| 78 | if ($scope.properties.AccountLockoutDuration == |
| 79 | $scope.origProp.AccountLockoutDuration && |
| 80 | $scope.properties.AccountLockoutThreshold == |
| 81 | $scope.origProp.AccountLockoutThreshold) { |
| 82 | // No change in properties, just return; |
| 83 | $scope.loading = false; |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | APIUtils |
| 88 | .saveUserAccountProperties( |
| 89 | data['AccountLockoutDuration'], data['AccountLockoutThreshold']) |
| 90 | .then( |
| 91 | function(response) { |
beccabroek | a5deeea | 2019-03-22 15:38:49 -0500 | [diff] [blame] | 92 | toastService.success( |
| 93 | 'User account properties have been updated successfully'); |
AppaRao Puli | b1e7c86 | 2019-03-12 14:56:40 +0530 | [diff] [blame] | 94 | }, |
| 95 | function(error) { |
beccabroek | a5deeea | 2019-03-22 15:38:49 -0500 | [diff] [blame] | 96 | toastService.error('Unable to update account properties'); |
AppaRao Puli | b1e7c86 | 2019-03-12 14:56:40 +0530 | [diff] [blame] | 97 | }) |
| 98 | .finally(function() { |
| 99 | loadUserInfo(); |
| 100 | $scope.loading = false; |
| 101 | }); |
| 102 | }; |
| 103 | |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 104 | $scope.setSelectedUser = function(user) { |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 105 | $scope.isUserSelected = true; |
| 106 | $scope.selectedUser = angular.copy(user); |
| 107 | $scope.selectedUser.VerifyPassword = null; |
| 108 | // Used while renaming the user. |
| 109 | $scope.selectedUser.CurrentUserName = $scope.selectedUser.UserName; |
| 110 | }; |
| 111 | $scope.createNewUser = function() { |
beccabroek | a5deeea | 2019-03-22 15:38:49 -0500 | [diff] [blame] | 112 | if ($scope.users.length >= 15) { |
| 113 | toastService.error( |
| 114 | 'Cannot create user. The maximum number of users that can be created is 15'); |
| 115 | return; |
| 116 | } |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 117 | if (!$scope.selectedUser.UserName || !$scope.selectedUser.Password) { |
beccabroek | a5deeea | 2019-03-22 15:38:49 -0500 | [diff] [blame] | 118 | toastService.error('Username or password cannot be empty'); |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 119 | return; |
| 120 | } |
| 121 | if ($scope.selectedUser.Password !== |
| 122 | $scope.selectedUser.VerifyPassword) { |
beccabroek | a5deeea | 2019-03-22 15:38:49 -0500 | [diff] [blame] | 123 | toastService.error('Passwords do not match'); |
| 124 | return; |
| 125 | } |
| 126 | if ($scope.doesUserExist()) { |
| 127 | toastService.error('Username already exists'); |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 128 | return; |
| 129 | } |
| 130 | var user = $scope.selectedUser.UserName; |
| 131 | var passwd = $scope.selectedUser.Password; |
| 132 | var role = $scope.selectedUser.RoleId; |
| 133 | var enabled = false; |
| 134 | if ($scope.selectedUser.Enabled != null) { |
| 135 | enabled = $scope.selectedUser.Enabled; |
| 136 | } |
| 137 | |
| 138 | $scope.loading = true; |
| 139 | APIUtils.createUser(user, passwd, role, enabled) |
| 140 | .then( |
| 141 | function(response) { |
beccabroek | a5deeea | 2019-03-22 15:38:49 -0500 | [diff] [blame] | 142 | toastService.success('User has been created successfully'); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 143 | }, |
| 144 | function(error) { |
beccabroek | a5deeea | 2019-03-22 15:38:49 -0500 | [diff] [blame] | 145 | toastService.error('Failed to create new user'); |
Gunnar Mills | b3d48d4 | 2018-05-25 08:34:35 -0500 | [diff] [blame] | 146 | }) |
| 147 | .finally(function() { |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 148 | loadUserInfo(); |
| 149 | $scope.loading = false; |
Gunnar Mills | b3d48d4 | 2018-05-25 08:34:35 -0500 | [diff] [blame] | 150 | }); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 151 | }; |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 152 | $scope.updateUserInfo = function() { |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 153 | if ($scope.selectedUser.Password !== |
| 154 | $scope.selectedUser.VerifyPassword) { |
beccabroek | a5deeea | 2019-03-22 15:38:49 -0500 | [diff] [blame] | 155 | toastService.error('Passwords do not match'); |
| 156 | return; |
| 157 | } |
| 158 | if ($scope.doesUserExist()) { |
| 159 | toastService.error('Username already exists'); |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 160 | return; |
| 161 | } |
| 162 | var data = {}; |
| 163 | if ($scope.selectedUser.UserName !== |
| 164 | $scope.selectedUser.CurrentUserName) { |
| 165 | data['UserName'] = $scope.selectedUser.UserName; |
| 166 | } |
| 167 | $scope.selectedUser.VerifyPassword = null; |
| 168 | if ($scope.selectedUser.Password != null) { |
| 169 | data['Password'] = $scope.selectedUser.Password; |
| 170 | } |
| 171 | data['RoleId'] = $scope.selectedUser.RoleId; |
| 172 | data['Enabled'] = $scope.selectedUser.Enabled; |
| 173 | |
| 174 | $scope.loading = true; |
| 175 | APIUtils |
| 176 | .updateUser( |
| 177 | $scope.selectedUser.CurrentUserName, data['UserName'], |
| 178 | data['Password'], data['RoleId'], data['Enabled']) |
| 179 | .then( |
| 180 | function(response) { |
beccabroek | a5deeea | 2019-03-22 15:38:49 -0500 | [diff] [blame] | 181 | toastService.success('User has been updated successfully'); |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 182 | }, |
| 183 | function(error) { |
beccabroek | a5deeea | 2019-03-22 15:38:49 -0500 | [diff] [blame] | 184 | toastService.error('Unable to update user'); |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 185 | }) |
| 186 | .finally(function() { |
| 187 | loadUserInfo(); |
| 188 | $scope.loading = false; |
| 189 | }); |
| 190 | }; |
| 191 | $scope.deleteUser = function(userName) { |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 192 | $scope.loading = true; |
| 193 | APIUtils.deleteUser(userName) |
| 194 | .then( |
| 195 | function(response) { |
beccabroek | a5deeea | 2019-03-22 15:38:49 -0500 | [diff] [blame] | 196 | toastService.success('User has been deleted successfully'); |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 197 | }, |
| 198 | function(error) { |
beccabroek | a5deeea | 2019-03-22 15:38:49 -0500 | [diff] [blame] | 199 | toastService.error('Unable to delete user'); |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 200 | }) |
| 201 | .finally(function() { |
| 202 | loadUserInfo(); |
| 203 | $scope.loading = false; |
| 204 | }); |
| 205 | }; |
AppaRao Puli | cf7219c | 2018-12-27 16:17:46 +0530 | [diff] [blame] | 206 | |
beccabroek | a5deeea | 2019-03-22 15:38:49 -0500 | [diff] [blame] | 207 | $scope.doesUserExist = function() { |
| 208 | for (var i in $scope.users) { |
| 209 | // If a user exists with the same user name and a different Id then |
| 210 | // the username already exists and isn't valid |
| 211 | if (($scope.users[i].UserName === $scope.selectedUser.UserName) && |
| 212 | ($scope.users[i].Id !== $scope.selectedUser.Id)) { |
| 213 | return true; |
| 214 | } |
| 215 | } |
| 216 | }; |
AppaRao Puli | 28711a6 | 2018-10-17 16:07:55 +0530 | [diff] [blame] | 217 | loadUserInfo(); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 218 | } |
| 219 | ]); |
Iftekharul Islam | cd78950 | 2017-04-19 14:37:55 -0500 | [diff] [blame] | 220 | })(angular); |