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: [], |
Yoshie Muranaka | 038a9da | 2020-04-17 11:22:56 -0700 | [diff] [blame] | 23 | accountRoles: [], |
Yoshie Muranaka | 52b0223 | 2020-02-20 08:00:45 -0800 | [diff] [blame] | 24 | accountLockoutDuration: null, |
| 25 | accountLockoutThreshold: null, |
| 26 | accountMinPasswordLength: null, |
| 27 | accountMaxPasswordLength: null |
Yoshie Muranaka | 35080ac | 2020-01-17 15:38:57 -0600 | [diff] [blame] | 28 | }, |
| 29 | getters: { |
| 30 | allUsers(state) { |
| 31 | return state.allUsers; |
Yoshie Muranaka | 52b0223 | 2020-02-20 08:00:45 -0800 | [diff] [blame] | 32 | }, |
Yoshie Muranaka | 038a9da | 2020-04-17 11:22:56 -0700 | [diff] [blame] | 33 | accountRoles(state) { |
| 34 | return state.accountRoles; |
| 35 | }, |
Yoshie Muranaka | 52b0223 | 2020-02-20 08:00:45 -0800 | [diff] [blame] | 36 | accountSettings(state) { |
| 37 | return { |
| 38 | lockoutDuration: state.accountLockoutDuration, |
| 39 | lockoutThreshold: state.accountLockoutThreshold |
| 40 | }; |
| 41 | }, |
| 42 | accountPasswordRequirements(state) { |
| 43 | return { |
| 44 | minLength: state.accountMinPasswordLength, |
| 45 | maxLength: state.accountMaxPasswordLength |
| 46 | }; |
Yoshie Muranaka | 35080ac | 2020-01-17 15:38:57 -0600 | [diff] [blame] | 47 | } |
| 48 | }, |
| 49 | mutations: { |
| 50 | setUsers(state, allUsers) { |
| 51 | state.allUsers = allUsers; |
Yoshie Muranaka | 52b0223 | 2020-02-20 08:00:45 -0800 | [diff] [blame] | 52 | }, |
Yoshie Muranaka | 038a9da | 2020-04-17 11:22:56 -0700 | [diff] [blame] | 53 | setAccountRoles(state, accountRoles) { |
| 54 | state.accountRoles = accountRoles; |
| 55 | }, |
Yoshie Muranaka | 52b0223 | 2020-02-20 08:00:45 -0800 | [diff] [blame] | 56 | setLockoutDuration(state, lockoutDuration) { |
| 57 | state.accountLockoutDuration = lockoutDuration; |
| 58 | }, |
| 59 | setLockoutThreshold(state, lockoutThreshold) { |
| 60 | state.accountLockoutThreshold = lockoutThreshold; |
| 61 | }, |
| 62 | setAccountMinPasswordLength(state, minPasswordLength) { |
| 63 | state.accountMinPasswordLength = minPasswordLength; |
| 64 | }, |
| 65 | setAccountMaxPasswordLength(state, maxPasswordLength) { |
| 66 | state.accountMaxPasswordLength = maxPasswordLength; |
Yoshie Muranaka | 35080ac | 2020-01-17 15:38:57 -0600 | [diff] [blame] | 67 | } |
| 68 | }, |
| 69 | actions: { |
| 70 | getUsers({ commit }) { |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 71 | api |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 72 | .get('/redfish/v1/AccountService/Accounts') |
| 73 | .then(response => response.data.Members.map(user => user['@odata.id'])) |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 74 | .then(userIds => api.all(userIds.map(user => api.get(user)))) |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 75 | .then(users => { |
| 76 | const userData = users.map(user => user.data); |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 77 | commit('setUsers', userData); |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 78 | }) |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 79 | .catch(error => { |
| 80 | console.log(error); |
| 81 | throw new Error('Error loading local users.'); |
| 82 | }); |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 83 | }, |
Yoshie Muranaka | 52b0223 | 2020-02-20 08:00:45 -0800 | [diff] [blame] | 84 | getAccountSettings({ commit }) { |
| 85 | api |
| 86 | .get('/redfish/v1/AccountService') |
| 87 | .then(({ data }) => { |
| 88 | commit('setLockoutDuration', data.AccountLockoutDuration); |
| 89 | commit('setLockoutThreshold', data.AccountLockoutThreshold); |
| 90 | commit('setAccountMinPasswordLength', data.MinPasswordLength); |
| 91 | commit('setAccountMaxPasswordLength', data.MaxPasswordLength); |
| 92 | }) |
| 93 | .catch(error => { |
| 94 | console.log(error); |
| 95 | throw new Error('Error loading account settings.'); |
| 96 | }); |
| 97 | }, |
Yoshie Muranaka | 038a9da | 2020-04-17 11:22:56 -0700 | [diff] [blame] | 98 | getAccountRoles({ commit }) { |
| 99 | api |
| 100 | .get('/redfish/v1/AccountService/Roles') |
| 101 | .then(({ data: { Members = [] } = {} }) => { |
| 102 | const roles = Members.map(role => { |
| 103 | return role['@odata.id'].split('/').pop(); |
| 104 | }); |
| 105 | commit('setAccountRoles', roles); |
| 106 | }) |
| 107 | .catch(error => console.log(error)); |
| 108 | }, |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 109 | async createUser({ dispatch }, { username, password, privilege, status }) { |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 110 | const data = { |
| 111 | UserName: username, |
| 112 | Password: password, |
| 113 | RoleId: privilege, |
| 114 | Enabled: status |
| 115 | }; |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 116 | return await api |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 117 | .post('/redfish/v1/AccountService/Accounts', data) |
| 118 | .then(() => dispatch('getUsers')) |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 119 | .then(() => `Created user '${username}'.`) |
| 120 | .catch(error => { |
| 121 | console.log(error); |
| 122 | throw new Error(`Error creating user '${username}'.`); |
| 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 updateUser( |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 126 | { dispatch }, |
Yoshie Muranaka | 1f9ed4c | 2020-03-26 16:59:54 -0700 | [diff] [blame] | 127 | { originalUsername, username, password, privilege, status, locked } |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 128 | ) { |
| 129 | const data = {}; |
| 130 | if (username) data.UserName = username; |
| 131 | if (password) data.Password = password; |
| 132 | if (privilege) data.RoleId = privilege; |
| 133 | if (status !== undefined) data.Enabled = status; |
Yoshie Muranaka | 1f9ed4c | 2020-03-26 16:59:54 -0700 | [diff] [blame] | 134 | if (locked !== undefined) data.Locked = locked; |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 135 | return await api |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 136 | .patch(`/redfish/v1/AccountService/Accounts/${originalUsername}`, data) |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 137 | .then(() => dispatch('getUsers')) |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 138 | .then(() => `Updated user '${originalUsername}'.`) |
| 139 | .catch(error => { |
| 140 | console.log(error); |
| 141 | throw new Error(`Error updating user '${originalUsername}'.`); |
| 142 | }); |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 143 | }, |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 144 | async deleteUser({ dispatch }, username) { |
| 145 | return await api |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 146 | .delete(`/redfish/v1/AccountService/Accounts/${username}`) |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 147 | .then(() => dispatch('getUsers')) |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 148 | .then(() => `Deleted user '${username}'.`) |
| 149 | .catch(error => { |
| 150 | console.log(error); |
| 151 | throw new Error(`Error deleting user '${username}'.`); |
| 152 | }); |
Yoshie Muranaka | 183c275 | 2020-02-12 11:30:49 -0800 | [diff] [blame] | 153 | }, |
| 154 | async deleteUsers({ dispatch }, users) { |
| 155 | const promises = users.map(({ username }) => { |
| 156 | return api |
| 157 | .delete(`/redfish/v1/AccountService/Accounts/${username}`) |
| 158 | .catch(error => { |
| 159 | console.log(error); |
| 160 | return error; |
| 161 | }); |
| 162 | }); |
| 163 | return await api |
| 164 | .all(promises) |
| 165 | .then(response => { |
| 166 | dispatch('getUsers'); |
| 167 | return response; |
| 168 | }) |
| 169 | .then( |
| 170 | api.spread((...responses) => { |
| 171 | const { successCount, errorCount } = getResponseCount(responses); |
| 172 | let toastMessages = []; |
| 173 | |
| 174 | if (successCount) { |
| 175 | const message = i18n.tc( |
Yoshie Muranaka | 547b5fc | 2020-02-24 15:42:40 -0800 | [diff] [blame] | 176 | 'pageLocalUserManagement.toast.successDeleteUsers', |
Yoshie Muranaka | 183c275 | 2020-02-12 11:30:49 -0800 | [diff] [blame] | 177 | successCount |
| 178 | ); |
| 179 | toastMessages.push({ type: 'success', message }); |
| 180 | } |
| 181 | |
| 182 | if (errorCount) { |
| 183 | const message = i18n.tc( |
Yoshie Muranaka | 547b5fc | 2020-02-24 15:42:40 -0800 | [diff] [blame] | 184 | 'pageLocalUserManagement.toast.errorDeleteUsers', |
Yoshie Muranaka | 183c275 | 2020-02-12 11:30:49 -0800 | [diff] [blame] | 185 | errorCount |
| 186 | ); |
| 187 | toastMessages.push({ type: 'error', message }); |
| 188 | } |
| 189 | |
| 190 | return toastMessages; |
| 191 | }) |
| 192 | ); |
| 193 | }, |
| 194 | async enableUsers({ dispatch }, users) { |
| 195 | const data = { |
| 196 | Enabled: true |
| 197 | }; |
| 198 | const promises = users.map(({ username }) => { |
| 199 | return api |
| 200 | .patch(`/redfish/v1/AccountService/Accounts/${username}`, data) |
| 201 | .catch(error => { |
| 202 | console.log(error); |
| 203 | return error; |
| 204 | }); |
| 205 | }); |
| 206 | return await api |
| 207 | .all(promises) |
| 208 | .then(response => { |
| 209 | dispatch('getUsers'); |
| 210 | return response; |
| 211 | }) |
| 212 | .then( |
| 213 | api.spread((...responses) => { |
| 214 | const { successCount, errorCount } = getResponseCount(responses); |
| 215 | let toastMessages = []; |
| 216 | |
| 217 | if (successCount) { |
| 218 | const message = i18n.tc( |
Yoshie Muranaka | 547b5fc | 2020-02-24 15:42:40 -0800 | [diff] [blame] | 219 | 'pageLocalUserManagement.toast.successEnableUsers', |
Yoshie Muranaka | 183c275 | 2020-02-12 11:30:49 -0800 | [diff] [blame] | 220 | successCount |
| 221 | ); |
| 222 | toastMessages.push({ type: 'success', message }); |
| 223 | } |
| 224 | |
| 225 | if (errorCount) { |
| 226 | const message = i18n.tc( |
Yoshie Muranaka | 547b5fc | 2020-02-24 15:42:40 -0800 | [diff] [blame] | 227 | 'pageLocalUserManagement.toast.errorEnableUsers', |
Yoshie Muranaka | 183c275 | 2020-02-12 11:30:49 -0800 | [diff] [blame] | 228 | errorCount |
| 229 | ); |
| 230 | toastMessages.push({ type: 'error', message }); |
| 231 | } |
| 232 | |
| 233 | return toastMessages; |
| 234 | }) |
| 235 | ); |
| 236 | }, |
| 237 | async disableUsers({ dispatch }, users) { |
| 238 | const data = { |
| 239 | Enabled: false |
| 240 | }; |
| 241 | const promises = users.map(({ username }) => { |
| 242 | return api |
| 243 | .patch(`/redfish/v1/AccountService/Accounts/${username}`, data) |
| 244 | .catch(error => { |
| 245 | console.log(error); |
| 246 | return error; |
| 247 | }); |
| 248 | }); |
| 249 | return await api |
| 250 | .all(promises) |
| 251 | .then(response => { |
| 252 | dispatch('getUsers'); |
| 253 | return response; |
| 254 | }) |
| 255 | .then( |
| 256 | api.spread((...responses) => { |
| 257 | const { successCount, errorCount } = getResponseCount(responses); |
| 258 | let toastMessages = []; |
| 259 | |
| 260 | if (successCount) { |
| 261 | const message = i18n.tc( |
Yoshie Muranaka | 547b5fc | 2020-02-24 15:42:40 -0800 | [diff] [blame] | 262 | 'pageLocalUserManagement.toast.successDisableUsers', |
Yoshie Muranaka | 183c275 | 2020-02-12 11:30:49 -0800 | [diff] [blame] | 263 | successCount |
| 264 | ); |
| 265 | toastMessages.push({ type: 'success', message }); |
| 266 | } |
| 267 | |
| 268 | if (errorCount) { |
| 269 | const message = i18n.tc( |
Yoshie Muranaka | 547b5fc | 2020-02-24 15:42:40 -0800 | [diff] [blame] | 270 | 'pageLocalUserManagement.toast.errorDisableUsers', |
Yoshie Muranaka | 183c275 | 2020-02-12 11:30:49 -0800 | [diff] [blame] | 271 | errorCount |
| 272 | ); |
| 273 | toastMessages.push({ type: 'error', message }); |
| 274 | } |
| 275 | |
| 276 | return toastMessages; |
| 277 | }) |
| 278 | ); |
Yoshie Muranaka | 1b1c100 | 2020-02-20 10:18:36 -0800 | [diff] [blame] | 279 | }, |
| 280 | async saveAccountSettings( |
| 281 | { dispatch }, |
| 282 | { lockoutThreshold, lockoutDuration } |
| 283 | ) { |
| 284 | const data = {}; |
| 285 | if (lockoutThreshold !== undefined) { |
| 286 | data.AccountLockoutThreshold = lockoutThreshold; |
| 287 | } |
| 288 | if (lockoutDuration !== undefined) { |
| 289 | data.AccountLockoutDuration = lockoutDuration; |
| 290 | } |
| 291 | |
| 292 | return await api |
| 293 | .patch('/redfish/v1/AccountService', data) |
| 294 | //GET new settings to update view |
| 295 | .then(() => dispatch('getAccountSettings')) |
Yoshie Muranaka | 547b5fc | 2020-02-24 15:42:40 -0800 | [diff] [blame] | 296 | .then(() => i18n.t('pageLocalUserManagement.toast.successSaveSettings')) |
Yoshie Muranaka | 1b1c100 | 2020-02-20 10:18:36 -0800 | [diff] [blame] | 297 | .catch(error => { |
| 298 | console.log(error); |
| 299 | const message = i18n.t( |
Yoshie Muranaka | 547b5fc | 2020-02-24 15:42:40 -0800 | [diff] [blame] | 300 | 'pageLocalUserManagement.toast.errorSaveSettings' |
Yoshie Muranaka | 1b1c100 | 2020-02-20 10:18:36 -0800 | [diff] [blame] | 301 | ); |
| 302 | throw new Error(message); |
| 303 | }); |
Yoshie Muranaka | 35080ac | 2020-01-17 15:38:57 -0600 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | }; |
| 307 | |
| 308 | export default LocalUserManagementStore; |