1import api, { getResponseCount } from '@/store/api';
2import i18n from '@/i18n';
3
4const UserManagementStore = {
5  namespaced: true,
6  state: {
7    allUsers: [],
8    accountRoles: [],
9    accountLockoutDuration: null,
10    accountLockoutThreshold: null,
11    accountMinPasswordLength: null,
12    accountMaxPasswordLength: null,
13  },
14  getters: {
15    allUsers(state) {
16      return state.allUsers;
17    },
18    accountRoles(state) {
19      return state.accountRoles;
20    },
21    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      };
32    },
33  },
34  mutations: {
35    setUsers(state, allUsers) {
36      state.allUsers = allUsers;
37    },
38    setAccountRoles(state, accountRoles) {
39      state.accountRoles = accountRoles;
40    },
41    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;
52    },
53  },
54  actions: {
55    async getUsers({ commit }) {
56      return await api
57        .get('/redfish/v1/AccountService/Accounts')
58        .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);
64          commit('setUsers', userData);
65        })
66        .catch((error) => {
67          console.log(error);
68          const message = i18n.t('pageUserManagement.toast.errorLoadUsers');
69          throw new Error(message);
70        });
71    },
72    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);
83          const message = i18n.t(
84            'pageUserManagement.toast.errorLoadAccountSettings'
85          );
86          throw new Error(message);
87        });
88    },
89    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    },
100    async createUser({ dispatch }, { username, password, privilege, status }) {
101      const data = {
102        UserName: username,
103        Password: password,
104        RoleId: privilege,
105        Enabled: status,
106      };
107      return await api
108        .post('/redfish/v1/AccountService/Accounts', data)
109        .then(() => dispatch('getUsers'))
110        .then(() =>
111          i18n.t('pageUserManagement.toast.successCreateUser', {
112            username,
113          })
114        )
115        .catch((error) => {
116          let message = i18n.t('pageUserManagement.toast.errorCreateUser', {
117            username,
118          });
119          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          }
137          throw new Error(message);
138        });
139    },
140    async updateUser(
141      { dispatch },
142      { originalUsername, username, password, privilege, status, locked }
143    ) {
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;
149      if (locked !== undefined) data.Locked = locked;
150      return await api
151        .patch(`/redfish/v1/AccountService/Accounts/${originalUsername}`, data)
152        .then(() => dispatch('getUsers'))
153        .then(() =>
154          i18n.t('pageUserManagement.toast.successUpdateUser', {
155            username: originalUsername,
156          })
157        )
158        .catch((error) => {
159          console.log(error);
160          const message = i18n.t('pageUserManagement.toast.errorUpdateUser', {
161            username: originalUsername,
162          });
163          throw new Error(message);
164        });
165    },
166    async deleteUser({ dispatch }, username) {
167      return await api
168        .delete(`/redfish/v1/AccountService/Accounts/${username}`)
169        .then(() => dispatch('getUsers'))
170        .then(() =>
171          i18n.t('pageUserManagement.toast.successDeleteUser', {
172            username,
173          })
174        )
175        .catch((error) => {
176          console.log(error);
177          const message = i18n.t('pageUserManagement.toast.errorDeleteUser', {
178            username,
179          });
180          throw new Error(message);
181        });
182    },
183    async deleteUsers({ dispatch }, users) {
184      const promises = users.map(({ username }) => {
185        return api
186          .delete(`/redfish/v1/AccountService/Accounts/${username}`)
187          .catch((error) => {
188            console.log(error);
189            return error;
190          });
191      });
192      return await api
193        .all(promises)
194        .then((response) => {
195          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(
205                'pageUserManagement.toast.successBatchDelete',
206                successCount
207              );
208              toastMessages.push({ type: 'success', message });
209            }
210
211            if (errorCount) {
212              const message = i18n.tc(
213                'pageUserManagement.toast.errorBatchDelete',
214                errorCount
215              );
216              toastMessages.push({ type: 'error', message });
217            }
218
219            return toastMessages;
220          })
221        );
222    },
223    async enableUsers({ dispatch }, users) {
224      const data = {
225        Enabled: true,
226      };
227      const promises = users.map(({ username }) => {
228        return api
229          .patch(`/redfish/v1/AccountService/Accounts/${username}`, data)
230          .catch((error) => {
231            console.log(error);
232            return error;
233          });
234      });
235      return await api
236        .all(promises)
237        .then((response) => {
238          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(
248                'pageUserManagement.toast.successBatchEnable',
249                successCount
250              );
251              toastMessages.push({ type: 'success', message });
252            }
253
254            if (errorCount) {
255              const message = i18n.tc(
256                'pageUserManagement.toast.errorBatchEnable',
257                errorCount
258              );
259              toastMessages.push({ type: 'error', message });
260            }
261
262            return toastMessages;
263          })
264        );
265    },
266    async disableUsers({ dispatch }, users) {
267      const data = {
268        Enabled: false,
269      };
270      const promises = users.map(({ username }) => {
271        return api
272          .patch(`/redfish/v1/AccountService/Accounts/${username}`, data)
273          .catch((error) => {
274            console.log(error);
275            return error;
276          });
277      });
278      return await api
279        .all(promises)
280        .then((response) => {
281          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(
291                'pageUserManagement.toast.successBatchDisable',
292                successCount
293              );
294              toastMessages.push({ type: 'success', message });
295            }
296
297            if (errorCount) {
298              const message = i18n.tc(
299                'pageUserManagement.toast.errorBatchDisable',
300                errorCount
301              );
302              toastMessages.push({ type: 'error', message });
303            }
304
305            return toastMessages;
306          })
307        );
308    },
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'))
325        .then(() => i18n.t('pageUserManagement.toast.successSaveSettings'))
326        .catch((error) => {
327          console.log(error);
328          const message = i18n.t('pageUserManagement.toast.errorSaveSettings');
329          throw new Error(message);
330        });
331    },
332  },
333};
334
335export default UserManagementStore;
336