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: { |
Yoshie Muranaka | 52b0223 | 2020-02-20 08:00:45 -0800 | [diff] [blame^] | 22 | allUsers: [], |
| 23 | accountLockoutDuration: null, |
| 24 | accountLockoutThreshold: null, |
| 25 | accountMinPasswordLength: null, |
| 26 | accountMaxPasswordLength: null |
Yoshie Muranaka | 35080ac | 2020-01-17 15:38:57 -0600 | [diff] [blame] | 27 | }, |
| 28 | getters: { |
| 29 | allUsers(state) { |
| 30 | return state.allUsers; |
Yoshie Muranaka | 52b0223 | 2020-02-20 08:00:45 -0800 | [diff] [blame^] | 31 | }, |
| 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 Muranaka | 35080ac | 2020-01-17 15:38:57 -0600 | [diff] [blame] | 43 | } |
| 44 | }, |
| 45 | mutations: { |
| 46 | setUsers(state, allUsers) { |
| 47 | state.allUsers = allUsers; |
Yoshie Muranaka | 52b0223 | 2020-02-20 08:00:45 -0800 | [diff] [blame^] | 48 | }, |
| 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 Muranaka | 35080ac | 2020-01-17 15:38:57 -0600 | [diff] [blame] | 60 | } |
| 61 | }, |
| 62 | actions: { |
| 63 | getUsers({ commit }) { |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 64 | api |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 65 | .get('/redfish/v1/AccountService/Accounts') |
| 66 | .then(response => response.data.Members.map(user => user['@odata.id'])) |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 67 | .then(userIds => api.all(userIds.map(user => api.get(user)))) |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 68 | .then(users => { |
| 69 | const userData = users.map(user => user.data); |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 70 | commit('setUsers', userData); |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 71 | }) |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 72 | .catch(error => { |
| 73 | console.log(error); |
| 74 | throw new Error('Error loading local users.'); |
| 75 | }); |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 76 | }, |
Yoshie Muranaka | 52b0223 | 2020-02-20 08:00:45 -0800 | [diff] [blame^] | 77 | 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 Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 91 | async createUser({ dispatch }, { username, password, privilege, status }) { |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 92 | const data = { |
| 93 | UserName: username, |
| 94 | Password: password, |
| 95 | RoleId: privilege, |
| 96 | Enabled: status |
| 97 | }; |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 98 | return await api |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 99 | .post('/redfish/v1/AccountService/Accounts', data) |
| 100 | .then(() => dispatch('getUsers')) |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 101 | .then(() => `Created user '${username}'.`) |
| 102 | .catch(error => { |
| 103 | console.log(error); |
| 104 | throw new Error(`Error creating user '${username}'.`); |
| 105 | }); |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 106 | }, |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 107 | async updateUser( |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 108 | { 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 Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 116 | return await api |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 117 | .patch(`/redfish/v1/AccountService/Accounts/${originalUsername}`, data) |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 118 | .then(() => dispatch('getUsers')) |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 119 | .then(() => `Updated user '${originalUsername}'.`) |
| 120 | .catch(error => { |
| 121 | console.log(error); |
| 122 | throw new Error(`Error updating user '${originalUsername}'.`); |
| 123 | }); |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 124 | }, |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 125 | async deleteUser({ dispatch }, username) { |
| 126 | return await api |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 127 | .delete(`/redfish/v1/AccountService/Accounts/${username}`) |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 128 | .then(() => dispatch('getUsers')) |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 129 | .then(() => `Deleted user '${username}'.`) |
| 130 | .catch(error => { |
| 131 | console.log(error); |
| 132 | throw new Error(`Error deleting user '${username}'.`); |
| 133 | }); |
Yoshie Muranaka | 183c275 | 2020-02-12 11:30:49 -0800 | [diff] [blame] | 134 | }, |
| 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 Muranaka | 35080ac | 2020-01-17 15:38:57 -0600 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | }; |
| 263 | |
| 264 | export default LocalUserManagementStore; |