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