blob: e9de3cc1ee6c707562a85c73d92768b6bf514c19 [file] [log] [blame]
Yoshie Muranaka224d6ad2020-05-21 09:10:52 -07001import api, { getResponseCount } from '@/store/api';
2import i18n from '@/i18n';
Yoshie Muranaka35080ac2020-01-17 15:38:57 -06003
4const LocalUserManagementStore = {
5 namespaced: true,
6 state: {
Yoshie Muranaka52b02232020-02-20 08:00:45 -08007 allUsers: [],
Yoshie Muranaka038a9da2020-04-17 11:22:56 -07008 accountRoles: [],
Yoshie Muranaka52b02232020-02-20 08:00:45 -08009 accountLockoutDuration: null,
10 accountLockoutThreshold: null,
11 accountMinPasswordLength: null,
12 accountMaxPasswordLength: null
Yoshie Muranaka35080ac2020-01-17 15:38:57 -060013 },
14 getters: {
15 allUsers(state) {
16 return state.allUsers;
Yoshie Muranaka52b02232020-02-20 08:00:45 -080017 },
Yoshie Muranaka038a9da2020-04-17 11:22:56 -070018 accountRoles(state) {
19 return state.accountRoles;
20 },
Yoshie Muranaka52b02232020-02-20 08:00:45 -080021 accountSettings(state) {
22 return {
23 lockoutDuration: state.accountLockoutDuration,
24 lockoutThreshold: state.accountLockoutThreshold
25 };
26 },
27 accountPasswordRequirements(state) {
28 return {
29 minLength: state.accountMinPasswordLength,
30 maxLength: state.accountMaxPasswordLength
31 };
Yoshie Muranaka35080ac2020-01-17 15:38:57 -060032 }
33 },
34 mutations: {
35 setUsers(state, allUsers) {
36 state.allUsers = allUsers;
Yoshie Muranaka52b02232020-02-20 08:00:45 -080037 },
Yoshie Muranaka038a9da2020-04-17 11:22:56 -070038 setAccountRoles(state, accountRoles) {
39 state.accountRoles = accountRoles;
40 },
Yoshie Muranaka52b02232020-02-20 08:00:45 -080041 setLockoutDuration(state, lockoutDuration) {
42 state.accountLockoutDuration = lockoutDuration;
43 },
44 setLockoutThreshold(state, lockoutThreshold) {
45 state.accountLockoutThreshold = lockoutThreshold;
46 },
47 setAccountMinPasswordLength(state, minPasswordLength) {
48 state.accountMinPasswordLength = minPasswordLength;
49 },
50 setAccountMaxPasswordLength(state, maxPasswordLength) {
51 state.accountMaxPasswordLength = maxPasswordLength;
Yoshie Muranaka35080ac2020-01-17 15:38:57 -060052 }
53 },
54 actions: {
Yoshie Muranaka346be2a2020-04-28 11:12:14 -070055 async getUsers({ commit }) {
56 return await api
Derick Montaguefded0d12019-12-11 06:16:40 -060057 .get('/redfish/v1/AccountService/Accounts')
58 .then(response => response.data.Members.map(user => user['@odata.id']))
Yoshie Muranaka463a5702019-12-04 09:09:36 -080059 .then(userIds => api.all(userIds.map(user => api.get(user))))
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080060 .then(users => {
61 const userData = users.map(user => user.data);
Derick Montaguefded0d12019-12-11 06:16:40 -060062 commit('setUsers', userData);
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080063 })
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -080064 .catch(error => {
65 console.log(error);
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -070066 const message = i18n.t(
67 'pageLocalUserManagement.toast.errorLoadUsers'
68 );
69 throw new Error(message);
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -080070 });
Yoshie Muranaka463a5702019-12-04 09:09:36 -080071 },
Yoshie Muranaka52b02232020-02-20 08:00:45 -080072 getAccountSettings({ commit }) {
73 api
74 .get('/redfish/v1/AccountService')
75 .then(({ data }) => {
76 commit('setLockoutDuration', data.AccountLockoutDuration);
77 commit('setLockoutThreshold', data.AccountLockoutThreshold);
78 commit('setAccountMinPasswordLength', data.MinPasswordLength);
79 commit('setAccountMaxPasswordLength', data.MaxPasswordLength);
80 })
81 .catch(error => {
82 console.log(error);
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -070083 const message = i18n.t(
84 'pageLocalUserManagement.toast.errorLoadAccountSettings'
85 );
86 throw new Error(message);
Yoshie Muranaka52b02232020-02-20 08:00:45 -080087 });
88 },
Yoshie Muranaka038a9da2020-04-17 11:22:56 -070089 getAccountRoles({ commit }) {
90 api
91 .get('/redfish/v1/AccountService/Roles')
92 .then(({ data: { Members = [] } = {} }) => {
93 const roles = Members.map(role => {
94 return role['@odata.id'].split('/').pop();
95 });
96 commit('setAccountRoles', roles);
97 })
98 .catch(error => console.log(error));
99 },
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800100 async createUser({ dispatch }, { username, password, privilege, status }) {
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800101 const data = {
102 UserName: username,
103 Password: password,
104 RoleId: privilege,
105 Enabled: status
106 };
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800107 return await api
Derick Montaguefded0d12019-12-11 06:16:40 -0600108 .post('/redfish/v1/AccountService/Accounts', data)
109 .then(() => dispatch('getUsers'))
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700110 .then(() =>
111 i18n.t('pageLocalUserManagement.toast.successCreateUser', {
112 username
113 })
114 )
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800115 .catch(error => {
116 console.log(error);
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700117 const message = i18n.t(
118 'pageLocalUserManagement.toast.errorCreateUser',
119 { username }
120 );
121 throw new Error(message);
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800122 });
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800123 },
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800124 async updateUser(
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800125 { dispatch },
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700126 { originalUsername, username, password, privilege, status, locked }
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800127 ) {
128 const data = {};
129 if (username) data.UserName = username;
130 if (password) data.Password = password;
131 if (privilege) data.RoleId = privilege;
132 if (status !== undefined) data.Enabled = status;
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700133 if (locked !== undefined) data.Locked = locked;
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800134 return await api
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800135 .patch(`/redfish/v1/AccountService/Accounts/${originalUsername}`, data)
Derick Montaguefded0d12019-12-11 06:16:40 -0600136 .then(() => dispatch('getUsers'))
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700137 .then(() =>
138 i18n.t('pageLocalUserManagement.toast.successUpdateUser', {
139 username: originalUsername
140 })
141 )
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800142 .catch(error => {
143 console.log(error);
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700144 const message = i18n.t(
145 'pageLocalUserManagement.toast.errorUpdateUser',
146 { username: originalUsername }
147 );
148 throw new Error(message);
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800149 });
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800150 },
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800151 async deleteUser({ dispatch }, username) {
152 return await api
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800153 .delete(`/redfish/v1/AccountService/Accounts/${username}`)
Derick Montaguefded0d12019-12-11 06:16:40 -0600154 .then(() => dispatch('getUsers'))
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700155 .then(() =>
156 i18n.t('pageLocalUserManagement.toast.successDeleteUser', {
157 username
158 })
159 )
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800160 .catch(error => {
161 console.log(error);
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700162 const message = i18n.t(
163 'pageLocalUserManagement.toast.errorDeleteUser',
164 { username }
165 );
166 throw new Error(message);
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800167 });
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800168 },
169 async deleteUsers({ dispatch }, users) {
170 const promises = users.map(({ username }) => {
171 return api
172 .delete(`/redfish/v1/AccountService/Accounts/${username}`)
173 .catch(error => {
174 console.log(error);
175 return error;
176 });
177 });
178 return await api
179 .all(promises)
180 .then(response => {
181 dispatch('getUsers');
182 return response;
183 })
184 .then(
185 api.spread((...responses) => {
186 const { successCount, errorCount } = getResponseCount(responses);
187 let toastMessages = [];
188
189 if (successCount) {
190 const message = i18n.tc(
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700191 'pageLocalUserManagement.toast.successBatchDelete',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800192 successCount
193 );
194 toastMessages.push({ type: 'success', message });
195 }
196
197 if (errorCount) {
198 const message = i18n.tc(
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700199 'pageLocalUserManagement.toast.errorBatchDelete',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800200 errorCount
201 );
202 toastMessages.push({ type: 'error', message });
203 }
204
205 return toastMessages;
206 })
207 );
208 },
209 async enableUsers({ dispatch }, users) {
210 const data = {
211 Enabled: true
212 };
213 const promises = users.map(({ username }) => {
214 return api
215 .patch(`/redfish/v1/AccountService/Accounts/${username}`, data)
216 .catch(error => {
217 console.log(error);
218 return error;
219 });
220 });
221 return await api
222 .all(promises)
223 .then(response => {
224 dispatch('getUsers');
225 return response;
226 })
227 .then(
228 api.spread((...responses) => {
229 const { successCount, errorCount } = getResponseCount(responses);
230 let toastMessages = [];
231
232 if (successCount) {
233 const message = i18n.tc(
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700234 'pageLocalUserManagement.toast.successBatchEnable',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800235 successCount
236 );
237 toastMessages.push({ type: 'success', message });
238 }
239
240 if (errorCount) {
241 const message = i18n.tc(
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700242 'pageLocalUserManagement.toast.errorBatchEnable',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800243 errorCount
244 );
245 toastMessages.push({ type: 'error', message });
246 }
247
248 return toastMessages;
249 })
250 );
251 },
252 async disableUsers({ dispatch }, users) {
253 const data = {
254 Enabled: false
255 };
256 const promises = users.map(({ username }) => {
257 return api
258 .patch(`/redfish/v1/AccountService/Accounts/${username}`, data)
259 .catch(error => {
260 console.log(error);
261 return error;
262 });
263 });
264 return await api
265 .all(promises)
266 .then(response => {
267 dispatch('getUsers');
268 return response;
269 })
270 .then(
271 api.spread((...responses) => {
272 const { successCount, errorCount } = getResponseCount(responses);
273 let toastMessages = [];
274
275 if (successCount) {
276 const message = i18n.tc(
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700277 'pageLocalUserManagement.toast.successBatchDisable',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800278 successCount
279 );
280 toastMessages.push({ type: 'success', message });
281 }
282
283 if (errorCount) {
284 const message = i18n.tc(
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700285 'pageLocalUserManagement.toast.errorBatchDisable',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800286 errorCount
287 );
288 toastMessages.push({ type: 'error', message });
289 }
290
291 return toastMessages;
292 })
293 );
Yoshie Muranaka1b1c1002020-02-20 10:18:36 -0800294 },
295 async saveAccountSettings(
296 { dispatch },
297 { lockoutThreshold, lockoutDuration }
298 ) {
299 const data = {};
300 if (lockoutThreshold !== undefined) {
301 data.AccountLockoutThreshold = lockoutThreshold;
302 }
303 if (lockoutDuration !== undefined) {
304 data.AccountLockoutDuration = lockoutDuration;
305 }
306
307 return await api
308 .patch('/redfish/v1/AccountService', data)
309 //GET new settings to update view
310 .then(() => dispatch('getAccountSettings'))
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -0800311 .then(() => i18n.t('pageLocalUserManagement.toast.successSaveSettings'))
Yoshie Muranaka1b1c1002020-02-20 10:18:36 -0800312 .catch(error => {
313 console.log(error);
314 const message = i18n.t(
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -0800315 'pageLocalUserManagement.toast.errorSaveSettings'
Yoshie Muranaka1b1c1002020-02-20 10:18:36 -0800316 );
317 throw new Error(message);
318 });
Yoshie Muranaka35080ac2020-01-17 15:38:57 -0600319 }
320 }
321};
322
323export default LocalUserManagementStore;