blob: 78dbd43340e8eaf2990975a9b54ec334d96b6cdd [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) => {
Sivaprabu Ganesan28974472023-01-05 18:52:51 +0530116 let message = i18n.t('pageUserManagement.toast.errorCreateUser', {
Sandeepa Singhb4406162021-07-26 15:05:39 +0530117 username,
118 });
Sivaprabu Ganesan28974472023-01-05 18:52:51 +0530119 if (error.response && error.response.data) {
120 if (error.response.data['UserName@Message.ExtendedInfo']) {
121 let obj = error.response.data['UserName@Message.ExtendedInfo'];
122 for (var key in obj) {
123 if (obj[key].Message) {
124 let msg = obj[key].Message;
125 if (msg.indexOf('already exists') != -1) {
126 message = i18n.t(
127 'pageUserManagement.toast.errorAlreadyExistUser',
128 {
129 username,
130 }
131 );
132 }
133 }
134 }
135 }
136 }
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700137 throw new Error(message);
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800138 });
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800139 },
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800140 async updateUser(
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800141 { dispatch },
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700142 { originalUsername, username, password, privilege, status, locked }
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800143 ) {
144 const data = {};
145 if (username) data.UserName = username;
146 if (password) data.Password = password;
147 if (privilege) data.RoleId = privilege;
148 if (status !== undefined) data.Enabled = status;
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700149 if (locked !== undefined) data.Locked = locked;
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800150 return await api
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800151 .patch(`/redfish/v1/AccountService/Accounts/${originalUsername}`, data)
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.successUpdateUser', {
Derick Montague602e98a2020-10-21 16:20:00 -0500155 username: originalUsername,
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.errorUpdateUser', {
161 username: originalUsername,
162 });
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700163 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(() =>
Sandeepa Singhb4406162021-07-26 15:05:39 +0530171 i18n.t('pageUserManagement.toast.successDeleteUser', {
Derick Montague602e98a2020-10-21 16:20:00 -0500172 username,
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700173 })
174 )
Derick Montague602e98a2020-10-21 16:20:00 -0500175 .catch((error) => {
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800176 console.log(error);
Sandeepa Singhb4406162021-07-26 15:05:39 +0530177 const message = i18n.t('pageUserManagement.toast.errorDeleteUser', {
178 username,
179 });
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700180 throw new Error(message);
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800181 });
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800182 },
183 async deleteUsers({ dispatch }, users) {
184 const promises = users.map(({ username }) => {
185 return api
186 .delete(`/redfish/v1/AccountService/Accounts/${username}`)
Derick Montague602e98a2020-10-21 16:20:00 -0500187 .catch((error) => {
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800188 console.log(error);
189 return error;
190 });
191 });
192 return await api
193 .all(promises)
Derick Montague602e98a2020-10-21 16:20:00 -0500194 .then((response) => {
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800195 dispatch('getUsers');
196 return response;
197 })
198 .then(
199 api.spread((...responses) => {
200 const { successCount, errorCount } = getResponseCount(responses);
201 let toastMessages = [];
202
203 if (successCount) {
204 const message = i18n.tc(
Sandeepa Singhb4406162021-07-26 15:05:39 +0530205 'pageUserManagement.toast.successBatchDelete',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800206 successCount
207 );
208 toastMessages.push({ type: 'success', message });
209 }
210
211 if (errorCount) {
212 const message = i18n.tc(
Sandeepa Singhb4406162021-07-26 15:05:39 +0530213 'pageUserManagement.toast.errorBatchDelete',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800214 errorCount
215 );
216 toastMessages.push({ type: 'error', message });
217 }
218
219 return toastMessages;
220 })
221 );
222 },
223 async enableUsers({ dispatch }, users) {
224 const data = {
Derick Montague602e98a2020-10-21 16:20:00 -0500225 Enabled: true,
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800226 };
227 const promises = users.map(({ username }) => {
228 return api
229 .patch(`/redfish/v1/AccountService/Accounts/${username}`, data)
Derick Montague602e98a2020-10-21 16:20:00 -0500230 .catch((error) => {
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800231 console.log(error);
232 return error;
233 });
234 });
235 return await api
236 .all(promises)
Derick Montague602e98a2020-10-21 16:20:00 -0500237 .then((response) => {
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800238 dispatch('getUsers');
239 return response;
240 })
241 .then(
242 api.spread((...responses) => {
243 const { successCount, errorCount } = getResponseCount(responses);
244 let toastMessages = [];
245
246 if (successCount) {
247 const message = i18n.tc(
Sandeepa Singhb4406162021-07-26 15:05:39 +0530248 'pageUserManagement.toast.successBatchEnable',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800249 successCount
250 );
251 toastMessages.push({ type: 'success', message });
252 }
253
254 if (errorCount) {
255 const message = i18n.tc(
Sandeepa Singhb4406162021-07-26 15:05:39 +0530256 'pageUserManagement.toast.errorBatchEnable',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800257 errorCount
258 );
259 toastMessages.push({ type: 'error', message });
260 }
261
262 return toastMessages;
263 })
264 );
265 },
266 async disableUsers({ dispatch }, users) {
267 const data = {
Derick Montague602e98a2020-10-21 16:20:00 -0500268 Enabled: false,
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800269 };
270 const promises = users.map(({ username }) => {
271 return api
272 .patch(`/redfish/v1/AccountService/Accounts/${username}`, data)
Derick Montague602e98a2020-10-21 16:20:00 -0500273 .catch((error) => {
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800274 console.log(error);
275 return error;
276 });
277 });
278 return await api
279 .all(promises)
Derick Montague602e98a2020-10-21 16:20:00 -0500280 .then((response) => {
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800281 dispatch('getUsers');
282 return response;
283 })
284 .then(
285 api.spread((...responses) => {
286 const { successCount, errorCount } = getResponseCount(responses);
287 let toastMessages = [];
288
289 if (successCount) {
290 const message = i18n.tc(
Sandeepa Singhb4406162021-07-26 15:05:39 +0530291 'pageUserManagement.toast.successBatchDisable',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800292 successCount
293 );
294 toastMessages.push({ type: 'success', message });
295 }
296
297 if (errorCount) {
298 const message = i18n.tc(
Sandeepa Singhb4406162021-07-26 15:05:39 +0530299 'pageUserManagement.toast.errorBatchDisable',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800300 errorCount
301 );
302 toastMessages.push({ type: 'error', message });
303 }
304
305 return toastMessages;
306 })
307 );
Yoshie Muranaka1b1c1002020-02-20 10:18:36 -0800308 },
309 async saveAccountSettings(
310 { dispatch },
311 { lockoutThreshold, lockoutDuration }
312 ) {
313 const data = {};
314 if (lockoutThreshold !== undefined) {
315 data.AccountLockoutThreshold = lockoutThreshold;
316 }
317 if (lockoutDuration !== undefined) {
318 data.AccountLockoutDuration = lockoutDuration;
319 }
320
321 return await api
322 .patch('/redfish/v1/AccountService', data)
323 //GET new settings to update view
324 .then(() => dispatch('getAccountSettings'))
Sandeepa Singhb4406162021-07-26 15:05:39 +0530325 .then(() => i18n.t('pageUserManagement.toast.successSaveSettings'))
Derick Montague602e98a2020-10-21 16:20:00 -0500326 .catch((error) => {
Yoshie Muranaka1b1c1002020-02-20 10:18:36 -0800327 console.log(error);
Sandeepa Singhb4406162021-07-26 15:05:39 +0530328 const message = i18n.t('pageUserManagement.toast.errorSaveSettings');
Yoshie Muranaka1b1c1002020-02-20 10:18:36 -0800329 throw new Error(message);
330 });
Derick Montague602e98a2020-10-21 16:20:00 -0500331 },
332 },
Yoshie Muranaka35080ac2020-01-17 15:38:57 -0600333};
334
Sandeepa Singhb4406162021-07-26 15:05:39 +0530335export default UserManagementStore;