blob: 6bc6ec5d528c159da50e2560369ea688653628e5 [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,
Derick Montague602e98a2020-10-21 16:20:00 -050012 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,
Derick Montague602e98a2020-10-21 16:20:00 -050024 lockoutThreshold: state.accountLockoutThreshold,
Yoshie Muranaka52b02232020-02-20 08:00:45 -080025 };
26 },
27 accountPasswordRequirements(state) {
28 return {
29 minLength: state.accountMinPasswordLength,
Derick Montague602e98a2020-10-21 16:20:00 -050030 maxLength: state.accountMaxPasswordLength,
Yoshie Muranaka52b02232020-02-20 08:00:45 -080031 };
Derick Montague602e98a2020-10-21 16:20:00 -050032 },
Yoshie Muranaka35080ac2020-01-17 15:38:57 -060033 },
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;
Derick Montague602e98a2020-10-21 16:20:00 -050052 },
Yoshie Muranaka35080ac2020-01-17 15:38:57 -060053 },
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')
Derick Montague602e98a2020-10-21 16:20:00 -050058 .then((response) =>
59 response.data.Members.map((user) => user['@odata.id'])
60 )
61 .then((userIds) => api.all(userIds.map((user) => api.get(user))))
62 .then((users) => {
63 const userData = users.map((user) => user.data);
Derick Montaguefded0d12019-12-11 06:16:40 -060064 commit('setUsers', userData);
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080065 })
Derick Montague602e98a2020-10-21 16:20:00 -050066 .catch((error) => {
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -080067 console.log(error);
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -070068 const message = i18n.t(
69 'pageLocalUserManagement.toast.errorLoadUsers'
70 );
71 throw new Error(message);
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -080072 });
Yoshie Muranaka463a5702019-12-04 09:09:36 -080073 },
Yoshie Muranaka52b02232020-02-20 08:00:45 -080074 getAccountSettings({ commit }) {
75 api
76 .get('/redfish/v1/AccountService')
77 .then(({ data }) => {
78 commit('setLockoutDuration', data.AccountLockoutDuration);
79 commit('setLockoutThreshold', data.AccountLockoutThreshold);
80 commit('setAccountMinPasswordLength', data.MinPasswordLength);
81 commit('setAccountMaxPasswordLength', data.MaxPasswordLength);
82 })
Derick Montague602e98a2020-10-21 16:20:00 -050083 .catch((error) => {
Yoshie Muranaka52b02232020-02-20 08:00:45 -080084 console.log(error);
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -070085 const message = i18n.t(
86 'pageLocalUserManagement.toast.errorLoadAccountSettings'
87 );
88 throw new Error(message);
Yoshie Muranaka52b02232020-02-20 08:00:45 -080089 });
90 },
Yoshie Muranaka038a9da2020-04-17 11:22:56 -070091 getAccountRoles({ commit }) {
92 api
93 .get('/redfish/v1/AccountService/Roles')
94 .then(({ data: { Members = [] } = {} }) => {
Derick Montague602e98a2020-10-21 16:20:00 -050095 const roles = Members.map((role) => {
Yoshie Muranaka038a9da2020-04-17 11:22:56 -070096 return role['@odata.id'].split('/').pop();
97 });
98 commit('setAccountRoles', roles);
99 })
Derick Montague602e98a2020-10-21 16:20:00 -0500100 .catch((error) => console.log(error));
Yoshie Muranaka038a9da2020-04-17 11:22:56 -0700101 },
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800102 async createUser({ dispatch }, { username, password, privilege, status }) {
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800103 const data = {
104 UserName: username,
105 Password: password,
106 RoleId: privilege,
Derick Montague602e98a2020-10-21 16:20:00 -0500107 Enabled: status,
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800108 };
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800109 return await api
Derick Montaguefded0d12019-12-11 06:16:40 -0600110 .post('/redfish/v1/AccountService/Accounts', data)
111 .then(() => dispatch('getUsers'))
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700112 .then(() =>
113 i18n.t('pageLocalUserManagement.toast.successCreateUser', {
Derick Montague602e98a2020-10-21 16:20:00 -0500114 username,
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700115 })
116 )
Derick Montague602e98a2020-10-21 16:20:00 -0500117 .catch((error) => {
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800118 console.log(error);
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700119 const message = i18n.t(
120 'pageLocalUserManagement.toast.errorCreateUser',
121 { username }
122 );
123 throw new Error(message);
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800124 });
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800125 },
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800126 async updateUser(
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800127 { dispatch },
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700128 { originalUsername, username, password, privilege, status, locked }
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800129 ) {
130 const data = {};
131 if (username) data.UserName = username;
132 if (password) data.Password = password;
133 if (privilege) data.RoleId = privilege;
134 if (status !== undefined) data.Enabled = status;
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700135 if (locked !== undefined) data.Locked = locked;
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800136 return await api
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800137 .patch(`/redfish/v1/AccountService/Accounts/${originalUsername}`, data)
Derick Montaguefded0d12019-12-11 06:16:40 -0600138 .then(() => dispatch('getUsers'))
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700139 .then(() =>
140 i18n.t('pageLocalUserManagement.toast.successUpdateUser', {
Derick Montague602e98a2020-10-21 16:20:00 -0500141 username: originalUsername,
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700142 })
143 )
Derick Montague602e98a2020-10-21 16:20:00 -0500144 .catch((error) => {
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800145 console.log(error);
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700146 const message = i18n.t(
147 'pageLocalUserManagement.toast.errorUpdateUser',
148 { username: originalUsername }
149 );
150 throw new Error(message);
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800151 });
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800152 },
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800153 async deleteUser({ dispatch }, username) {
154 return await api
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800155 .delete(`/redfish/v1/AccountService/Accounts/${username}`)
Derick Montaguefded0d12019-12-11 06:16:40 -0600156 .then(() => dispatch('getUsers'))
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700157 .then(() =>
158 i18n.t('pageLocalUserManagement.toast.successDeleteUser', {
Derick Montague602e98a2020-10-21 16:20:00 -0500159 username,
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700160 })
161 )
Derick Montague602e98a2020-10-21 16:20:00 -0500162 .catch((error) => {
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800163 console.log(error);
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700164 const message = i18n.t(
165 'pageLocalUserManagement.toast.errorDeleteUser',
166 { username }
167 );
168 throw new Error(message);
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800169 });
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800170 },
171 async deleteUsers({ dispatch }, users) {
172 const promises = users.map(({ username }) => {
173 return api
174 .delete(`/redfish/v1/AccountService/Accounts/${username}`)
Derick Montague602e98a2020-10-21 16:20:00 -0500175 .catch((error) => {
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800176 console.log(error);
177 return error;
178 });
179 });
180 return await api
181 .all(promises)
Derick Montague602e98a2020-10-21 16:20:00 -0500182 .then((response) => {
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800183 dispatch('getUsers');
184 return response;
185 })
186 .then(
187 api.spread((...responses) => {
188 const { successCount, errorCount } = getResponseCount(responses);
189 let toastMessages = [];
190
191 if (successCount) {
192 const message = i18n.tc(
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700193 'pageLocalUserManagement.toast.successBatchDelete',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800194 successCount
195 );
196 toastMessages.push({ type: 'success', message });
197 }
198
199 if (errorCount) {
200 const message = i18n.tc(
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700201 'pageLocalUserManagement.toast.errorBatchDelete',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800202 errorCount
203 );
204 toastMessages.push({ type: 'error', message });
205 }
206
207 return toastMessages;
208 })
209 );
210 },
211 async enableUsers({ dispatch }, users) {
212 const data = {
Derick Montague602e98a2020-10-21 16:20:00 -0500213 Enabled: true,
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800214 };
215 const promises = users.map(({ username }) => {
216 return api
217 .patch(`/redfish/v1/AccountService/Accounts/${username}`, data)
Derick Montague602e98a2020-10-21 16:20:00 -0500218 .catch((error) => {
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800219 console.log(error);
220 return error;
221 });
222 });
223 return await api
224 .all(promises)
Derick Montague602e98a2020-10-21 16:20:00 -0500225 .then((response) => {
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800226 dispatch('getUsers');
227 return response;
228 })
229 .then(
230 api.spread((...responses) => {
231 const { successCount, errorCount } = getResponseCount(responses);
232 let toastMessages = [];
233
234 if (successCount) {
235 const message = i18n.tc(
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700236 'pageLocalUserManagement.toast.successBatchEnable',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800237 successCount
238 );
239 toastMessages.push({ type: 'success', message });
240 }
241
242 if (errorCount) {
243 const message = i18n.tc(
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700244 'pageLocalUserManagement.toast.errorBatchEnable',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800245 errorCount
246 );
247 toastMessages.push({ type: 'error', message });
248 }
249
250 return toastMessages;
251 })
252 );
253 },
254 async disableUsers({ dispatch }, users) {
255 const data = {
Derick Montague602e98a2020-10-21 16:20:00 -0500256 Enabled: false,
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800257 };
258 const promises = users.map(({ username }) => {
259 return api
260 .patch(`/redfish/v1/AccountService/Accounts/${username}`, data)
Derick Montague602e98a2020-10-21 16:20:00 -0500261 .catch((error) => {
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800262 console.log(error);
263 return error;
264 });
265 });
266 return await api
267 .all(promises)
Derick Montague602e98a2020-10-21 16:20:00 -0500268 .then((response) => {
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800269 dispatch('getUsers');
270 return response;
271 })
272 .then(
273 api.spread((...responses) => {
274 const { successCount, errorCount } = getResponseCount(responses);
275 let toastMessages = [];
276
277 if (successCount) {
278 const message = i18n.tc(
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700279 'pageLocalUserManagement.toast.successBatchDisable',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800280 successCount
281 );
282 toastMessages.push({ type: 'success', message });
283 }
284
285 if (errorCount) {
286 const message = i18n.tc(
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700287 'pageLocalUserManagement.toast.errorBatchDisable',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800288 errorCount
289 );
290 toastMessages.push({ type: 'error', message });
291 }
292
293 return toastMessages;
294 })
295 );
Yoshie Muranaka1b1c1002020-02-20 10:18:36 -0800296 },
297 async saveAccountSettings(
298 { dispatch },
299 { lockoutThreshold, lockoutDuration }
300 ) {
301 const data = {};
302 if (lockoutThreshold !== undefined) {
303 data.AccountLockoutThreshold = lockoutThreshold;
304 }
305 if (lockoutDuration !== undefined) {
306 data.AccountLockoutDuration = lockoutDuration;
307 }
308
309 return await api
310 .patch('/redfish/v1/AccountService', data)
311 //GET new settings to update view
312 .then(() => dispatch('getAccountSettings'))
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -0800313 .then(() => i18n.t('pageLocalUserManagement.toast.successSaveSettings'))
Derick Montague602e98a2020-10-21 16:20:00 -0500314 .catch((error) => {
Yoshie Muranaka1b1c1002020-02-20 10:18:36 -0800315 console.log(error);
316 const message = i18n.t(
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -0800317 'pageLocalUserManagement.toast.errorSaveSettings'
Yoshie Muranaka1b1c1002020-02-20 10:18:36 -0800318 );
319 throw new Error(message);
320 });
Derick Montague602e98a2020-10-21 16:20:00 -0500321 },
322 },
Yoshie Muranaka35080ac2020-01-17 15:38:57 -0600323};
324
325export default LocalUserManagementStore;