blob: 67c3a1e45aaa1200a94183f8a8b75eab86a6843d [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 },
109 { originalUsername, username, password, privilege, status }
110 ) {
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 Muranaka0fc91e72020-02-05 11:23:06 -0800116 return await api
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800117 .patch(`/redfish/v1/AccountService/Accounts/${originalUsername}`, data)
Derick Montaguefded0d12019-12-11 06:16:40 -0600118 .then(() => dispatch('getUsers'))
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800119 .then(() => `Updated user '${originalUsername}'.`)
120 .catch(error => {
121 console.log(error);
122 throw new Error(`Error updating user '${originalUsername}'.`);
123 });
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800124 },
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800125 async deleteUser({ dispatch }, username) {
126 return await api
Yoshie Muranaka463a5702019-12-04 09:09:36 -0800127 .delete(`/redfish/v1/AccountService/Accounts/${username}`)
Derick Montaguefded0d12019-12-11 06:16:40 -0600128 .then(() => dispatch('getUsers'))
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -0800129 .then(() => `Deleted user '${username}'.`)
130 .catch(error => {
131 console.log(error);
132 throw new Error(`Error deleting user '${username}'.`);
133 });
Yoshie Muranaka183c2752020-02-12 11:30:49 -0800134 },
135 async deleteUsers({ dispatch }, users) {
136 const promises = users.map(({ username }) => {
137 return api
138 .delete(`/redfish/v1/AccountService/Accounts/${username}`)
139 .catch(error => {
140 console.log(error);
141 return error;
142 });
143 });
144 return await api
145 .all(promises)
146 .then(response => {
147 dispatch('getUsers');
148 return response;
149 })
150 .then(
151 api.spread((...responses) => {
152 const { successCount, errorCount } = getResponseCount(responses);
153 let toastMessages = [];
154
155 if (successCount) {
156 const message = i18n.tc(
157 'localUserManagement.toastMessages.successDeleteUsers',
158 successCount
159 );
160 toastMessages.push({ type: 'success', message });
161 }
162
163 if (errorCount) {
164 const message = i18n.tc(
165 'localUserManagement.toastMessages.errorDeleteUsers',
166 errorCount
167 );
168 toastMessages.push({ type: 'error', message });
169 }
170
171 return toastMessages;
172 })
173 );
174 },
175 async enableUsers({ dispatch }, users) {
176 const data = {
177 Enabled: true
178 };
179 const promises = users.map(({ username }) => {
180 return api
181 .patch(`/redfish/v1/AccountService/Accounts/${username}`, data)
182 .catch(error => {
183 console.log(error);
184 return error;
185 });
186 });
187 return await api
188 .all(promises)
189 .then(response => {
190 dispatch('getUsers');
191 return response;
192 })
193 .then(
194 api.spread((...responses) => {
195 const { successCount, errorCount } = getResponseCount(responses);
196 let toastMessages = [];
197
198 if (successCount) {
199 const message = i18n.tc(
200 'localUserManagement.toastMessages.successEnableUsers',
201 successCount
202 );
203 toastMessages.push({ type: 'success', message });
204 }
205
206 if (errorCount) {
207 const message = i18n.tc(
208 'localUserManagement.toastMessages.errorEnableUsers',
209 errorCount
210 );
211 toastMessages.push({ type: 'error', message });
212 }
213
214 return toastMessages;
215 })
216 );
217 },
218 async disableUsers({ dispatch }, users) {
219 const data = {
220 Enabled: false
221 };
222 const promises = users.map(({ username }) => {
223 return api
224 .patch(`/redfish/v1/AccountService/Accounts/${username}`, data)
225 .catch(error => {
226 console.log(error);
227 return error;
228 });
229 });
230 return await api
231 .all(promises)
232 .then(response => {
233 dispatch('getUsers');
234 return response;
235 })
236 .then(
237 api.spread((...responses) => {
238 const { successCount, errorCount } = getResponseCount(responses);
239 let toastMessages = [];
240
241 if (successCount) {
242 const message = i18n.tc(
243 'localUserManagement.toastMessages.successDisableUsers',
244 successCount
245 );
246 toastMessages.push({ type: 'success', message });
247 }
248
249 if (errorCount) {
250 const message = i18n.tc(
251 'localUserManagement.toastMessages.errorDisableUsers',
252 errorCount
253 );
254 toastMessages.push({ type: 'error', message });
255 }
256
257 return toastMessages;
258 })
259 );
Yoshie Muranaka35080ac2020-01-17 15:38:57 -0600260 }
261 }
262};
263
264export default LocalUserManagementStore;