blob: 5f908449f942d8ec30f83bad46fe7010ff67e33e [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 Puli28711a62018-10-17 16:07:55 +053013 '$scope', 'APIUtils',
14 function($scope, APIUtils) {
15 // TODO: Get the roles using Roles redfish URI.
16 $scope.roles = ['Administrator', 'Operator', 'User', 'Callback'];
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() {
22 $scope.users = [];
23 $scope.loading = true;
24 $scope.isUserSelected = false;
25 $scope.selectedUser = null;
Gunnar Mills6b97dde2018-03-14 17:04:58 -050026
AppaRao Puli28711a62018-10-17 16:07:55 +053027 APIUtils.getAllUserAccounts()
Andrew Geisslerd27bb132018-05-24 11:07:27 -070028 .then(
AppaRao Puli28711a62018-10-17 16:07:55 +053029 function(res) {
30 $scope.users = res;
31 },
32 function(error) {
33 console.log(JSON.stringify(error));
34 })
35 .finally(function() {
36 $scope.loading = false;
37 });
38 };
39 $scope.cancel = function() {
40 $scope.state = 'none';
41 $scope.outMsg = '';
42 loadUserInfo();
43 };
44 $scope.setSelectedUser = function(user) {
45 $scope.state = 'none';
46 $scope.outMsg = '';
47
48 $scope.isUserSelected = true;
49 $scope.selectedUser = angular.copy(user);
50 $scope.selectedUser.VerifyPassword = null;
51 // Used while renaming the user.
52 $scope.selectedUser.CurrentUserName = $scope.selectedUser.UserName;
53 };
54 $scope.createNewUser = function() {
55 $scope.state = 'none';
56 $scope.outMsg = '';
57
58 if (!$scope.selectedUser.UserName || !$scope.selectedUser.Password) {
59 $scope.state = 'error';
60 $scope.outMsg = 'Username or Password can\'t be empty';
61 return;
62 }
63 if ($scope.selectedUser.Password !==
64 $scope.selectedUser.VerifyPassword) {
65 $scope.state = 'error';
66 $scope.outMsg = 'Passwords do not match';
67 return;
68 }
69 var user = $scope.selectedUser.UserName;
70 var passwd = $scope.selectedUser.Password;
71 var role = $scope.selectedUser.RoleId;
72 var enabled = false;
73 if ($scope.selectedUser.Enabled != null) {
74 enabled = $scope.selectedUser.Enabled;
75 }
76
77 $scope.loading = true;
78 APIUtils.createUser(user, passwd, role, enabled)
79 .then(
80 function(response) {
81 $scope.state = 'success';
82 $scope.outMsg = 'User has been created successfully';
Andrew Geisslerd27bb132018-05-24 11:07:27 -070083 },
84 function(error) {
85 $scope.state = 'error';
AppaRao Puli28711a62018-10-17 16:07:55 +053086 if ((error.data.error['@Message.ExtendedInfo'] !=
87 undefined) &&
88 (error.data.error['@Message.ExtendedInfo'].length != 0)) {
89 $scope.outMsg =
90 error.data.error['@Message.ExtendedInfo'][0].Message;
91 } else {
92 $scope.outMsg = 'Failed to create new user.';
93 }
Gunnar Millsb3d48d42018-05-25 08:34:35 -050094 })
95 .finally(function() {
AppaRao Puli28711a62018-10-17 16:07:55 +053096 loadUserInfo();
97 $scope.loading = false;
Gunnar Millsb3d48d42018-05-25 08:34:35 -050098 });
Andrew Geisslerd27bb132018-05-24 11:07:27 -070099 };
AppaRao Puli28711a62018-10-17 16:07:55 +0530100 $scope.updateUserInfo = function() {
101 $scope.state = 'none';
102 $scope.outMsg = '';
103 if ($scope.selectedUser.Password !==
104 $scope.selectedUser.VerifyPassword) {
105 $scope.state = 'error';
106 $scope.outMsg = 'Passwords do not match';
107 return;
108 }
109 var data = {};
110 if ($scope.selectedUser.UserName !==
111 $scope.selectedUser.CurrentUserName) {
112 data['UserName'] = $scope.selectedUser.UserName;
113 }
114 $scope.selectedUser.VerifyPassword = null;
115 if ($scope.selectedUser.Password != null) {
116 data['Password'] = $scope.selectedUser.Password;
117 }
118 data['RoleId'] = $scope.selectedUser.RoleId;
119 data['Enabled'] = $scope.selectedUser.Enabled;
120
121 $scope.loading = true;
122 APIUtils
123 .updateUser(
124 $scope.selectedUser.CurrentUserName, data['UserName'],
125 data['Password'], data['RoleId'], data['Enabled'])
126 .then(
127 function(response) {
128 $scope.state = 'success';
129 $scope.outMsg = 'User has been updated successfully';
130 },
131 function(error) {
132 $scope.state = 'error';
133 if ((error.data.error['@Message.ExtendedInfo'] !=
134 undefined) &&
135 (error.data.error['@Message.ExtendedInfo'].length != 0)) {
136 $scope.outMsg =
137 error.data.error['@Message.ExtendedInfo'][0].Message;
138 } else {
139 $scope.outMsg = 'Updating user failed.';
140 }
141 })
142 .finally(function() {
143 loadUserInfo();
144 $scope.loading = false;
145 });
146 };
147 $scope.deleteUser = function(userName) {
148 $scope.state = 'none';
149 $scope.outMsg = '';
150
151 $scope.loading = true;
152 APIUtils.deleteUser(userName)
153 .then(
154 function(response) {
155 $scope.state = 'success';
156 $scope.outMsg = 'User has been deleted successfully';
157 },
158 function(error) {
159 $scope.state = 'error';
160 if ((error.data.error['@Message.ExtendedInfo'] !=
161 undefined) &&
162 (error.data.error['@Message.ExtendedInfo'].length != 0)) {
163 $scope.outMsg =
164 error.data.error['@Message.ExtendedInfo'][0].Message;
165 } else {
166 $scope.outMsg = 'Deleting user failed.';
167 }
168 })
169 .finally(function() {
170 loadUserInfo();
171 $scope.loading = false;
172 });
173 };
174 loadUserInfo();
Andrew Geisslerd27bb132018-05-24 11:07:27 -0700175 }
176 ]);
Iftekharul Islamcd789502017-04-19 14:37:55 -0500177})(angular);