Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 1 | import api from '../../api'; |
Yoshie Muranaka | 35080ac | 2020-01-17 15:38:57 -0600 | [diff] [blame] | 2 | |
| 3 | const LocalUserManagementStore = { |
| 4 | namespaced: true, |
| 5 | state: { |
| 6 | allUsers: [] |
| 7 | }, |
| 8 | getters: { |
| 9 | allUsers(state) { |
| 10 | return state.allUsers; |
| 11 | } |
| 12 | }, |
| 13 | mutations: { |
| 14 | setUsers(state, allUsers) { |
| 15 | state.allUsers = allUsers; |
| 16 | } |
| 17 | }, |
| 18 | actions: { |
| 19 | getUsers({ commit }) { |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 20 | api |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 21 | .get('/redfish/v1/AccountService/Accounts') |
| 22 | .then(response => response.data.Members.map(user => user['@odata.id'])) |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 23 | .then(userIds => api.all(userIds.map(user => api.get(user)))) |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 24 | .then(users => { |
| 25 | const userData = users.map(user => user.data); |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 26 | commit('setUsers', userData); |
Yoshie Muranaka | 74c24f1 | 2019-12-03 10:45:46 -0800 | [diff] [blame] | 27 | }) |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame^] | 28 | .catch(error => { |
| 29 | console.log(error); |
| 30 | throw new Error('Error loading local users.'); |
| 31 | }); |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 32 | }, |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame^] | 33 | async createUser({ dispatch }, { username, password, privilege, status }) { |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 34 | const data = { |
| 35 | UserName: username, |
| 36 | Password: password, |
| 37 | RoleId: privilege, |
| 38 | Enabled: status |
| 39 | }; |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame^] | 40 | return await api |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 41 | .post('/redfish/v1/AccountService/Accounts', data) |
| 42 | .then(() => dispatch('getUsers')) |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame^] | 43 | .then(() => `Created user '${username}'.`) |
| 44 | .catch(error => { |
| 45 | console.log(error); |
| 46 | throw new Error(`Error creating user '${username}'.`); |
| 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 updateUser( |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 50 | { dispatch }, |
| 51 | { originalUsername, username, password, privilege, status } |
| 52 | ) { |
| 53 | const data = {}; |
| 54 | if (username) data.UserName = username; |
| 55 | if (password) data.Password = password; |
| 56 | if (privilege) data.RoleId = privilege; |
| 57 | if (status !== undefined) data.Enabled = status; |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame^] | 58 | return await api |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 59 | .patch(`/redfish/v1/AccountService/Accounts/${originalUsername}`, data) |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 60 | .then(() => dispatch('getUsers')) |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame^] | 61 | .then(() => `Updated user '${originalUsername}'.`) |
| 62 | .catch(error => { |
| 63 | console.log(error); |
| 64 | throw new Error(`Error updating user '${originalUsername}'.`); |
| 65 | }); |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 66 | }, |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame^] | 67 | async deleteUser({ dispatch }, username) { |
| 68 | return await api |
Yoshie Muranaka | 463a570 | 2019-12-04 09:09:36 -0800 | [diff] [blame] | 69 | .delete(`/redfish/v1/AccountService/Accounts/${username}`) |
Derick Montague | fded0d1 | 2019-12-11 06:16:40 -0600 | [diff] [blame] | 70 | .then(() => dispatch('getUsers')) |
Yoshie Muranaka | 0fc91e7 | 2020-02-05 11:23:06 -0800 | [diff] [blame^] | 71 | .then(() => `Deleted user '${username}'.`) |
| 72 | .catch(error => { |
| 73 | console.log(error); |
| 74 | throw new Error(`Error deleting user '${username}'.`); |
| 75 | }); |
Yoshie Muranaka | 35080ac | 2020-01-17 15:38:57 -0600 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | export default LocalUserManagementStore; |