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 }, |
Yoshie Muranaka | 1f9ed4c | 2020-03-26 16:59:54 -0700 | [diff] [blame] | 109 | { originalUsername, username, password, privilege, status, locked } |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 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 | 1f9ed4c | 2020-03-26 16:59:54 -0700 | [diff] [blame] | 116 | if (locked !== undefined) data.Locked = locked; |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 117 | return await api |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 118 | .patch(`/redfish/v1/AccountService/Accounts/${originalUsername}`, data) |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 119 | .then(() => dispatch('getUsers')) |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 120 | .then(() => `Updated user '${originalUsername}'.`) |
| 121 | .catch(error => { |
| 122 | console.log(error); |
| 123 | throw new Error(`Error updating user '${originalUsername}'.`); |
| 124 | }); |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 125 | }, |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 126 | async deleteUser({ dispatch }, username) { |
| 127 | return await api |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 128 | .delete(`/redfish/v1/AccountService/Accounts/${username}`) |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 129 | .then(() => dispatch('getUsers')) |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame] | 130 | .then(() => `Deleted user '${username}'.`) |
| 131 | .catch(error => { |
| 132 | console.log(error); |
| 133 | throw new Error(`Error deleting user '${username}'.`); |
| 134 | }); |
Yoshie Muranaka | 183c275 | 2020-02-12 11:30:49 -0800 | [diff] [blame] | 135 | }, |
| 136 | async deleteUsers({ dispatch }, users) { |
| 137 | const promises = users.map(({ username }) => { |
| 138 | return api |
| 139 | .delete(`/redfish/v1/AccountService/Accounts/${username}`) |
| 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( |
Yoshie Muranaka | 547b5fc | 2020-02-24 15:42:40 -0800 | [diff] [blame] | 158 | 'pageLocalUserManagement.toast.successDeleteUsers', |
Yoshie Muranaka | 183c275 | 2020-02-12 11:30:49 -0800 | [diff] [blame] | 159 | successCount |
| 160 | ); |
| 161 | toastMessages.push({ type: 'success', message }); |
| 162 | } |
| 163 | |
| 164 | if (errorCount) { |
| 165 | const message = i18n.tc( |
Yoshie Muranaka | 547b5fc | 2020-02-24 15:42:40 -0800 | [diff] [blame] | 166 | 'pageLocalUserManagement.toast.errorDeleteUsers', |
Yoshie Muranaka | 183c275 | 2020-02-12 11:30:49 -0800 | [diff] [blame] | 167 | errorCount |
| 168 | ); |
| 169 | toastMessages.push({ type: 'error', message }); |
| 170 | } |
| 171 | |
| 172 | return toastMessages; |
| 173 | }) |
| 174 | ); |
| 175 | }, |
| 176 | async enableUsers({ dispatch }, users) { |
| 177 | const data = { |
| 178 | Enabled: true |
| 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( |
Yoshie Muranaka | 547b5fc | 2020-02-24 15:42:40 -0800 | [diff] [blame] | 201 | 'pageLocalUserManagement.toast.successEnableUsers', |
Yoshie Muranaka | 183c275 | 2020-02-12 11:30:49 -0800 | [diff] [blame] | 202 | successCount |
| 203 | ); |
| 204 | toastMessages.push({ type: 'success', message }); |
| 205 | } |
| 206 | |
| 207 | if (errorCount) { |
| 208 | const message = i18n.tc( |
Yoshie Muranaka | 547b5fc | 2020-02-24 15:42:40 -0800 | [diff] [blame] | 209 | 'pageLocalUserManagement.toast.errorEnableUsers', |
Yoshie Muranaka | 183c275 | 2020-02-12 11:30:49 -0800 | [diff] [blame] | 210 | errorCount |
| 211 | ); |
| 212 | toastMessages.push({ type: 'error', message }); |
| 213 | } |
| 214 | |
| 215 | return toastMessages; |
| 216 | }) |
| 217 | ); |
| 218 | }, |
| 219 | async disableUsers({ dispatch }, users) { |
| 220 | const data = { |
| 221 | Enabled: false |
| 222 | }; |
| 223 | const promises = users.map(({ username }) => { |
| 224 | return api |
| 225 | .patch(`/redfish/v1/AccountService/Accounts/${username}`, data) |
| 226 | .catch(error => { |
| 227 | console.log(error); |
| 228 | return error; |
| 229 | }); |
| 230 | }); |
| 231 | return await api |
| 232 | .all(promises) |
| 233 | .then(response => { |
| 234 | dispatch('getUsers'); |
| 235 | return response; |
| 236 | }) |
| 237 | .then( |
| 238 | api.spread((...responses) => { |
| 239 | const { successCount, errorCount } = getResponseCount(responses); |
| 240 | let toastMessages = []; |
| 241 | |
| 242 | if (successCount) { |
| 243 | const message = i18n.tc( |
Yoshie Muranaka | 547b5fc | 2020-02-24 15:42:40 -0800 | [diff] [blame] | 244 | 'pageLocalUserManagement.toast.successDisableUsers', |
Yoshie Muranaka | 183c275 | 2020-02-12 11:30:49 -0800 | [diff] [blame] | 245 | successCount |
| 246 | ); |
| 247 | toastMessages.push({ type: 'success', message }); |
| 248 | } |
| 249 | |
| 250 | if (errorCount) { |
| 251 | const message = i18n.tc( |
Yoshie Muranaka | 547b5fc | 2020-02-24 15:42:40 -0800 | [diff] [blame] | 252 | 'pageLocalUserManagement.toast.errorDisableUsers', |
Yoshie Muranaka | 183c275 | 2020-02-12 11:30:49 -0800 | [diff] [blame] | 253 | errorCount |
| 254 | ); |
| 255 | toastMessages.push({ type: 'error', message }); |
| 256 | } |
| 257 | |
| 258 | return toastMessages; |
| 259 | }) |
| 260 | ); |
Yoshie Muranaka | 1b1c100 | 2020-02-20 10:18:36 -0800 | [diff] [blame] | 261 | }, |
| 262 | async saveAccountSettings( |
| 263 | { dispatch }, |
| 264 | { lockoutThreshold, lockoutDuration } |
| 265 | ) { |
| 266 | const data = {}; |
| 267 | if (lockoutThreshold !== undefined) { |
| 268 | data.AccountLockoutThreshold = lockoutThreshold; |
| 269 | } |
| 270 | if (lockoutDuration !== undefined) { |
| 271 | data.AccountLockoutDuration = lockoutDuration; |
| 272 | } |
| 273 | |
| 274 | return await api |
| 275 | .patch('/redfish/v1/AccountService', data) |
| 276 | //GET new settings to update view |
| 277 | .then(() => dispatch('getAccountSettings')) |
Yoshie Muranaka | 547b5fc | 2020-02-24 15:42:40 -0800 | [diff] [blame] | 278 | .then(() => i18n.t('pageLocalUserManagement.toast.successSaveSettings')) |
Yoshie Muranaka | 1b1c100 | 2020-02-20 10:18:36 -0800 | [diff] [blame] | 279 | .catch(error => { |
| 280 | console.log(error); |
| 281 | const message = i18n.t( |
Yoshie Muranaka | 547b5fc | 2020-02-24 15:42:40 -0800 | [diff] [blame] | 282 | 'pageLocalUserManagement.toast.errorSaveSettings' |
Yoshie Muranaka | 1b1c100 | 2020-02-20 10:18:36 -0800 | [diff] [blame] | 283 | ); |
| 284 | throw new Error(message); |
| 285 | }); |
Yoshie Muranaka | 35080ac | 2020-01-17 15:38:57 -0600 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | }; |
| 289 | |
| 290 | export default LocalUserManagementStore; |