blob: 362f3f64b403b973e5331300c3e550786b4512d7 [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
Sandeepa Singhb4406162021-07-26 15:05:39 +05304const UserManagementStore = {
Yoshie Muranaka35080ac2020-01-17 15:38:57 -06005 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);
Sandeepa Singhb4406162021-07-26 15:05:39 +053068 const message = i18n.t('pageUserManagement.toast.errorLoadUsers');
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -070069 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 })
Derick Montague602e98a2020-10-21 16:20:00 -050081 .catch((error) => {
Yoshie Muranaka52b02232020-02-20 08:00:45 -080082 console.log(error);
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -070083 const message = i18n.t(
Sandeepa Singhb4406162021-07-26 15:05:39 +053084 'pageUserManagement.toast.errorLoadAccountSettings'
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -070085 );
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 = [] } = {} }) => {
Derick Montague602e98a2020-10-21 16:20:00 -050093 const roles = Members.map((role) => {
Yoshie Muranaka038a9da2020-04-17 11:22:56 -070094 return role['@odata.id'].split('/').pop();
95 });
96 commit('setAccountRoles', roles);
97 })
Derick Montague602e98a2020-10-21 16:20:00 -050098 .catch((error) => console.log(error));
Yoshie Muranaka038a9da2020-04-17 11:22:56 -070099 },
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,
Derick Montague602e98a2020-10-21 16:20:00 -0500105 Enabled: status,
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800106 };
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(() =>
Sandeepa Singhb4406162021-07-26 15:05:39 +0530111 i18n.t('pageUserManagement.toast.successCreateUser', {
Derick Montague602e98a2020-10-21 16:20:00 -0500112 username,
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700113 })
114 )
Derick Montague602e98a2020-10-21 16:20:00 -0500115 .catch((error) => {
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800116 console.log(error);
Sandeepa Singhb4406162021-07-26 15:05:39 +0530117 const message = i18n.t('pageUserManagement.toast.errorCreateUser', {
118 username,
119 });
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700120 throw new Error(message);
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800121 });
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800122 },
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800123 async updateUser(
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800124 { dispatch },
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700125 { originalUsername, username, password, privilege, status, locked }
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800126 ) {
127 const data = {};
128 if (username) data.UserName = username;
129 if (password) data.Password = password;
130 if (privilege) data.RoleId = privilege;
131 if (status !== undefined) data.Enabled = status;
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700132 if (locked !== undefined) data.Locked = locked;
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800133 return await api
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800134 .patch(`/redfish/v1/AccountService/Accounts/${originalUsername}`, data)
Derick Montaguefded0d12019-12-11 06:16:40 -0600135 .then(() => dispatch('getUsers'))
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700136 .then(() =>
Sandeepa Singhb4406162021-07-26 15:05:39 +0530137 i18n.t('pageUserManagement.toast.successUpdateUser', {
Derick Montague602e98a2020-10-21 16:20:00 -0500138 username: originalUsername,
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700139 })
140 )
Derick Montague602e98a2020-10-21 16:20:00 -0500141 .catch((error) => {
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800142 console.log(error);
Sandeepa Singhb4406162021-07-26 15:05:39 +0530143 const message = i18n.t('pageUserManagement.toast.errorUpdateUser', {
144 username: originalUsername,
145 });
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700146 throw new Error(message);
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800147 });
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800148 },
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800149 async deleteUser({ dispatch }, username) {
150 return await api
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800151 .delete(`/redfish/v1/AccountService/Accounts/${username}`)
Derick Montaguefded0d12019-12-11 06:16:40 -0600152 .then(() => dispatch('getUsers'))
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700153 .then(() =>
Sandeepa Singhb4406162021-07-26 15:05:39 +0530154 i18n.t('pageUserManagement.toast.successDeleteUser', {
Derick Montague602e98a2020-10-21 16:20:00 -0500155 username,
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700156 })
157 )
Derick Montague602e98a2020-10-21 16:20:00 -0500158 .catch((error) => {
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800159 console.log(error);
Sandeepa Singhb4406162021-07-26 15:05:39 +0530160 const message = i18n.t('pageUserManagement.toast.errorDeleteUser', {
161 username,
162 });
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700163 throw new Error(message);
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800164 });
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800165 },
166 async deleteUsers({ dispatch }, users) {
167 const promises = users.map(({ username }) => {
168 return api
169 .delete(`/redfish/v1/AccountService/Accounts/${username}`)
Derick Montague602e98a2020-10-21 16:20:00 -0500170 .catch((error) => {
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800171 console.log(error);
172 return error;
173 });
174 });
175 return await api
176 .all(promises)
Derick Montague602e98a2020-10-21 16:20:00 -0500177 .then((response) => {
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800178 dispatch('getUsers');
179 return response;
180 })
181 .then(
182 api.spread((...responses) => {
183 const { successCount, errorCount } = getResponseCount(responses);
184 let toastMessages = [];
185
186 if (successCount) {
187 const message = i18n.tc(
Sandeepa Singhb4406162021-07-26 15:05:39 +0530188 'pageUserManagement.toast.successBatchDelete',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800189 successCount
190 );
191 toastMessages.push({ type: 'success', message });
192 }
193
194 if (errorCount) {
195 const message = i18n.tc(
Sandeepa Singhb4406162021-07-26 15:05:39 +0530196 'pageUserManagement.toast.errorBatchDelete',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800197 errorCount
198 );
199 toastMessages.push({ type: 'error', message });
200 }
201
202 return toastMessages;
203 })
204 );
205 },
206 async enableUsers({ dispatch }, users) {
207 const data = {
Derick Montague602e98a2020-10-21 16:20:00 -0500208 Enabled: true,
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800209 };
210 const promises = users.map(({ username }) => {
211 return api
212 .patch(`/redfish/v1/AccountService/Accounts/${username}`, data)
Derick Montague602e98a2020-10-21 16:20:00 -0500213 .catch((error) => {
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800214 console.log(error);
215 return error;
216 });
217 });
218 return await api
219 .all(promises)
Derick Montague602e98a2020-10-21 16:20:00 -0500220 .then((response) => {
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800221 dispatch('getUsers');
222 return response;
223 })
224 .then(
225 api.spread((...responses) => {
226 const { successCount, errorCount } = getResponseCount(responses);
227 let toastMessages = [];
228
229 if (successCount) {
230 const message = i18n.tc(
Sandeepa Singhb4406162021-07-26 15:05:39 +0530231 'pageUserManagement.toast.successBatchEnable',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800232 successCount
233 );
234 toastMessages.push({ type: 'success', message });
235 }
236
237 if (errorCount) {
238 const message = i18n.tc(
Sandeepa Singhb4406162021-07-26 15:05:39 +0530239 'pageUserManagement.toast.errorBatchEnable',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800240 errorCount
241 );
242 toastMessages.push({ type: 'error', message });
243 }
244
245 return toastMessages;
246 })
247 );
248 },
249 async disableUsers({ dispatch }, users) {
250 const data = {
Derick Montague602e98a2020-10-21 16:20:00 -0500251 Enabled: false,
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800252 };
253 const promises = users.map(({ username }) => {
254 return api
255 .patch(`/redfish/v1/AccountService/Accounts/${username}`, data)
Derick Montague602e98a2020-10-21 16:20:00 -0500256 .catch((error) => {
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800257 console.log(error);
258 return error;
259 });
260 });
261 return await api
262 .all(promises)
Derick Montague602e98a2020-10-21 16:20:00 -0500263 .then((response) => {
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800264 dispatch('getUsers');
265 return response;
266 })
267 .then(
268 api.spread((...responses) => {
269 const { successCount, errorCount } = getResponseCount(responses);
270 let toastMessages = [];
271
272 if (successCount) {
273 const message = i18n.tc(
Sandeepa Singhb4406162021-07-26 15:05:39 +0530274 'pageUserManagement.toast.successBatchDisable',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800275 successCount
276 );
277 toastMessages.push({ type: 'success', message });
278 }
279
280 if (errorCount) {
281 const message = i18n.tc(
Sandeepa Singhb4406162021-07-26 15:05:39 +0530282 'pageUserManagement.toast.errorBatchDisable',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800283 errorCount
284 );
285 toastMessages.push({ type: 'error', message });
286 }
287
288 return toastMessages;
289 })
290 );
Yoshie Muranaka1b1c1002020-02-20 10:18:36 -0800291 },
292 async saveAccountSettings(
293 { dispatch },
294 { lockoutThreshold, lockoutDuration }
295 ) {
296 const data = {};
297 if (lockoutThreshold !== undefined) {
298 data.AccountLockoutThreshold = lockoutThreshold;
299 }
300 if (lockoutDuration !== undefined) {
301 data.AccountLockoutDuration = lockoutDuration;
302 }
303
304 return await api
305 .patch('/redfish/v1/AccountService', data)
306 //GET new settings to update view
307 .then(() => dispatch('getAccountSettings'))
Sandeepa Singhb4406162021-07-26 15:05:39 +0530308 .then(() => i18n.t('pageUserManagement.toast.successSaveSettings'))
Derick Montague602e98a2020-10-21 16:20:00 -0500309 .catch((error) => {
Yoshie Muranaka1b1c1002020-02-20 10:18:36 -0800310 console.log(error);
Sandeepa Singhb4406162021-07-26 15:05:39 +0530311 const message = i18n.t('pageUserManagement.toast.errorSaveSettings');
Yoshie Muranaka1b1c1002020-02-20 10:18:36 -0800312 throw new Error(message);
313 });
Derick Montague602e98a2020-10-21 16:20:00 -0500314 },
315 },
Yoshie Muranaka35080ac2020-01-17 15:38:57 -0600316};
317
Sandeepa Singhb4406162021-07-26 15:05:39 +0530318export default UserManagementStore;