blob: 54fbbccddfbf0a4ca24b909953e82fb07ecee71f [file] [log] [blame]
Yoshie Muranakac4e38ab2020-04-09 12:41:27 -07001import api from '@/store/api';
2import i18n from '@/i18n';
3
4const LdapStore = {
5 namespaced: true,
6 state: {
7 isServiceEnabled: null,
8 ldap: {
9 serviceEnabled: null,
10 serviceAddress: null,
11 bindDn: null,
12 baseDn: null,
13 userAttribute: null,
14 groupsAttribute: null
15 },
16 activeDirectory: {
17 serviceEnabled: null,
18 serviceAddress: null,
19 bindDn: null,
20 baseDn: null,
21 userAttribute: null,
22 groupsAttribute: null
23 }
24 },
25 getters: {
26 isServiceEnabled: state => state.isServiceEnabled,
27 ldap: state => state.ldap,
28 activeDirectory: state => state.activeDirectory
29 },
30 mutations: {
31 setServiceEnabled: (state, serviceEnabled) =>
32 (state.isServiceEnabled = serviceEnabled),
33 setLdapProperties: (
34 state,
35 {
36 ServiceEnabled,
37 ServiceAddresses,
38 Authentication = {},
39 LDAPService: { SearchSettings = {} } = {}
40 }
41 ) => {
42 state.ldap.serviceAddress = ServiceAddresses[0];
43 state.ldap.serviceEnabled = ServiceEnabled;
44 state.ldap.baseDn = SearchSettings.BaseDistinguishedNames[0];
45 state.ldap.bindDn = Authentication.Username;
46 state.ldap.userAttribute = SearchSettings.UsernameAttribute;
47 state.ldap.groupsAttribute = SearchSettings.GroupsAttribute;
48 },
49 setActiveDirectoryProperties: (
50 state,
51 {
52 ServiceEnabled,
53 ServiceAddresses,
54 Authentication = {},
55 LDAPService: { SearchSettings = {} } = {}
56 }
57 ) => {
58 state.activeDirectory.serviceEnabled = ServiceEnabled;
59 state.activeDirectory.serviceAddress = ServiceAddresses[0];
60 state.activeDirectory.bindDn = Authentication.Username;
61 state.activeDirectory.baseDn = SearchSettings.BaseDistinguishedNames[0];
62 state.activeDirectory.userAttribute = SearchSettings.UsernameAttribute;
63 state.activeDirectory.groupsAttribute = SearchSettings.GroupsAttribute;
64 }
65 },
66 actions: {
67 getAccountSettings({ commit }) {
68 api
69 .get('/redfish/v1/AccountService')
70 .then(({ data: { LDAP = {}, ActiveDirectory = {} } }) => {
71 const ldapEnabled = LDAP.ServiceEnabled;
72 const activeDirectoryEnabled = ActiveDirectory.ServiceEnabled;
73
74 commit('setServiceEnabled', ldapEnabled || activeDirectoryEnabled);
75 commit('setLdapProperties', LDAP);
76 commit('setActiveDirectoryProperties', ActiveDirectory);
77 })
78 .catch(error => console.log(error));
79 },
80 async saveLdapSettings({ state, dispatch }, properties) {
81 const data = { LDAP: properties };
82 if (state.activeDirectory.serviceEnabled) {
83 // Disable Active Directory service if enabled
84 await api.patch('/redfish/v1/AccountService', {
85 ActiveDirectory: { ServiceEnabled: false }
86 });
87 }
88 return await api
89 .patch('/redfish/v1/AccountService', data)
90 .then(() => dispatch('getAccountSettings'))
91 .then(() => i18n.t('pageLdap.toast.successSaveLdapSettings'))
92 .catch(error => {
93 console.log(error);
94 throw new Error(i18n.t('pageLdap.toast.errorSaveLdapSettings'));
95 });
96 },
97 async saveActiveDirectorySettings({ state, dispatch }, properties) {
98 const data = { ActiveDirectory: properties };
99 if (state.ldap.serviceEnabled) {
100 // Disable LDAP service if enabled
101 await api.patch('/redfish/v1/AccountService', {
102 LDAP: { ServiceEnabled: false }
103 });
104 }
105 return await api
106 .patch('/redfish/v1/AccountService', data)
107 .then(() => dispatch('getAccountSettings'))
108 .then(() => i18n.t('pageLdap.toast.successSaveActiveDirectorySettings'))
109 .catch(error => {
110 console.log(error);
111 throw new Error(
112 i18n.t('pageLdap.toast.errorSaveActiveDirectorySettings')
113 );
114 });
115 },
116 async saveAccountSettings(
117 { dispatch },
118 {
119 serviceEnabled,
120 serviceAddress,
121 activeDirectoryEnabled,
122 bindDn,
123 bindPassword,
124 baseDn,
125 userIdAttribute,
126 groupIdAttribute
127 }
128 ) {
129 const data = {
130 ServiceEnabled: serviceEnabled,
131 ServiceAddresses: [serviceAddress],
132 Authentication: {
133 Username: bindDn,
134 Password: bindPassword
135 },
136 LDAPService: {
137 SearchSettings: {
138 BaseDistinguishedNames: [baseDn]
139 }
140 }
141 };
142 if (groupIdAttribute)
143 data.LDAPService.SearchSettings.GroupsAttribute = groupIdAttribute;
144 if (userIdAttribute)
145 data.LDAPService.SearchSettings.UsernameAttribute = userIdAttribute;
146
147 if (activeDirectoryEnabled) {
148 return await dispatch('saveActiveDirectorySettings', data);
149 } else {
150 return await dispatch('saveLdapSettings', data);
151 }
152 }
153 }
154};
155
156export default LdapStore;