blob: 4148f547e8503a8883366a680f5bcd2932554086 [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;
Gunnar Mills08744f82018-03-19 16:24:41 -050020
AppaRao Puli28711a62018-10-17 16:07:55 +053021 function loadUserInfo() {
AppaRao Puli28711a62018-10-17 16:07:55 +053022 $scope.loading = true;
23 $scope.isUserSelected = false;
24 $scope.selectedUser = null;
AppaRao Pulicf7219c2018-12-27 16:17:46 +053025 $q.all([
26 APIUtils.getAllUserAccounts().then(
AppaRao Puli28711a62018-10-17 16:07:55 +053027 function(res) {
28 $scope.users = res;
29 },
30 function(error) {
31 console.log(JSON.stringify(error));
AppaRao Pulicf7219c2018-12-27 16:17:46 +053032 }),
33 APIUtils.getAccountServiceRoles().then(
34 function(res) {
35 $scope.roles = res;
36 },
37 function(error) {
38 console.log(JSON.stringify(error));
AppaRao Puli28711a62018-10-17 16:07:55 +053039 })
AppaRao Pulicf7219c2018-12-27 16:17:46 +053040 ]).finally(function() {
41 $scope.loading = false;
42 });
AppaRao Puli28711a62018-10-17 16:07:55 +053043 };
AppaRao Pulicf7219c2018-12-27 16:17:46 +053044
AppaRao Puli28711a62018-10-17 16:07:55 +053045 $scope.cancel = function() {
46 $scope.state = 'none';
47 $scope.outMsg = '';
48 loadUserInfo();
49 };
50 $scope.setSelectedUser = function(user) {
51 $scope.state = 'none';
52 $scope.outMsg = '';
53
54 $scope.isUserSelected = true;
55 $scope.selectedUser = angular.copy(user);
56 $scope.selectedUser.VerifyPassword = null;
57 // Used while renaming the user.
58 $scope.selectedUser.CurrentUserName = $scope.selectedUser.UserName;
59 };
60 $scope.createNewUser = function() {
61 $scope.state = 'none';
62 $scope.outMsg = '';
63
64 if (!$scope.selectedUser.UserName || !$scope.selectedUser.Password) {
65 $scope.state = 'error';
66 $scope.outMsg = 'Username or Password can\'t be empty';
67 return;
68 }
69 if ($scope.selectedUser.Password !==
70 $scope.selectedUser.VerifyPassword) {
71 $scope.state = 'error';
72 $scope.outMsg = 'Passwords do not match';
73 return;
74 }
75 var user = $scope.selectedUser.UserName;
76 var passwd = $scope.selectedUser.Password;
77 var role = $scope.selectedUser.RoleId;
78 var enabled = false;
79 if ($scope.selectedUser.Enabled != null) {
80 enabled = $scope.selectedUser.Enabled;
81 }
82
83 $scope.loading = true;
84 APIUtils.createUser(user, passwd, role, enabled)
85 .then(
86 function(response) {
87 $scope.state = 'success';
88 $scope.outMsg = 'User has been created successfully';
Andrew Geisslerd27bb132018-05-24 11:07:27 -070089 },
90 function(error) {
91 $scope.state = 'error';
AppaRao Puli28711a62018-10-17 16:07:55 +053092 if ((error.data.error['@Message.ExtendedInfo'] !=
93 undefined) &&
94 (error.data.error['@Message.ExtendedInfo'].length != 0)) {
95 $scope.outMsg =
96 error.data.error['@Message.ExtendedInfo'][0].Message;
97 } else {
98 $scope.outMsg = 'Failed to create new user.';
99 }
Gunnar Millsb3d48d42018-05-25 08:34:35 -0500100 })
101 .finally(function() {
AppaRao Puli28711a62018-10-17 16:07:55 +0530102 loadUserInfo();
103 $scope.loading = false;
Gunnar Millsb3d48d42018-05-25 08:34:35 -0500104 });
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700105 };
AppaRao Puli28711a62018-10-17 16:07:55 +0530106 $scope.updateUserInfo = function() {
107 $scope.state = 'none';
108 $scope.outMsg = '';
109 if ($scope.selectedUser.Password !==
110 $scope.selectedUser.VerifyPassword) {
111 $scope.state = 'error';
112 $scope.outMsg = 'Passwords do not match';
113 return;
114 }
115 var data = {};
116 if ($scope.selectedUser.UserName !==
117 $scope.selectedUser.CurrentUserName) {
118 data['UserName'] = $scope.selectedUser.UserName;
119 }
120 $scope.selectedUser.VerifyPassword = null;
121 if ($scope.selectedUser.Password != null) {
122 data['Password'] = $scope.selectedUser.Password;
123 }
124 data['RoleId'] = $scope.selectedUser.RoleId;
125 data['Enabled'] = $scope.selectedUser.Enabled;
126
127 $scope.loading = true;
128 APIUtils
129 .updateUser(
130 $scope.selectedUser.CurrentUserName, data['UserName'],
131 data['Password'], data['RoleId'], data['Enabled'])
132 .then(
133 function(response) {
134 $scope.state = 'success';
135 $scope.outMsg = 'User has been updated successfully';
136 },
137 function(error) {
138 $scope.state = 'error';
139 if ((error.data.error['@Message.ExtendedInfo'] !=
140 undefined) &&
141 (error.data.error['@Message.ExtendedInfo'].length != 0)) {
142 $scope.outMsg =
143 error.data.error['@Message.ExtendedInfo'][0].Message;
144 } else {
145 $scope.outMsg = 'Updating user failed.';
146 }
147 })
148 .finally(function() {
149 loadUserInfo();
150 $scope.loading = false;
151 });
152 };
153 $scope.deleteUser = function(userName) {
154 $scope.state = 'none';
155 $scope.outMsg = '';
156
157 $scope.loading = true;
158 APIUtils.deleteUser(userName)
159 .then(
160 function(response) {
161 $scope.state = 'success';
162 $scope.outMsg = 'User has been deleted successfully';
163 },
164 function(error) {
165 $scope.state = 'error';
166 if ((error.data.error['@Message.ExtendedInfo'] !=
167 undefined) &&
168 (error.data.error['@Message.ExtendedInfo'].length != 0)) {
169 $scope.outMsg =
170 error.data.error['@Message.ExtendedInfo'][0].Message;
171 } else {
172 $scope.outMsg = 'Deleting user failed.';
173 }
174 })
175 .finally(function() {
176 loadUserInfo();
177 $scope.loading = false;
178 });
179 };
AppaRao Pulicf7219c2018-12-27 16:17:46 +0530180
AppaRao Puli28711a62018-10-17 16:07:55 +0530181 loadUserInfo();
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700182 }
183 ]);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500184})(angular);