blob: e12db008670c3c89ea98ecdf3ee484cfcaff1d04 [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', [
AppaRao Pulicf7219c2018-12-27 16:17:46 +053013 '$scope', '$q', 'APIUtils',
14 function($scope, $q, APIUtils) {
15 $scope.users = [];
16 $scope.roles = [];
Andrew Geisslerd27bb132018-05-24 11:07:27 -070017 $scope.state = 'none';
AppaRao Puli28711a62018-10-17 16:07:55 +053018 $scope.outMsg = '';
19 $scope.loading = true;
AppaRao Pulib1e7c862019-03-12 14:56:40 +053020 $scope.properties = {};
21 $scope.origProp = {};
Gunnar Mills08744f82018-03-19 16:24:41 -050022
AppaRao Puli28711a62018-10-17 16:07:55 +053023 function loadUserInfo() {
AppaRao Puli28711a62018-10-17 16:07:55 +053024 $scope.loading = true;
25 $scope.isUserSelected = false;
26 $scope.selectedUser = null;
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() {
61 $scope.state = 'none';
62 $scope.outMsg = '';
63 loadUserInfo();
64 };
AppaRao Pulib1e7c862019-03-12 14:56:40 +053065
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 Puli28711a62018-10-17 16:07:55 +0530109 $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 Geisslerd27bb132018-05-24 11:07:27 -0700148 },
149 function(error) {
150 $scope.state = 'error';
AppaRao Pulia83cd052019-01-07 23:25:43 +0530151 $scope.outMsg = 'Failed to create new user';
Gunnar Millsb3d48d42018-05-25 08:34:35 -0500152 })
153 .finally(function() {
AppaRao Puli28711a62018-10-17 16:07:55 +0530154 loadUserInfo();
155 $scope.loading = false;
Gunnar Millsb3d48d42018-05-25 08:34:35 -0500156 });
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700157 };
AppaRao Puli28711a62018-10-17 16:07:55 +0530158 $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 Pulia83cd052019-01-07 23:25:43 +0530191 $scope.outMsg = 'Updating user failed';
AppaRao Puli28711a62018-10-17 16:07:55 +0530192 })
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 Pulia83cd052019-01-07 23:25:43 +0530211 $scope.outMsg = 'Deleting user failed';
AppaRao Puli28711a62018-10-17 16:07:55 +0530212 })
213 .finally(function() {
214 loadUserInfo();
215 $scope.loading = false;
216 });
217 };
AppaRao Pulicf7219c2018-12-27 16:17:46 +0530218
AppaRao Puli28711a62018-10-17 16:07:55 +0530219 loadUserInfo();
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700220 }
221 ]);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500222})(angular);