blob: b9b67eac93eeabe346c8d0d892f9be60a0d4a8e7 [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: {
Yoshie Muranaka346be2a2020-04-28 11:12:14 -070070 async getUsers({ commit }) {
71 return await 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);
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -070081 const message = i18n.t(
82 'pageLocalUserManagement.toast.errorLoadUsers'
83 );
84 throw new Error(message);
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -080085 });
Yoshie Muranaka463a5702019-12-04 09:09:36 -080086 },
Yoshie Muranaka52b02232020-02-20 08:00:45 -080087 getAccountSettings({ commit }) {
88 api
89 .get('/redfish/v1/AccountService')
90 .then(({ data }) => {
91 commit('setLockoutDuration', data.AccountLockoutDuration);
92 commit('setLockoutThreshold', data.AccountLockoutThreshold);
93 commit('setAccountMinPasswordLength', data.MinPasswordLength);
94 commit('setAccountMaxPasswordLength', data.MaxPasswordLength);
95 })
96 .catch(error => {
97 console.log(error);
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -070098 const message = i18n.t(
99 'pageLocalUserManagement.toast.errorLoadAccountSettings'
100 );
101 throw new Error(message);
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800102 });
103 },
Yoshie Muranaka038a9da2020-04-17 11:22:56 -0700104 getAccountRoles({ commit }) {
105 api
106 .get('/redfish/v1/AccountService/Roles')
107 .then(({ data: { Members = [] } = {} }) => {
108 const roles = Members.map(role => {
109 return role['@odata.id'].split('/').pop();
110 });
111 commit('setAccountRoles', roles);
112 })
113 .catch(error => console.log(error));
114 },
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800115 async createUser({ dispatch }, { username, password, privilege, status }) {
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800116 const data = {
117 UserName: username,
118 Password: password,
119 RoleId: privilege,
120 Enabled: status
121 };
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800122 return await api
Derick Montaguefded0d12019-12-11 06:16:40 -0600123 .post('/redfish/v1/AccountService/Accounts', data)
124 .then(() => dispatch('getUsers'))
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700125 .then(() =>
126 i18n.t('pageLocalUserManagement.toast.successCreateUser', {
127 username
128 })
129 )
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800130 .catch(error => {
131 console.log(error);
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700132 const message = i18n.t(
133 'pageLocalUserManagement.toast.errorCreateUser',
134 { username }
135 );
136 throw new Error(message);
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800137 });
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800138 },
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800139 async updateUser(
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800140 { dispatch },
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700141 { originalUsername, username, password, privilege, status, locked }
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800142 ) {
143 const data = {};
144 if (username) data.UserName = username;
145 if (password) data.Password = password;
146 if (privilege) data.RoleId = privilege;
147 if (status !== undefined) data.Enabled = status;
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700148 if (locked !== undefined) data.Locked = locked;
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800149 return await api
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800150 .patch(`/redfish/v1/AccountService/Accounts/${originalUsername}`, data)
Derick Montaguefded0d12019-12-11 06:16:40 -0600151 .then(() => dispatch('getUsers'))
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700152 .then(() =>
153 i18n.t('pageLocalUserManagement.toast.successUpdateUser', {
154 username: originalUsername
155 })
156 )
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800157 .catch(error => {
158 console.log(error);
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700159 const message = i18n.t(
160 'pageLocalUserManagement.toast.errorUpdateUser',
161 { username: originalUsername }
162 );
163 throw new Error(message);
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800164 });
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800165 },
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800166 async deleteUser({ dispatch }, username) {
167 return await api
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800168 .delete(`/redfish/v1/AccountService/Accounts/${username}`)
Derick Montaguefded0d12019-12-11 06:16:40 -0600169 .then(() => dispatch('getUsers'))
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700170 .then(() =>
171 i18n.t('pageLocalUserManagement.toast.successDeleteUser', {
172 username
173 })
174 )
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800175 .catch(error => {
176 console.log(error);
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700177 const message = i18n.t(
178 'pageLocalUserManagement.toast.errorDeleteUser',
179 { username }
180 );
181 throw new Error(message);
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800182 });
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800183 },
184 async deleteUsers({ dispatch }, users) {
185 const promises = users.map(({ username }) => {
186 return api
187 .delete(`/redfish/v1/AccountService/Accounts/${username}`)
188 .catch(error => {
189 console.log(error);
190 return error;
191 });
192 });
193 return await api
194 .all(promises)
195 .then(response => {
196 dispatch('getUsers');
197 return response;
198 })
199 .then(
200 api.spread((...responses) => {
201 const { successCount, errorCount } = getResponseCount(responses);
202 let toastMessages = [];
203
204 if (successCount) {
205 const message = i18n.tc(
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700206 'pageLocalUserManagement.toast.successBatchDelete',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800207 successCount
208 );
209 toastMessages.push({ type: 'success', message });
210 }
211
212 if (errorCount) {
213 const message = i18n.tc(
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700214 'pageLocalUserManagement.toast.errorBatchDelete',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800215 errorCount
216 );
217 toastMessages.push({ type: 'error', message });
218 }
219
220 return toastMessages;
221 })
222 );
223 },
224 async enableUsers({ dispatch }, users) {
225 const data = {
226 Enabled: true
227 };
228 const promises = users.map(({ username }) => {
229 return api
230 .patch(`/redfish/v1/AccountService/Accounts/${username}`, data)
231 .catch(error => {
232 console.log(error);
233 return error;
234 });
235 });
236 return await api
237 .all(promises)
238 .then(response => {
239 dispatch('getUsers');
240 return response;
241 })
242 .then(
243 api.spread((...responses) => {
244 const { successCount, errorCount } = getResponseCount(responses);
245 let toastMessages = [];
246
247 if (successCount) {
248 const message = i18n.tc(
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700249 'pageLocalUserManagement.toast.successBatchEnable',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800250 successCount
251 );
252 toastMessages.push({ type: 'success', message });
253 }
254
255 if (errorCount) {
256 const message = i18n.tc(
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700257 'pageLocalUserManagement.toast.errorBatchEnable',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800258 errorCount
259 );
260 toastMessages.push({ type: 'error', message });
261 }
262
263 return toastMessages;
264 })
265 );
266 },
267 async disableUsers({ dispatch }, users) {
268 const data = {
269 Enabled: false
270 };
271 const promises = users.map(({ username }) => {
272 return api
273 .patch(`/redfish/v1/AccountService/Accounts/${username}`, data)
274 .catch(error => {
275 console.log(error);
276 return error;
277 });
278 });
279 return await api
280 .all(promises)
281 .then(response => {
282 dispatch('getUsers');
283 return response;
284 })
285 .then(
286 api.spread((...responses) => {
287 const { successCount, errorCount } = getResponseCount(responses);
288 let toastMessages = [];
289
290 if (successCount) {
291 const message = i18n.tc(
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700292 'pageLocalUserManagement.toast.successBatchDisable',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800293 successCount
294 );
295 toastMessages.push({ type: 'success', message });
296 }
297
298 if (errorCount) {
299 const message = i18n.tc(
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700300 'pageLocalUserManagement.toast.errorBatchDisable',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800301 errorCount
302 );
303 toastMessages.push({ type: 'error', message });
304 }
305
306 return toastMessages;
307 })
308 );
Yoshie Muranaka1b1c1002020-02-20 10:18:36 -0800309 },
310 async saveAccountSettings(
311 { dispatch },
312 { lockoutThreshold, lockoutDuration }
313 ) {
314 const data = {};
315 if (lockoutThreshold !== undefined) {
316 data.AccountLockoutThreshold = lockoutThreshold;
317 }
318 if (lockoutDuration !== undefined) {
319 data.AccountLockoutDuration = lockoutDuration;
320 }
321
322 return await api
323 .patch('/redfish/v1/AccountService', data)
324 //GET new settings to update view
325 .then(() => dispatch('getAccountSettings'))
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -0800326 .then(() => i18n.t('pageLocalUserManagement.toast.successSaveSettings'))
Yoshie Muranaka1b1c1002020-02-20 10:18:36 -0800327 .catch(error => {
328 console.log(error);
329 const message = i18n.t(
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -0800330 'pageLocalUserManagement.toast.errorSaveSettings'
Yoshie Muranaka1b1c1002020-02-20 10:18:36 -0800331 );
332 throw new Error(message);
333 });
Yoshie Muranaka35080ac2020-01-17 15:38:57 -0600334 }
335 }
336};
337
338export default LocalUserManagementStore;