blob: de79a2d71540afd69b1d45b9522ac3959331d5ee [file] [log] [blame]
Yoshie Muranaka74c24f12019-12-03 10:45:46 -08001import 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
21 .get("/redfish/v1/AccountService/Accounts")
Yoshie Muranaka463a5702019-12-04 09:09:36 -080022 .then(response => response.data.Members.map(user => user["@odata.id"]))
23 .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);
26 commit("setUsers", userData);
27 })
Yoshie Muranaka463a5702019-12-04 09:09:36 -080028 .catch(error => console.log(error));
29 },
30 createUser({ dispatch }, { username, password, privilege, status }) {
31 const data = {
32 UserName: username,
33 Password: password,
34 RoleId: privilege,
35 Enabled: status
36 };
37 api
38 .post("/redfish/v1/AccountService/Accounts", data)
39 .then(() => dispatch("getUsers"))
40 .catch(error => console.log(error));
41 },
42 updateUser(
43 { dispatch },
44 { originalUsername, username, password, privilege, status }
45 ) {
46 const data = {};
47 if (username) data.UserName = username;
48 if (password) data.Password = password;
49 if (privilege) data.RoleId = privilege;
50 if (status !== undefined) data.Enabled = status;
51 api
52 .patch(`/redfish/v1/AccountService/Accounts/${originalUsername}`, data)
53 .then(() => dispatch("getUsers"))
54 .catch(error => console.log(error));
55 },
56 deleteUser({ dispatch }, username) {
57 api
58 .delete(`/redfish/v1/AccountService/Accounts/${username}`)
59 .then(() => dispatch("getUsers"))
60 .catch(error => console.log(error));
Yoshie Muranaka35080ac2020-01-17 15:38:57 -060061 }
62 }
63};
64
65export default LocalUserManagementStore;