blob: c729d15fd6e530ffdede97e3403545a46e7e363f [file] [log] [blame]
Derick Montaguefded0d12019-12-11 06:16:40 -06001import api from '../../api';
Yoshie Muranaka183c2752020-02-12 11:30:49 -08002import i18n from '../../../i18n';
3
4const getResponseCount = responses => {
5 let successCount = 0;
6 let errorCount = 0;
7
8 responses.forEach(response => {
9 if (response instanceof Error) errorCount++;
10 else successCount++;
11 });
12
13 return {
14 successCount,
15 errorCount
16 };
17};
Yoshie Muranaka35080ac2020-01-17 15:38:57 -060018
19const LocalUserManagementStore = {
20 namespaced: true,
21 state: {
Yoshie Muranaka52b02232020-02-20 08:00:45 -080022 allUsers: [],
23 accountLockoutDuration: null,
24 accountLockoutThreshold: null,
25 accountMinPasswordLength: null,
26 accountMaxPasswordLength: null
Yoshie Muranaka35080ac2020-01-17 15:38:57 -060027 },
28 getters: {
29 allUsers(state) {
30 return state.allUsers;
Yoshie Muranaka52b02232020-02-20 08:00:45 -080031 },
32 accountSettings(state) {
33 return {
34 lockoutDuration: state.accountLockoutDuration,
35 lockoutThreshold: state.accountLockoutThreshold
36 };
37 },
38 accountPasswordRequirements(state) {
39 return {
40 minLength: state.accountMinPasswordLength,
41 maxLength: state.accountMaxPasswordLength
42 };
Yoshie Muranaka35080ac2020-01-17 15:38:57 -060043 }
44 },
45 mutations: {
46 setUsers(state, allUsers) {
47 state.allUsers = allUsers;
Yoshie Muranaka52b02232020-02-20 08:00:45 -080048 },
49 setLockoutDuration(state, lockoutDuration) {
50 state.accountLockoutDuration = lockoutDuration;
51 },
52 setLockoutThreshold(state, lockoutThreshold) {
53 state.accountLockoutThreshold = lockoutThreshold;
54 },
55 setAccountMinPasswordLength(state, minPasswordLength) {
56 state.accountMinPasswordLength = minPasswordLength;
57 },
58 setAccountMaxPasswordLength(state, maxPasswordLength) {
59 state.accountMaxPasswordLength = maxPasswordLength;
Yoshie Muranaka35080ac2020-01-17 15:38:57 -060060 }
61 },
62 actions: {
63 getUsers({ commit }) {
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080064 api
Derick Montaguefded0d12019-12-11 06:16:40 -060065 .get('/redfish/v1/AccountService/Accounts')
66 .then(response => response.data.Members.map(user => user['@odata.id']))
Yoshie Muranaka463a5702019-12-04 09:09:36 -080067 .then(userIds => api.all(userIds.map(user => api.get(user))))
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080068 .then(users => {
69 const userData = users.map(user => user.data);
Derick Montaguefded0d12019-12-11 06:16:40 -060070 commit('setUsers', userData);
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080071 })
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -080072 .catch(error => {
73 console.log(error);
74 throw new Error('Error loading local users.');
75 });
Yoshie Muranaka463a5702019-12-04 09:09:36 -080076 },
Yoshie Muranaka52b02232020-02-20 08:00:45 -080077 getAccountSettings({ commit }) {
78 api
79 .get('/redfish/v1/AccountService')
80 .then(({ data }) => {
81 commit('setLockoutDuration', data.AccountLockoutDuration);
82 commit('setLockoutThreshold', data.AccountLockoutThreshold);
83 commit('setAccountMinPasswordLength', data.MinPasswordLength);
84 commit('setAccountMaxPasswordLength', data.MaxPasswordLength);
85 })
86 .catch(error => {
87 console.log(error);
88 throw new Error('Error loading account settings.');
89 });
90 },
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -080091 async createUser({ dispatch }, { username, password, privilege, status }) {
Yoshie Muranaka463a5702019-12-04 09:09:36 -080092 const data = {
93 UserName: username,
94 Password: password,
95 RoleId: privilege,
96 Enabled: status
97 };
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -080098 return await api
Derick Montaguefded0d12019-12-11 06:16:40 -060099 .post('/redfish/v1/AccountService/Accounts', data)
100 .then(() => dispatch('getUsers'))
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800101 .then(() => `Created user '${username}'.`)
102 .catch(error => {
103 console.log(error);
104 throw new Error(`Error creating user '${username}'.`);
105 });
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800106 },
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800107 async updateUser(
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800108 { dispatch },
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700109 { originalUsername, username, password, privilege, status, locked }
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800110 ) {
111 const data = {};
112 if (username) data.UserName = username;
113 if (password) data.Password = password;
114 if (privilege) data.RoleId = privilege;
115 if (status !== undefined) data.Enabled = status;
Yoshie Muranaka1f9ed4c2020-03-26 16:59:54 -0700116 if (locked !== undefined) data.Locked = locked;
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800117 return await api
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800118 .patch(`/redfish/v1/AccountService/Accounts/${originalUsername}`, data)
Derick Montaguefded0d12019-12-11 06:16:40 -0600119 .then(() => dispatch('getUsers'))
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800120 .then(() => `Updated user '${originalUsername}'.`)
121 .catch(error => {
122 console.log(error);
123 throw new Error(`Error updating user '${originalUsername}'.`);
124 });
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800125 },
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800126 async deleteUser({ dispatch }, username) {
127 return await api
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800128 .delete(`/redfish/v1/AccountService/Accounts/${username}`)
Derick Montaguefded0d12019-12-11 06:16:40 -0600129 .then(() => dispatch('getUsers'))
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800130 .then(() => `Deleted user '${username}'.`)
131 .catch(error => {
132 console.log(error);
133 throw new Error(`Error deleting user '${username}'.`);
134 });
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800135 },
136 async deleteUsers({ dispatch }, users) {
137 const promises = users.map(({ username }) => {
138 return api
139 .delete(`/redfish/v1/AccountService/Accounts/${username}`)
140 .catch(error => {
141 console.log(error);
142 return error;
143 });
144 });
145 return await api
146 .all(promises)
147 .then(response => {
148 dispatch('getUsers');
149 return response;
150 })
151 .then(
152 api.spread((...responses) => {
153 const { successCount, errorCount } = getResponseCount(responses);
154 let toastMessages = [];
155
156 if (successCount) {
157 const message = i18n.tc(
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -0800158 'pageLocalUserManagement.toast.successDeleteUsers',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800159 successCount
160 );
161 toastMessages.push({ type: 'success', message });
162 }
163
164 if (errorCount) {
165 const message = i18n.tc(
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -0800166 'pageLocalUserManagement.toast.errorDeleteUsers',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800167 errorCount
168 );
169 toastMessages.push({ type: 'error', message });
170 }
171
172 return toastMessages;
173 })
174 );
175 },
176 async enableUsers({ dispatch }, users) {
177 const data = {
178 Enabled: true
179 };
180 const promises = users.map(({ username }) => {
181 return api
182 .patch(`/redfish/v1/AccountService/Accounts/${username}`, data)
183 .catch(error => {
184 console.log(error);
185 return error;
186 });
187 });
188 return await api
189 .all(promises)
190 .then(response => {
191 dispatch('getUsers');
192 return response;
193 })
194 .then(
195 api.spread((...responses) => {
196 const { successCount, errorCount } = getResponseCount(responses);
197 let toastMessages = [];
198
199 if (successCount) {
200 const message = i18n.tc(
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -0800201 'pageLocalUserManagement.toast.successEnableUsers',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800202 successCount
203 );
204 toastMessages.push({ type: 'success', message });
205 }
206
207 if (errorCount) {
208 const message = i18n.tc(
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -0800209 'pageLocalUserManagement.toast.errorEnableUsers',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800210 errorCount
211 );
212 toastMessages.push({ type: 'error', message });
213 }
214
215 return toastMessages;
216 })
217 );
218 },
219 async disableUsers({ dispatch }, users) {
220 const data = {
221 Enabled: false
222 };
223 const promises = users.map(({ username }) => {
224 return api
225 .patch(`/redfish/v1/AccountService/Accounts/${username}`, data)
226 .catch(error => {
227 console.log(error);
228 return error;
229 });
230 });
231 return await api
232 .all(promises)
233 .then(response => {
234 dispatch('getUsers');
235 return response;
236 })
237 .then(
238 api.spread((...responses) => {
239 const { successCount, errorCount } = getResponseCount(responses);
240 let toastMessages = [];
241
242 if (successCount) {
243 const message = i18n.tc(
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -0800244 'pageLocalUserManagement.toast.successDisableUsers',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800245 successCount
246 );
247 toastMessages.push({ type: 'success', message });
248 }
249
250 if (errorCount) {
251 const message = i18n.tc(
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -0800252 'pageLocalUserManagement.toast.errorDisableUsers',
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800253 errorCount
254 );
255 toastMessages.push({ type: 'error', message });
256 }
257
258 return toastMessages;
259 })
260 );
Yoshie Muranaka1b1c1002020-02-20 10:18:36 -0800261 },
262 async saveAccountSettings(
263 { dispatch },
264 { lockoutThreshold, lockoutDuration }
265 ) {
266 const data = {};
267 if (lockoutThreshold !== undefined) {
268 data.AccountLockoutThreshold = lockoutThreshold;
269 }
270 if (lockoutDuration !== undefined) {
271 data.AccountLockoutDuration = lockoutDuration;
272 }
273
274 return await api
275 .patch('/redfish/v1/AccountService', data)
276 //GET new settings to update view
277 .then(() => dispatch('getAccountSettings'))
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -0800278 .then(() => i18n.t('pageLocalUserManagement.toast.successSaveSettings'))
Yoshie Muranaka1b1c1002020-02-20 10:18:36 -0800279 .catch(error => {
280 console.log(error);
281 const message = i18n.t(
Yoshie Muranaka547b5fc2020-02-24 15:42:40 -0800282 'pageLocalUserManagement.toast.errorSaveSettings'
Yoshie Muranaka1b1c1002020-02-20 10:18:36 -0800283 );
284 throw new Error(message);
285 });
Yoshie Muranaka35080ac2020-01-17 15:38:57 -0600286 }
287 }
288};
289
290export default LocalUserManagementStore;