xref: /openbmc/webui-vue/src/store/modules/SecurityAndAccess/SessionsStore.js (revision 80a87851ab2b1bddb9f42cc494b0ad7799b06012)
1import api, { getResponseCount } from '@/store/api';
2import i18n from '@/i18n';
3
4const SessionsStore = {
5  namespaced: true,
6  state: {
7    allConnections: [],
8  },
9  getters: {
10    allConnections: (state) => state.allConnections,
11  },
12  mutations: {
13    setAllConnections: (state, allConnections) =>
14      (state.allConnections = allConnections),
15  },
16  actions: {
17    async getSessionsData({ commit }) {
18      return await api
19        .get('/redfish/v1/SessionService/Sessions')
20        .then((response) =>
21          response.data.Members.map((sessionLogs) => sessionLogs['@odata.id'])
22        )
23        .then((sessionUris) =>
24          api.all(sessionUris.map((sessionUri) => api.get(sessionUri)))
25        )
26        .then((sessionUris) => {
27          const allConnectionsData = sessionUris.map((sessionUri) => {
28            //For filtering IP address to IPv4
29            let filteredIPAddress = sessionUri.data?.ClientOriginIPAddress.slice(
30              7
31            );
32            return {
33              clientID: sessionUri.data?.Oem?.OpenBMC.ClientID,
34              username: sessionUri.data?.UserName,
35              ipAddress: filteredIPAddress,
36              uri: sessionUri.data['@odata.id'],
37            };
38          });
39          commit('setAllConnections', allConnectionsData);
40        })
41        .catch((error) => {
42          console.log('Client Session Data:', error);
43        });
44    },
45    async disconnectSessions({ dispatch }, uris = []) {
46      const promises = uris.map((uri) =>
47        api.delete(uri).catch((error) => {
48          console.log(error);
49          return error;
50        })
51      );
52      return await api
53        .all(promises)
54        .then((response) => {
55          dispatch('getSessionsData');
56          return response;
57        })
58        .then(
59          api.spread((...responses) => {
60            const { successCount, errorCount } = getResponseCount(responses);
61            const toastMessages = [];
62
63            if (successCount) {
64              const message = i18n.tc(
65                'pageSessions.toast.successDelete',
66                successCount
67              );
68              toastMessages.push({ type: 'success', message });
69            }
70
71            if (errorCount) {
72              const message = i18n.tc(
73                'pageSessions.toast.errorDelete',
74                errorCount
75              );
76              toastMessages.push({ type: 'error', message });
77            }
78            return toastMessages;
79          })
80        );
81    },
82  },
83};
84export default SessionsStore;
85