blob: b76d8120ae41d7f8ee62e69125156426ad3c616a [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 Pulia83cd052019-01-07 23:25:43 +053025 $scope.togglePassword = false;
26 $scope.toggleVerify = false;
27
AppaRao Pulicf7219c2018-12-27 16:17:46 +053028 $q.all([
29 APIUtils.getAllUserAccounts().then(
AppaRao Puli28711a62018-10-17 16:07:55 +053030 function(res) {
31 $scope.users = res;
32 },
33 function(error) {
34 console.log(JSON.stringify(error));
AppaRao Pulicf7219c2018-12-27 16:17:46 +053035 }),
36 APIUtils.getAccountServiceRoles().then(
37 function(res) {
38 $scope.roles = res;
39 },
40 function(error) {
41 console.log(JSON.stringify(error));
AppaRao Puli28711a62018-10-17 16:07:55 +053042 })
AppaRao Pulicf7219c2018-12-27 16:17:46 +053043 ]).finally(function() {
44 $scope.loading = false;
45 });
AppaRao Puli28711a62018-10-17 16:07:55 +053046 };
AppaRao Pulicf7219c2018-12-27 16:17:46 +053047
AppaRao Puli28711a62018-10-17 16:07:55 +053048 $scope.cancel = function() {
49 $scope.state = 'none';
50 $scope.outMsg = '';
51 loadUserInfo();
52 };
53 $scope.setSelectedUser = function(user) {
54 $scope.state = 'none';
55 $scope.outMsg = '';
56
57 $scope.isUserSelected = true;
58 $scope.selectedUser = angular.copy(user);
59 $scope.selectedUser.VerifyPassword = null;
60 // Used while renaming the user.
61 $scope.selectedUser.CurrentUserName = $scope.selectedUser.UserName;
62 };
63 $scope.createNewUser = function() {
64 $scope.state = 'none';
65 $scope.outMsg = '';
66
67 if (!$scope.selectedUser.UserName || !$scope.selectedUser.Password) {
68 $scope.state = 'error';
69 $scope.outMsg = 'Username or Password can\'t be empty';
70 return;
71 }
72 if ($scope.selectedUser.Password !==
73 $scope.selectedUser.VerifyPassword) {
74 $scope.state = 'error';
75 $scope.outMsg = 'Passwords do not match';
76 return;
77 }
78 var user = $scope.selectedUser.UserName;
79 var passwd = $scope.selectedUser.Password;
80 var role = $scope.selectedUser.RoleId;
81 var enabled = false;
82 if ($scope.selectedUser.Enabled != null) {
83 enabled = $scope.selectedUser.Enabled;
84 }
85
86 $scope.loading = true;
87 APIUtils.createUser(user, passwd, role, enabled)
88 .then(
89 function(response) {
90 $scope.state = 'success';
91 $scope.outMsg = 'User has been created successfully';
Andrew Geisslerd27bb132018-05-24 11:07:27 -070092 },
93 function(error) {
94 $scope.state = 'error';
AppaRao Pulia83cd052019-01-07 23:25:43 +053095 $scope.outMsg = 'Failed to create new user';
Gunnar Millsb3d48d42018-05-25 08:34:35 -050096 })
97 .finally(function() {
AppaRao Puli28711a62018-10-17 16:07:55 +053098 loadUserInfo();
99 $scope.loading = false;
Gunnar Millsb3d48d42018-05-25 08:34:35 -0500100 });
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700101 };
AppaRao Puli28711a62018-10-17 16:07:55 +0530102 $scope.updateUserInfo = function() {
103 $scope.state = 'none';
104 $scope.outMsg = '';
105 if ($scope.selectedUser.Password !==
106 $scope.selectedUser.VerifyPassword) {
107 $scope.state = 'error';
108 $scope.outMsg = 'Passwords do not match';
109 return;
110 }
111 var data = {};
112 if ($scope.selectedUser.UserName !==
113 $scope.selectedUser.CurrentUserName) {
114 data['UserName'] = $scope.selectedUser.UserName;
115 }
116 $scope.selectedUser.VerifyPassword = null;
117 if ($scope.selectedUser.Password != null) {
118 data['Password'] = $scope.selectedUser.Password;
119 }
120 data['RoleId'] = $scope.selectedUser.RoleId;
121 data['Enabled'] = $scope.selectedUser.Enabled;
122
123 $scope.loading = true;
124 APIUtils
125 .updateUser(
126 $scope.selectedUser.CurrentUserName, data['UserName'],
127 data['Password'], data['RoleId'], data['Enabled'])
128 .then(
129 function(response) {
130 $scope.state = 'success';
131 $scope.outMsg = 'User has been updated successfully';
132 },
133 function(error) {
134 $scope.state = 'error';
AppaRao Pulia83cd052019-01-07 23:25:43 +0530135 $scope.outMsg = 'Updating user failed';
AppaRao Puli28711a62018-10-17 16:07:55 +0530136 })
137 .finally(function() {
138 loadUserInfo();
139 $scope.loading = false;
140 });
141 };
142 $scope.deleteUser = function(userName) {
143 $scope.state = 'none';
144 $scope.outMsg = '';
145
146 $scope.loading = true;
147 APIUtils.deleteUser(userName)
148 .then(
149 function(response) {
150 $scope.state = 'success';
151 $scope.outMsg = 'User has been deleted successfully';
152 },
153 function(error) {
154 $scope.state = 'error';
AppaRao Pulia83cd052019-01-07 23:25:43 +0530155 $scope.outMsg = 'Deleting user failed';
AppaRao Puli28711a62018-10-17 16:07:55 +0530156 })
157 .finally(function() {
158 loadUserInfo();
159 $scope.loading = false;
160 });
161 };
AppaRao Pulicf7219c2018-12-27 16:17:46 +0530162
AppaRao Puli28711a62018-10-17 16:07:55 +0530163 loadUserInfo();
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700164 }
165 ]);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500166})(angular);