blob: eb5822e1adba5015a30eb8fd75dbd88262f31746 [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: {
22 allUsers: []
23 },
24 getters: {
25 allUsers(state) {
26 return state.allUsers;
27 }
28 },
29 mutations: {
30 setUsers(state, allUsers) {
31 state.allUsers = allUsers;
32 }
33 },
34 actions: {
35 getUsers({ commit }) {
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080036 api
Derick Montaguefded0d12019-12-11 06:16:40 -060037 .get('/redfish/v1/AccountService/Accounts')
38 .then(response => response.data.Members.map(user => user['@odata.id']))
Yoshie Muranaka463a5702019-12-04 09:09:36 -080039 .then(userIds => api.all(userIds.map(user => api.get(user))))
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080040 .then(users => {
41 const userData = users.map(user => user.data);
Derick Montaguefded0d12019-12-11 06:16:40 -060042 commit('setUsers', userData);
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080043 })
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -080044 .catch(error => {
45 console.log(error);
46 throw new Error('Error loading local users.');
47 });
Yoshie Muranaka463a5702019-12-04 09:09:36 -080048 },
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -080049 async createUser({ dispatch }, { username, password, privilege, status }) {
Yoshie Muranaka463a5702019-12-04 09:09:36 -080050 const data = {
51 UserName: username,
52 Password: password,
53 RoleId: privilege,
54 Enabled: status
55 };
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -080056 return await api
Derick Montaguefded0d12019-12-11 06:16:40 -060057 .post('/redfish/v1/AccountService/Accounts', data)
58 .then(() => dispatch('getUsers'))
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -080059 .then(() => `Created user '${username}'.`)
60 .catch(error => {
61 console.log(error);
62 throw new Error(`Error creating user '${username}'.`);
63 });
Yoshie Muranaka463a5702019-12-04 09:09:36 -080064 },
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -080065 async updateUser(
Yoshie Muranaka463a5702019-12-04 09:09:36 -080066 { dispatch },
67 { originalUsername, username, password, privilege, status }
68 ) {
69 const data = {};
70 if (username) data.UserName = username;
71 if (password) data.Password = password;
72 if (privilege) data.RoleId = privilege;
73 if (status !== undefined) data.Enabled = status;
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -080074 return await api
Yoshie Muranaka463a5702019-12-04 09:09:36 -080075 .patch(`/redfish/v1/AccountService/Accounts/${originalUsername}`, data)
Derick Montaguefded0d12019-12-11 06:16:40 -060076 .then(() => dispatch('getUsers'))
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -080077 .then(() => `Updated user '${originalUsername}'.`)
78 .catch(error => {
79 console.log(error);
80 throw new Error(`Error updating user '${originalUsername}'.`);
81 });
Yoshie Muranaka463a5702019-12-04 09:09:36 -080082 },
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -080083 async deleteUser({ dispatch }, username) {
84 return await api
Yoshie Muranaka463a5702019-12-04 09:09:36 -080085 .delete(`/redfish/v1/AccountService/Accounts/${username}`)
Derick Montaguefded0d12019-12-11 06:16:40 -060086 .then(() => dispatch('getUsers'))
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -080087 .then(() => `Deleted user '${username}'.`)
88 .catch(error => {
89 console.log(error);
90 throw new Error(`Error deleting user '${username}'.`);
91 });
Yoshie Muranaka183c2752020-02-12 11:30:49 -080092 },
93 async deleteUsers({ dispatch }, users) {
94 const promises = users.map(({ username }) => {
95 return api
96 .delete(`/redfish/v1/AccountService/Accounts/${username}`)
97 .catch(error => {
98 console.log(error);
99 return error;
100 });
101 });
102 return await api
103 .all(promises)
104 .then(response => {
105 dispatch('getUsers');
106 return response;
107 })
108 .then(
109 api.spread((...responses) => {
110 const { successCount, errorCount } = getResponseCount(responses);
111 let toastMessages = [];
112
113 if (successCount) {
114 const message = i18n.tc(
115 'localUserManagement.toastMessages.successDeleteUsers',
116 successCount
117 );
118 toastMessages.push({ type: 'success', message });
119 }
120
121 if (errorCount) {
122 const message = i18n.tc(
123 'localUserManagement.toastMessages.errorDeleteUsers',
124 errorCount
125 );
126 toastMessages.push({ type: 'error', message });
127 }
128
129 return toastMessages;
130 })
131 );
132 },
133 async enableUsers({ dispatch }, users) {
134 const data = {
135 Enabled: true
136 };
137 const promises = users.map(({ username }) => {
138 return api
139 .patch(`/redfish/v1/AccountService/Accounts/${username}`, data)
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(
158 'localUserManagement.toastMessages.successEnableUsers',
159 successCount
160 );
161 toastMessages.push({ type: 'success', message });
162 }
163
164 if (errorCount) {
165 const message = i18n.tc(
166 'localUserManagement.toastMessages.errorEnableUsers',
167 errorCount
168 );
169 toastMessages.push({ type: 'error', message });
170 }
171
172 return toastMessages;
173 })
174 );
175 },
176 async disableUsers({ dispatch }, users) {
177 const data = {
178 Enabled: false
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(
201 'localUserManagement.toastMessages.successDisableUsers',
202 successCount
203 );
204 toastMessages.push({ type: 'success', message });
205 }
206
207 if (errorCount) {
208 const message = i18n.tc(
209 'localUserManagement.toastMessages.errorDisableUsers',
210 errorCount
211 );
212 toastMessages.push({ type: 'error', message });
213 }
214
215 return toastMessages;
216 })
217 );
Yoshie Muranaka35080ac2020-01-17 15:38:57 -0600218 }
219 }
220};
221
222export default LocalUserManagementStore;