summaryrefslogtreecommitdiff
path: root/src/store/modules/AccessControl/LocalUserMangementStore.js
blob: de79a2d71540afd69b1d45b9522ac3959331d5ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import api from "../../api";

const LocalUserManagementStore = {
  namespaced: true,
  state: {
    allUsers: []
  },
  getters: {
    allUsers(state) {
      return state.allUsers;
    }
  },
  mutations: {
    setUsers(state, allUsers) {
      state.allUsers = allUsers;
    }
  },
  actions: {
    getUsers({ commit }) {
      api
        .get("/redfish/v1/AccountService/Accounts")
        .then(response => response.data.Members.map(user => user["@odata.id"]))
        .then(userIds => api.all(userIds.map(user => api.get(user))))
        .then(users => {
          const userData = users.map(user => user.data);
          commit("setUsers", userData);
        })
        .catch(error => console.log(error));
    },
    createUser({ dispatch }, { username, password, privilege, status }) {
      const data = {
        UserName: username,
        Password: password,
        RoleId: privilege,
        Enabled: status
      };
      api
        .post("/redfish/v1/AccountService/Accounts", data)
        .then(() => dispatch("getUsers"))
        .catch(error => console.log(error));
    },
    updateUser(
      { dispatch },
      { originalUsername, username, password, privilege, status }
    ) {
      const data = {};
      if (username) data.UserName = username;
      if (password) data.Password = password;
      if (privilege) data.RoleId = privilege;
      if (status !== undefined) data.Enabled = status;
      api
        .patch(`/redfish/v1/AccountService/Accounts/${originalUsername}`, data)
        .then(() => dispatch("getUsers"))
        .catch(error => console.log(error));
    },
    deleteUser({ dispatch }, username) {
      api
        .delete(`/redfish/v1/AccountService/Accounts/${username}`)
        .then(() => dispatch("getUsers"))
        .catch(error => console.log(error));
    }
  }
};

export default LocalUserManagementStore;