blob: 113b420f386c4560af59ab65d36a03c5a78257e9 [file] [log] [blame]
Derick Montaguefded0d12019-12-11 06:16:40 -06001import api from '../../api';
Yoshie Muranaka183c2752020-02-12 11:30:49 -08002import i18n from '../../../i18n';
3
4const getResponseCount = responses => {
5 let successCount = 0;
6 let errorCount = 0;
7
8 responses.forEach(response => {
9 if (response instanceof Error) errorCount++;
10 else successCount++;
11 });
12
13 return {
14 successCount,
15 errorCount
16 };
17};
Yoshie Muranaka35080ac2020-01-17 15:38:57 -060018
19const LocalUserManagementStore = {
20 namespaced: true,
21 state: {
Yoshie Muranaka52b02232020-02-20 08:00:45 -080022 allUsers: [],
Yoshie Muranaka038a9da2020-04-17 11:22:56 -070023 accountRoles: [],
Yoshie Muranaka52b02232020-02-20 08:00:45 -080024 accountLockoutDuration: null,
25 accountLockoutThreshold: null,
26 accountMinPasswordLength: null,
27 accountMaxPasswordLength: null
Yoshie Muranaka35080ac2020-01-17 15:38:57 -060028 },
29 getters: {
30 allUsers(state) {
31 return state.allUsers;
Yoshie Muranaka52b02232020-02-20 08:00:45 -080032 },
Yoshie Muranaka038a9da2020-04-17 11:22:56 -070033 accountRoles(state) {
34 return state.accountRoles;
35 },
Yoshie Muranaka52b02232020-02-20 08:00:45 -080036 accountSettings(state) {
37 return {
38 lockoutDuration: state.accountLockoutDuration,
39 lockoutThreshold: state.accountLockoutThreshold
40 };
41 },
42 accountPasswordRequirements(state) {
43 return {
44 minLength: state.accountMinPasswordLength,
45 maxLength: state.accountMaxPasswordLength
46 };
Yoshie Muranaka35080ac2020-01-17 15:38:57 -060047 }
48 },
49 mutations: {
50 setUsers(state, allUsers) {
51 state.allUsers = allUsers;
Yoshie Muranaka52b02232020-02-20 08:00:45 -080052 },
Yoshie Muranaka038a9da2020-04-17 11:22:56 -070053 setAccountRoles(state, accountRoles) {
54 state.accountRoles = accountRoles;
55 },
Yoshie Muranaka52b02232020-02-20 08:00:45 -080056 setLockoutDuration(state, lockoutDuration) {
57 state.accountLockoutDuration = lockoutDuration;
58 },
59 setLockoutThreshold(state, lockoutThreshold) {
60 state.accountLockoutThreshold = lockoutThreshold;
61 },
62 setAccountMinPasswordLength(state, minPasswordLength) {
63 state.accountMinPasswordLength = minPasswordLength;
64 },
65 setAccountMaxPasswordLength(state, maxPasswordLength) {
66 state.accountMaxPasswordLength = maxPasswordLength;
Yoshie Muranaka35080ac2020-01-17 15:38:57 -060067 }
68 },
69 actions: {
70 getUsers({ commit }) {
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080071 api
Derick Montaguefded0d12019-12-11 06:16:40 -060072 .get('/redfish/v1/AccountService/Accounts')
73 .then(response => response.data.Members.map(user => user['@odata.id']))
Yoshie Muranaka463a5702019-12-04 09:09:36 -080074 .then(userIds => api.all(userIds.map(user => api.get(user))))
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080075 .then(users => {
76 const userData = users.map(user => user.data);
Derick Montaguefded0d12019-12-11 06:16:40 -060077 commit('setUsers', userData);
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080078 })
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -080079 .catch(error => {
80 console.log(error);
81 throw new Error('Error loading local users.');
82 });
Yoshie Muranaka463a5702019-12-04 09:09:36 -080083 },
Yoshie Muranaka52b02232020-02-20 08:00:45 -080084 getAccountSettings({ commit }) {
85 api
86 .get('/redfish/v1/AccountService')
87 .then(({ data }) => {
88 commit('setLockoutDuration', data.AccountLockoutDuration);
89 commit('setLockoutThreshold', data.AccountLockoutThreshold);
90 commit('setAccountMinPasswordLength', data.MinPasswordLength);
91 commit('setAccountMaxPasswordLength', data.MaxPasswordLength);
92 })
93 .catch(error => {
94 console.log(error);
95 throw new Error('Error loading account settings.');
96 });
97 },
Yoshie Muranaka038a9da2020-04-17 11:22:56 -070098 getAccountRoles({ commit }) {
99 api
100 .get('/redfish/v1/AccountService/Roles')
101 .then(({ data: { Members = [] } = {} }) => {
102 const roles = Members.map(role => {
103 return role['@odata.id'].split('/').pop();
104 });
105 commit('setAccountRoles', roles);
106 })
107 .catch(error => console.log(error));
108 },
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800109 async createUser({ dispatch }, { username, password, privilege, status }) {
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800110 const data = {
111 UserName: username,
112 Password: password,
113 RoleId: privilege,
114 Enabled: status
115 };
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800116 return await api
Derick Montaguefded0d12019-12-11 06:16:40 -0600117 .post('/redfish/v1/AccountService/Accounts', data)
118 .then(() => dispatch('getUsers'))
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800119 .then(() => `Created user '${username}'.`)
120 .catch(error => {
121 console.log(error);
122 throw new Error(`Error creating user '${username}'.`);
123 });
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800124 },
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800125 async updateUser(
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800126 { dispatch },
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700127 { originalUsername, username, password, privilege, status, locked }
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800128 ) {
129 const data = {};
130 if (username) data.UserName = username;
131 if (password) data.Password = password;
132 if (privilege) data.RoleId = privilege;
133 if (status !== undefined) data.Enabled = status;
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700134 if (locked !== undefined) data.Locked = locked;
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800135 return await api
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800136 .patch(`/redfish/v1/AccountService/Accounts/${originalUsername}`, data)
Derick Montaguefded0d12019-12-11 06:16:40 -0600137 .then(() => dispatch('getUsers'))
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800138 .then(() => `Updated user '${originalUsername}'.`)
139 .catch(error => {
140 console.log(error);
141 throw new Error(`Error updating user '${originalUsername}'.`);
142 });
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800143 },
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800144 async deleteUser({ dispatch }, username) {
145 return await api
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800146 .delete(`/redfish/v1/AccountService/Accounts/${username}`)
Derick Montaguefded0d12019-12-11 06:16:40 -0600147 .then(() => dispatch('getUsers'))
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800148 .then(() => `Deleted user '${username}'.`)
149 .catch(error => {
150 console.log(error);
151 throw new Error(`Error deleting user '${username}'.`);
152 });
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800153 },
154 async deleteUsers({ dispatch }, users) {
155 const promises = users.map(({ username }) => {
156 return api
157 .delete(`/redfish/v1/AccountService/Accounts/${username}`)
158 .catch(error => {
159 console.log(error);
160 return error;
161 });
162 });
163 return await api
164 .all(promises)
165 .then(response => {
166 dispatch('getUsers');
167 return response;
168 })
169 .then(
170 api.spread((...responses) => {
171 const { successCount, errorCount } = getResponseCount(responses);
172 let toastMessages = [];
173
174 if (successCount) {
175 const message = i18n.tc(
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -0800176 'pageLocalUserManagement.toast.successDeleteUsers',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800177 successCount
178 );
179 toastMessages.push({ type: 'success', message });
180 }
181
182 if (errorCount) {
183 const message = i18n.tc(
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -0800184 'pageLocalUserManagement.toast.errorDeleteUsers',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800185 errorCount
186 );
187 toastMessages.push({ type: 'error', message });
188 }
189
190 return toastMessages;
191 })
192 );
193 },
194 async enableUsers({ dispatch }, users) {
195 const data = {
196 Enabled: true
197 };
198 const promises = users.map(({ username }) => {
199 return api
200 .patch(`/redfish/v1/AccountService/Accounts/${username}`, data)
201 .catch(error => {
202 console.log(error);
203 return error;
204 });
205 });
206 return await api
207 .all(promises)
208 .then(response => {
209 dispatch('getUsers');
210 return response;
211 })
212 .then(
213 api.spread((...responses) => {
214 const { successCount, errorCount } = getResponseCount(responses);
215 let toastMessages = [];
216
217 if (successCount) {
218 const message = i18n.tc(
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -0800219 'pageLocalUserManagement.toast.successEnableUsers',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800220 successCount
221 );
222 toastMessages.push({ type: 'success', message });
223 }
224
225 if (errorCount) {
226 const message = i18n.tc(
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -0800227 'pageLocalUserManagement.toast.errorEnableUsers',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800228 errorCount
229 );
230 toastMessages.push({ type: 'error', message });
231 }
232
233 return toastMessages;
234 })
235 );
236 },
237 async disableUsers({ dispatch }, users) {
238 const data = {
239 Enabled: false
240 };
241 const promises = users.map(({ username }) => {
242 return api
243 .patch(`/redfish/v1/AccountService/Accounts/${username}`, data)
244 .catch(error => {
245 console.log(error);
246 return error;
247 });
248 });
249 return await api
250 .all(promises)
251 .then(response => {
252 dispatch('getUsers');
253 return response;
254 })
255 .then(
256 api.spread((...responses) => {
257 const { successCount, errorCount } = getResponseCount(responses);
258 let toastMessages = [];
259
260 if (successCount) {
261 const message = i18n.tc(
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -0800262 'pageLocalUserManagement.toast.successDisableUsers',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800263 successCount
264 );
265 toastMessages.push({ type: 'success', message });
266 }
267
268 if (errorCount) {
269 const message = i18n.tc(
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -0800270 'pageLocalUserManagement.toast.errorDisableUsers',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800271 errorCount
272 );
273 toastMessages.push({ type: 'error', message });
274 }
275
276 return toastMessages;
277 })
278 );
Yoshie Muranaka1b1c1002020-02-20 10:18:36 -0800279 },
280 async saveAccountSettings(
281 { dispatch },
282 { lockoutThreshold, lockoutDuration }
283 ) {
284 const data = {};
285 if (lockoutThreshold !== undefined) {
286 data.AccountLockoutThreshold = lockoutThreshold;
287 }
288 if (lockoutDuration !== undefined) {
289 data.AccountLockoutDuration = lockoutDuration;
290 }
291
292 return await api
293 .patch('/redfish/v1/AccountService', data)
294 //GET new settings to update view
295 .then(() => dispatch('getAccountSettings'))
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -0800296 .then(() => i18n.t('pageLocalUserManagement.toast.successSaveSettings'))
Yoshie Muranaka1b1c1002020-02-20 10:18:36 -0800297 .catch(error => {
298 console.log(error);
299 const message = i18n.t(
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -0800300 'pageLocalUserManagement.toast.errorSaveSettings'
Yoshie Muranaka1b1c1002020-02-20 10:18:36 -0800301 );
302 throw new Error(message);
303 });
Yoshie Muranaka35080ac2020-01-17 15:38:57 -0600304 }
305 }
306};
307
308export default LocalUserManagementStore;