blob: bc14c734353747f9ceac65bed1d7d9f8da97d89b [file] [log] [blame]
Derick Montaguefded0d12019-12-11 06:16:40 -06001import api from '../../api';
Yoshie Muranaka35080ac2020-01-17 15:38:57 -06002
3const 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 Muranaka74c24f12019-12-03 10:45:46 -080020 api
Derick Montaguefded0d12019-12-11 06:16:40 -060021 .get('/redfish/v1/AccountService/Accounts')
22 .then(response => response.data.Members.map(user => user['@odata.id']))
Yoshie Muranaka463a5702019-12-04 09:09:36 -080023 .then(userIds => api.all(userIds.map(user => api.get(user))))
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080024 .then(users => {
25 const userData = users.map(user => user.data);
Derick Montaguefded0d12019-12-11 06:16:40 -060026 commit('setUsers', userData);
Yoshie Muranaka74c24f12019-12-03 10:45:46 -080027 })
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -080028 .catch(error => {
29 console.log(error);
30 throw new Error('Error loading local users.');
31 });
Yoshie Muranaka463a5702019-12-04 09:09:36 -080032 },
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -080033 async createUser({ dispatch }, { username, password, privilege, status }) {
Yoshie Muranaka463a5702019-12-04 09:09:36 -080034 const data = {
35 UserName: username,
36 Password: password,
37 RoleId: privilege,
38 Enabled: status
39 };
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -080040 return await api
Derick Montaguefded0d12019-12-11 06:16:40 -060041 .post('/redfish/v1/AccountService/Accounts', data)
42 .then(() => dispatch('getUsers'))
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -080043 .then(() => `Created user '${username}'.`)
44 .catch(error => {
45 console.log(error);
46 throw new Error(`Error creating user '${username}'.`);
47 });
Yoshie Muranaka463a5702019-12-04 09:09:36 -080048 },
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -080049 async updateUser(
Yoshie Muranaka463a5702019-12-04 09:09:36 -080050 { 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 Muranaka0fc91e72020-02-05 11:23:06 -080058 return await api
Yoshie Muranaka463a5702019-12-04 09:09:36 -080059 .patch(`/redfish/v1/AccountService/Accounts/${originalUsername}`, data)
Derick Montaguefded0d12019-12-11 06:16:40 -060060 .then(() => dispatch('getUsers'))
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -080061 .then(() => `Updated user '${originalUsername}'.`)
62 .catch(error => {
63 console.log(error);
64 throw new Error(`Error updating user '${originalUsername}'.`);
65 });
Yoshie Muranaka463a5702019-12-04 09:09:36 -080066 },
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -080067 async deleteUser({ dispatch }, username) {
68 return await api
Yoshie Muranaka463a5702019-12-04 09:09:36 -080069 .delete(`/redfish/v1/AccountService/Accounts/${username}`)
Derick Montaguefded0d12019-12-11 06:16:40 -060070 .then(() => dispatch('getUsers'))
Yoshie Muranaka0fc91e72020-02-05 11:23:06 -080071 .then(() => `Deleted user '${username}'.`)
72 .catch(error => {
73 console.log(error);
74 throw new Error(`Error deleting user '${username}'.`);
75 });
Yoshie Muranaka35080ac2020-01-17 15:38:57 -060076 }
77 }
78};
79
80export default LocalUserManagementStore;