blob: 12ec1706c94f3c9378c13f32965b1f173a0baac0 [file] [log] [blame]
Iftekharul Islamcd789502017-04-19 14:37:55 -05001/**
2 * Controller for user Accounts
3 *
4 * @module app/users
5 * @exports userAccountsController
6 * @name userAccountsController
Iftekharul Islamcd789502017-04-19 14:37:55 -05007 */
8
Andrew Geisslerba5e3f32018-05-24 10:58:00 -07009window.angular && (function(angular) {
10 'use strict';
Iftekharul Islamcd789502017-04-19 14:37:55 -050011
Andrew Geisslerd27bb132018-05-24 11:07:27 -070012 angular.module('app.users').controller('userAccountsController', [
beccabroeka5deeea2019-03-22 15:38:49 -050013 '$scope', '$q', 'APIUtils', 'toastService',
14 function($scope, $q, APIUtils, toastService) {
AppaRao Pulicf7219c2018-12-27 16:17:46 +053015 $scope.users = [];
16 $scope.roles = [];
AppaRao Puli28711a62018-10-17 16:07:55 +053017 $scope.loading = true;
AppaRao Pulib1e7c862019-03-12 14:56:40 +053018 $scope.properties = {};
19 $scope.origProp = {};
beccabroeka5deeea2019-03-22 15:38:49 -050020 $scope.submitted = false;
Gunnar Mills08744f82018-03-19 16:24:41 -050021
AppaRao Puli28711a62018-10-17 16:07:55 +053022 function loadUserInfo() {
AppaRao Puli28711a62018-10-17 16:07:55 +053023 $scope.loading = true;
beccabroeka5deeea2019-03-22 15:38:49 -050024 $scope.submitted = false;
AppaRao Puli28711a62018-10-17 16:07:55 +053025 $scope.isUserSelected = false;
beccabroeka5deeea2019-03-22 15:38:49 -050026 $scope.selectedUser = {};
AppaRao Pulia83cd052019-01-07 23:25:43 +053027 $scope.togglePassword = false;
28 $scope.toggleVerify = false;
29
AppaRao Pulicf7219c2018-12-27 16:17:46 +053030 $q.all([
31 APIUtils.getAllUserAccounts().then(
AppaRao Puli28711a62018-10-17 16:07:55 +053032 function(res) {
33 $scope.users = res;
34 },
35 function(error) {
36 console.log(JSON.stringify(error));
AppaRao Pulicf7219c2018-12-27 16:17:46 +053037 }),
AppaRao Pulib1e7c862019-03-12 14:56:40 +053038
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 Pulicf7219c2018-12-27 16:17:46 +053048 APIUtils.getAccountServiceRoles().then(
49 function(res) {
50 $scope.roles = res;
51 },
52 function(error) {
53 console.log(JSON.stringify(error));
AppaRao Puli28711a62018-10-17 16:07:55 +053054 })
AppaRao Pulicf7219c2018-12-27 16:17:46 +053055 ]).finally(function() {
56 $scope.loading = false;
57 });
AppaRao Puli28711a62018-10-17 16:07:55 +053058 };
AppaRao Pulicf7219c2018-12-27 16:17:46 +053059
AppaRao Puli28711a62018-10-17 16:07:55 +053060 $scope.cancel = function() {
AppaRao Puli28711a62018-10-17 16:07:55 +053061 loadUserInfo();
62 };
AppaRao Pulib1e7c862019-03-12 14:56:40 +053063
64 $scope.saveAllValues = function() {
AppaRao Pulib1e7c862019-03-12 14:56:40 +053065 $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) {
beccabroeka5deeea2019-03-22 15:38:49 -050092 toastService.success(
93 'User account properties have been updated successfully');
AppaRao Pulib1e7c862019-03-12 14:56:40 +053094 },
95 function(error) {
beccabroeka5deeea2019-03-22 15:38:49 -050096 toastService.error('Unable to update account properties');
AppaRao Pulib1e7c862019-03-12 14:56:40 +053097 })
98 .finally(function() {
99 loadUserInfo();
100 $scope.loading = false;
101 });
102 };
103
AppaRao Puli28711a62018-10-17 16:07:55 +0530104 $scope.setSelectedUser = function(user) {
AppaRao Puli28711a62018-10-17 16:07:55 +0530105 $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() {
beccabroeka5deeea2019-03-22 15:38:49 -0500112 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 Puli28711a62018-10-17 16:07:55 +0530117 if (!$scope.selectedUser.UserName || !$scope.selectedUser.Password) {
beccabroeka5deeea2019-03-22 15:38:49 -0500118 toastService.error('Username or password cannot be empty');
AppaRao Puli28711a62018-10-17 16:07:55 +0530119 return;
120 }
121 if ($scope.selectedUser.Password !==
122 $scope.selectedUser.VerifyPassword) {
beccabroeka5deeea2019-03-22 15:38:49 -0500123 toastService.error('Passwords do not match');
124 return;
125 }
126 if ($scope.doesUserExist()) {
127 toastService.error('Username already exists');
AppaRao Puli28711a62018-10-17 16:07:55 +0530128 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) {
beccabroeka5deeea2019-03-22 15:38:49 -0500142 toastService.success('User has been created successfully');
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700143 },
144 function(error) {
beccabroeka5deeea2019-03-22 15:38:49 -0500145 toastService.error('Failed to create new user');
Gunnar Millsb3d48d42018-05-25 08:34:35 -0500146 })
147 .finally(function() {
AppaRao Puli28711a62018-10-17 16:07:55 +0530148 loadUserInfo();
149 $scope.loading = false;
Gunnar Millsb3d48d42018-05-25 08:34:35 -0500150 });
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700151 };
AppaRao Puli28711a62018-10-17 16:07:55 +0530152 $scope.updateUserInfo = function() {
AppaRao Puli28711a62018-10-17 16:07:55 +0530153 if ($scope.selectedUser.Password !==
154 $scope.selectedUser.VerifyPassword) {
beccabroeka5deeea2019-03-22 15:38:49 -0500155 toastService.error('Passwords do not match');
156 return;
157 }
158 if ($scope.doesUserExist()) {
159 toastService.error('Username already exists');
AppaRao Puli28711a62018-10-17 16:07:55 +0530160 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) {
beccabroeka5deeea2019-03-22 15:38:49 -0500181 toastService.success('User has been updated successfully');
AppaRao Puli28711a62018-10-17 16:07:55 +0530182 },
183 function(error) {
beccabroeka5deeea2019-03-22 15:38:49 -0500184 toastService.error('Unable to update user');
AppaRao Puli28711a62018-10-17 16:07:55 +0530185 })
186 .finally(function() {
187 loadUserInfo();
188 $scope.loading = false;
189 });
190 };
191 $scope.deleteUser = function(userName) {
AppaRao Puli28711a62018-10-17 16:07:55 +0530192 $scope.loading = true;
193 APIUtils.deleteUser(userName)
194 .then(
195 function(response) {
beccabroeka5deeea2019-03-22 15:38:49 -0500196 toastService.success('User has been deleted successfully');
AppaRao Puli28711a62018-10-17 16:07:55 +0530197 },
198 function(error) {
beccabroeka5deeea2019-03-22 15:38:49 -0500199 toastService.error('Unable to delete user');
AppaRao Puli28711a62018-10-17 16:07:55 +0530200 })
201 .finally(function() {
202 loadUserInfo();
203 $scope.loading = false;
204 });
205 };
AppaRao Pulicf7219c2018-12-27 16:17:46 +0530206
beccabroeka5deeea2019-03-22 15:38:49 -0500207 $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 Puli28711a62018-10-17 16:07:55 +0530217 loadUserInfo();
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700218 }
219 ]);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500220})(angular);