Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 1 | import api from '../../api'; |
Yoshie Muranaka | 183c275 | 2020-02-12 11:30:49 -0800 | [diff] [blame^] | 2 | import i18n from '../../../i18n'; |
| 3 | |
| 4 | const 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 Muranaka | 35080ac | 2020-01-17 15:38:57 -0600 | [diff] [blame] | 18 | |
| 19 | const 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 Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 36 | api |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 37 | .get('/redfish/v1/AccountService/Accounts') |
| 38 | .then(response => response.data.Members.map(user => user['@odata.id'])) |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 39 | .then(userIds => api.all(userIds.map(user => api.get(user)))) |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 40 | .then(users => { |
| 41 | const userData = users.map(user => user.data); |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 42 | commit('setUsers', userData); |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 43 | }) |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 44 | .catch(error => { |
| 45 | console.log(error); |
| 46 | throw new Error('Error loading local users.'); |
| 47 | }); |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 48 | }, |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 49 | async createUser({ dispatch }, { username, password, privilege, status }) { |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 50 | const data = { |
| 51 | UserName: username, |
| 52 | Password: password, |
| 53 | RoleId: privilege, |
| 54 | Enabled: status |
| 55 | }; |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 56 | return await api |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 57 | .post('/redfish/v1/AccountService/Accounts', data) |
| 58 | .then(() => dispatch('getUsers')) |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 59 | .then(() => `Created user '${username}'.`) |
| 60 | .catch(error => { |
| 61 | console.log(error); |
| 62 | throw new Error(`Error creating user '${username}'.`); |
| 63 | }); |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 64 | }, |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 65 | async updateUser( |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 66 | { 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 Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 74 | return await api |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 75 | .patch(`/redfish/v1/AccountService/Accounts/${originalUsername}`, data) |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 76 | .then(() => dispatch('getUsers')) |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 77 | .then(() => `Updated user '${originalUsername}'.`) |
| 78 | .catch(error => { |
| 79 | console.log(error); |
| 80 | throw new Error(`Error updating user '${originalUsername}'.`); |
| 81 | }); |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 82 | }, |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 83 | async deleteUser({ dispatch }, username) { |
| 84 | return await api |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 85 | .delete(`/redfish/v1/AccountService/Accounts/${username}`) |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 86 | .then(() => dispatch('getUsers')) |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 87 | .then(() => `Deleted user '${username}'.`) |
| 88 | .catch(error => { |
| 89 | console.log(error); |
| 90 | throw new Error(`Error deleting user '${username}'.`); |
| 91 | }); |
Yoshie Muranaka | 183c275 | 2020-02-12 11:30:49 -0800 | [diff] [blame^] | 92 | }, |
| 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 Muranaka | 35080ac | 2020-01-17 15:38:57 -0600 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | }; |
| 221 | |
| 222 | export default LocalUserManagementStore; |