blob: 50361580a133fb1e0aeb60a04795a11eb5286353 [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
MichalX Szopinski5905f962021-06-07 15:46:59 +02004const getServerErrorMessages = function (error) {
5 let errorData = error.response.data.error
6 ? error.response.data.error
7 : error.response.data;
8 if (typeof errorData == 'string') {
9 return [];
10 }
11 return Object.values(errorData)
12 .reduce((a, b) => a.concat(b))
13 .filter((info) => info.Message)
14 .map((info) => info.Message);
15};
16
Sandeepa Singhb4406162021-07-26 15:05:39 +053017const UserManagementStore = {
Yoshie Muranaka35080ac2020-01-17 15:38:57 -060018 namespaced: true,
19 state: {
Yoshie Muranaka52b02232020-02-20 08:00:45 -080020 allUsers: [],
Yoshie Muranaka038a9da2020-04-17 11:22:56 -070021 accountRoles: [],
Yoshie Muranaka52b02232020-02-20 08:00:45 -080022 accountLockoutDuration: null,
23 accountLockoutThreshold: null,
24 accountMinPasswordLength: null,
Derick Montague602e98a2020-10-21 16:20:00 -050025 accountMaxPasswordLength: null,
Yoshie Muranaka35080ac2020-01-17 15:38:57 -060026 },
27 getters: {
28 allUsers(state) {
29 return state.allUsers;
Yoshie Muranaka52b02232020-02-20 08:00:45 -080030 },
Yoshie Muranaka038a9da2020-04-17 11:22:56 -070031 accountRoles(state) {
32 return state.accountRoles;
33 },
Yoshie Muranaka52b02232020-02-20 08:00:45 -080034 accountSettings(state) {
35 return {
36 lockoutDuration: state.accountLockoutDuration,
Derick Montague602e98a2020-10-21 16:20:00 -050037 lockoutThreshold: state.accountLockoutThreshold,
Yoshie Muranaka52b02232020-02-20 08:00:45 -080038 };
39 },
40 accountPasswordRequirements(state) {
41 return {
42 minLength: state.accountMinPasswordLength,
Derick Montague602e98a2020-10-21 16:20:00 -050043 maxLength: state.accountMaxPasswordLength,
Yoshie Muranaka52b02232020-02-20 08:00:45 -080044 };
Derick Montague602e98a2020-10-21 16:20:00 -050045 },
Yoshie Muranaka35080ac2020-01-17 15:38:57 -060046 },
47 mutations: {
48 setUsers(state, allUsers) {
49 state.allUsers = allUsers;
Yoshie Muranaka52b02232020-02-20 08:00:45 -080050 },
Yoshie Muranaka038a9da2020-04-17 11:22:56 -070051 setAccountRoles(state, accountRoles) {
52 state.accountRoles = accountRoles;
53 },
Yoshie Muranaka52b02232020-02-20 08:00:45 -080054 setLockoutDuration(state, lockoutDuration) {
55 state.accountLockoutDuration = lockoutDuration;
56 },
57 setLockoutThreshold(state, lockoutThreshold) {
58 state.accountLockoutThreshold = lockoutThreshold;
59 },
60 setAccountMinPasswordLength(state, minPasswordLength) {
61 state.accountMinPasswordLength = minPasswordLength;
62 },
63 setAccountMaxPasswordLength(state, maxPasswordLength) {
64 state.accountMaxPasswordLength = maxPasswordLength;
Derick Montague602e98a2020-10-21 16:20:00 -050065 },
Yoshie Muranaka35080ac2020-01-17 15:38:57 -060066 },
67 actions: {
Yoshie Muranaka346be2a2020-04-28 11:12:14 -070068 async getUsers({ commit }) {
69 return await api
Derick Montaguefded0d12019-12-11 06:16:40 -060070 .get('/redfish/v1/AccountService/Accounts')
Derick Montague602e98a2020-10-21 16:20:00 -050071 .then((response) =>
Ed Tanous81323992024-02-27 11:26:24 -080072 response.data.Members.map((user) => user['@odata.id']),
Derick Montague602e98a2020-10-21 16:20:00 -050073 )
74 .then((userIds) => api.all(userIds.map((user) => api.get(user))))
75 .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 })
Derick Montague602e98a2020-10-21 16:20:00 -050079 .catch((error) => {
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -080080 console.log(error);
Sandeepa Singhb4406162021-07-26 15:05:39 +053081 const message = i18n.t('pageUserManagement.toast.errorLoadUsers');
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -070082 throw new Error(message);
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -080083 });
Yoshie Muranaka463a5702019-12-04 09:09:36 -080084 },
Yoshie Muranaka52b02232020-02-20 08:00:45 -080085 getAccountSettings({ commit }) {
86 api
87 .get('/redfish/v1/AccountService')
88 .then(({ data }) => {
89 commit('setLockoutDuration', data.AccountLockoutDuration);
90 commit('setLockoutThreshold', data.AccountLockoutThreshold);
91 commit('setAccountMinPasswordLength', data.MinPasswordLength);
92 commit('setAccountMaxPasswordLength', data.MaxPasswordLength);
93 })
Derick Montague602e98a2020-10-21 16:20:00 -050094 .catch((error) => {
Yoshie Muranaka52b02232020-02-20 08:00:45 -080095 console.log(error);
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -070096 const message = i18n.t(
Ed Tanous81323992024-02-27 11:26:24 -080097 'pageUserManagement.toast.errorLoadAccountSettings',
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -070098 );
99 throw new Error(message);
Yoshie Muranaka52b02232020-02-20 08:00:45 -0800100 });
101 },
Yoshie Muranaka038a9da2020-04-17 11:22:56 -0700102 getAccountRoles({ commit }) {
103 api
104 .get('/redfish/v1/AccountService/Roles')
105 .then(({ data: { Members = [] } = {} }) => {
Derick Montague602e98a2020-10-21 16:20:00 -0500106 const roles = Members.map((role) => {
Yoshie Muranaka038a9da2020-04-17 11:22:56 -0700107 return role['@odata.id'].split('/').pop();
108 });
109 commit('setAccountRoles', roles);
110 })
Derick Montague602e98a2020-10-21 16:20:00 -0500111 .catch((error) => console.log(error));
Yoshie Muranaka038a9da2020-04-17 11:22:56 -0700112 },
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800113 async createUser({ dispatch }, { username, password, privilege, status }) {
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800114 const data = {
115 UserName: username,
116 Password: password,
117 RoleId: privilege,
Derick Montague602e98a2020-10-21 16:20:00 -0500118 Enabled: status,
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800119 };
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800120 return await api
Derick Montaguefded0d12019-12-11 06:16:40 -0600121 .post('/redfish/v1/AccountService/Accounts', data)
122 .then(() => dispatch('getUsers'))
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700123 .then(() =>
Sandeepa Singhb4406162021-07-26 15:05:39 +0530124 i18n.t('pageUserManagement.toast.successCreateUser', {
Derick Montague602e98a2020-10-21 16:20:00 -0500125 username,
Ed Tanous81323992024-02-27 11:26:24 -0800126 }),
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700127 )
Derick Montague602e98a2020-10-21 16:20:00 -0500128 .catch((error) => {
MichalX Szopinski5905f962021-06-07 15:46:59 +0200129 let serverMessages = getServerErrorMessages(error);
130 let message =
131 serverMessages.length > 0
132 ? serverMessages.join(' ')
133 : i18n.t('pageUserManagement.toast.errorCreateUser', {
134 username: username,
135 });
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700136 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 },
Ed Tanous81323992024-02-27 11:26:24 -0800141 { 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(() =>
Sandeepa Singhb4406162021-07-26 15:05:39 +0530153 i18n.t('pageUserManagement.toast.successUpdateUser', {
Derick Montague602e98a2020-10-21 16:20:00 -0500154 username: originalUsername,
Ed Tanous81323992024-02-27 11:26:24 -0800155 }),
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700156 )
Derick Montague602e98a2020-10-21 16:20:00 -0500157 .catch((error) => {
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800158 console.log(error);
MichalX Szopinski5905f962021-06-07 15:46:59 +0200159 const serverMessages = getServerErrorMessages(error);
160 const message =
161 serverMessages.length > 0
162 ? serverMessages.join(' ')
163 : i18n.t('pageUserManagement.toast.errorUpdateUser', {
164 username: originalUsername,
165 });
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700166 throw new Error(message);
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800167 });
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800168 },
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800169 async deleteUser({ dispatch }, username) {
170 return await api
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800171 .delete(`/redfish/v1/AccountService/Accounts/${username}`)
Derick Montaguefded0d12019-12-11 06:16:40 -0600172 .then(() => dispatch('getUsers'))
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700173 .then(() =>
Sandeepa Singhb4406162021-07-26 15:05:39 +0530174 i18n.t('pageUserManagement.toast.successDeleteUser', {
Derick Montague602e98a2020-10-21 16:20:00 -0500175 username,
Ed Tanous81323992024-02-27 11:26:24 -0800176 }),
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700177 )
Derick Montague602e98a2020-10-21 16:20:00 -0500178 .catch((error) => {
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800179 console.log(error);
Sandeepa Singhb4406162021-07-26 15:05:39 +0530180 const message = i18n.t('pageUserManagement.toast.errorDeleteUser', {
181 username,
182 });
Yoshie Muranaka8fc53ad2020-05-04 10:40:41 -0700183 throw new Error(message);
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800184 });
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800185 },
186 async deleteUsers({ dispatch }, users) {
187 const promises = users.map(({ username }) => {
188 return api
189 .delete(`/redfish/v1/AccountService/Accounts/${username}`)
Derick Montague602e98a2020-10-21 16:20:00 -0500190 .catch((error) => {
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800191 console.log(error);
192 return error;
193 });
194 });
195 return await api
196 .all(promises)
Derick Montague602e98a2020-10-21 16:20:00 -0500197 .then((response) => {
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800198 dispatch('getUsers');
199 return response;
200 })
201 .then(
202 api.spread((...responses) => {
203 const { successCount, errorCount } = getResponseCount(responses);
204 let toastMessages = [];
205
206 if (successCount) {
207 const message = i18n.tc(
Sandeepa Singhb4406162021-07-26 15:05:39 +0530208 'pageUserManagement.toast.successBatchDelete',
Ed Tanous81323992024-02-27 11:26:24 -0800209 successCount,
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800210 );
211 toastMessages.push({ type: 'success', message });
212 }
213
214 if (errorCount) {
215 const message = i18n.tc(
Sandeepa Singhb4406162021-07-26 15:05:39 +0530216 'pageUserManagement.toast.errorBatchDelete',
Ed Tanous81323992024-02-27 11:26:24 -0800217 errorCount,
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800218 );
219 toastMessages.push({ type: 'error', message });
220 }
221
222 return toastMessages;
Ed Tanous81323992024-02-27 11:26:24 -0800223 }),
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800224 );
225 },
226 async enableUsers({ dispatch }, users) {
227 const data = {
Derick Montague602e98a2020-10-21 16:20:00 -0500228 Enabled: true,
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800229 };
230 const promises = users.map(({ username }) => {
231 return api
232 .patch(`/redfish/v1/AccountService/Accounts/${username}`, data)
Derick Montague602e98a2020-10-21 16:20:00 -0500233 .catch((error) => {
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800234 console.log(error);
235 return error;
236 });
237 });
238 return await api
239 .all(promises)
Derick Montague602e98a2020-10-21 16:20:00 -0500240 .then((response) => {
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800241 dispatch('getUsers');
242 return response;
243 })
244 .then(
245 api.spread((...responses) => {
246 const { successCount, errorCount } = getResponseCount(responses);
247 let toastMessages = [];
248
249 if (successCount) {
250 const message = i18n.tc(
Sandeepa Singhb4406162021-07-26 15:05:39 +0530251 'pageUserManagement.toast.successBatchEnable',
Ed Tanous81323992024-02-27 11:26:24 -0800252 successCount,
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800253 );
254 toastMessages.push({ type: 'success', message });
255 }
256
257 if (errorCount) {
258 const message = i18n.tc(
Sandeepa Singhb4406162021-07-26 15:05:39 +0530259 'pageUserManagement.toast.errorBatchEnable',
Ed Tanous81323992024-02-27 11:26:24 -0800260 errorCount,
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800261 );
262 toastMessages.push({ type: 'error', message });
263 }
264
265 return toastMessages;
Ed Tanous81323992024-02-27 11:26:24 -0800266 }),
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800267 );
268 },
269 async disableUsers({ dispatch }, users) {
270 const data = {
Derick Montague602e98a2020-10-21 16:20:00 -0500271 Enabled: false,
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800272 };
273 const promises = users.map(({ username }) => {
274 return api
275 .patch(`/redfish/v1/AccountService/Accounts/${username}`, data)
Derick Montague602e98a2020-10-21 16:20:00 -0500276 .catch((error) => {
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800277 console.log(error);
278 return error;
279 });
280 });
281 return await api
282 .all(promises)
Derick Montague602e98a2020-10-21 16:20:00 -0500283 .then((response) => {
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800284 dispatch('getUsers');
285 return response;
286 })
287 .then(
288 api.spread((...responses) => {
289 const { successCount, errorCount } = getResponseCount(responses);
290 let toastMessages = [];
291
292 if (successCount) {
293 const message = i18n.tc(
Sandeepa Singhb4406162021-07-26 15:05:39 +0530294 'pageUserManagement.toast.successBatchDisable',
Ed Tanous81323992024-02-27 11:26:24 -0800295 successCount,
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800296 );
297 toastMessages.push({ type: 'success', message });
298 }
299
300 if (errorCount) {
301 const message = i18n.tc(
Sandeepa Singhb4406162021-07-26 15:05:39 +0530302 'pageUserManagement.toast.errorBatchDisable',
Ed Tanous81323992024-02-27 11:26:24 -0800303 errorCount,
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800304 );
305 toastMessages.push({ type: 'error', message });
306 }
307
308 return toastMessages;
Ed Tanous81323992024-02-27 11:26:24 -0800309 }),
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800310 );
Yoshie Muranaka1b1c1002020-02-20 10:18:36 -0800311 },
312 async saveAccountSettings(
313 { dispatch },
Ed Tanous81323992024-02-27 11:26:24 -0800314 { lockoutThreshold, lockoutDuration },
Yoshie Muranaka1b1c1002020-02-20 10:18:36 -0800315 ) {
316 const data = {};
317 if (lockoutThreshold !== undefined) {
318 data.AccountLockoutThreshold = lockoutThreshold;
319 }
320 if (lockoutDuration !== undefined) {
321 data.AccountLockoutDuration = lockoutDuration;
322 }
323
324 return await api
325 .patch('/redfish/v1/AccountService', data)
326 //GET new settings to update view
327 .then(() => dispatch('getAccountSettings'))
Sandeepa Singhb4406162021-07-26 15:05:39 +0530328 .then(() => i18n.t('pageUserManagement.toast.successSaveSettings'))
Derick Montague602e98a2020-10-21 16:20:00 -0500329 .catch((error) => {
Yoshie Muranaka1b1c1002020-02-20 10:18:36 -0800330 console.log(error);
Sandeepa Singhb4406162021-07-26 15:05:39 +0530331 const message = i18n.t('pageUserManagement.toast.errorSaveSettings');
Yoshie Muranaka1b1c1002020-02-20 10:18:36 -0800332 throw new Error(message);
333 });
Derick Montague602e98a2020-10-21 16:20:00 -0500334 },
335 },
Yoshie Muranaka35080ac2020-01-17 15:38:57 -0600336};
337
Sandeepa Singhb4406162021-07-26 15:05:39 +0530338export default UserManagementStore;