blob: edb063c7ce71a092e7c56bf16d09c132c006f235 [file] [log] [blame]
Yoshie Muranakac4e38ab2020-04-09 12:41:27 -07001import api from '@/store/api';
2import i18n from '@/i18n';
Yoshie Muranakadc3d5412020-04-17 09:39:41 -07003import { find } from 'lodash';
Yoshie Muranakac4e38ab2020-04-09 12:41:27 -07004
5const LdapStore = {
6 namespaced: true,
7 state: {
8 isServiceEnabled: null,
9 ldap: {
10 serviceEnabled: null,
11 serviceAddress: null,
12 bindDn: null,
13 baseDn: null,
14 userAttribute: null,
Yoshie Muranakadc3d5412020-04-17 09:39:41 -070015 groupsAttribute: null,
Derick Montague602e98a2020-10-21 16:20:00 -050016 roleGroups: [],
Yoshie Muranakac4e38ab2020-04-09 12:41:27 -070017 },
18 activeDirectory: {
19 serviceEnabled: null,
20 serviceAddress: null,
21 bindDn: null,
22 baseDn: null,
23 userAttribute: null,
Yoshie Muranakadc3d5412020-04-17 09:39:41 -070024 groupsAttribute: null,
Derick Montague602e98a2020-10-21 16:20:00 -050025 roleGroups: [],
26 },
Yoshie Muranakac4e38ab2020-04-09 12:41:27 -070027 },
28 getters: {
Derick Montague602e98a2020-10-21 16:20:00 -050029 isServiceEnabled: (state) => state.isServiceEnabled,
30 ldap: (state) => state.ldap,
31 activeDirectory: (state) => state.activeDirectory,
32 isActiveDirectoryEnabled: (state) => {
Yoshie Muranakadc3d5412020-04-17 09:39:41 -070033 return state.activeDirectory.serviceEnabled;
34 },
35 enabledRoleGroups: (state, getters) => {
36 const serviceType = getters.isActiveDirectoryEnabled
37 ? 'activeDirectory'
38 : 'ldap';
39 return state[serviceType].roleGroups;
Derick Montague602e98a2020-10-21 16:20:00 -050040 },
Yoshie Muranakac4e38ab2020-04-09 12:41:27 -070041 },
42 mutations: {
43 setServiceEnabled: (state, serviceEnabled) =>
44 (state.isServiceEnabled = serviceEnabled),
45 setLdapProperties: (
46 state,
47 {
48 ServiceEnabled,
Sandeepa Singhd2422c42021-06-01 15:57:33 +053049 ServiceAddresses = [],
Yoshie Muranakac4e38ab2020-04-09 12:41:27 -070050 Authentication = {},
Sandeepa Singhd2422c42021-06-01 15:57:33 +053051 LDAPService: {
52 SearchSettings: {
53 BaseDistinguishedNames = [],
54 UsernameAttribute,
55 GroupsAttribute,
56 } = {},
57 } = {},
Derick Montague602e98a2020-10-21 16:20:00 -050058 RemoteRoleMapping = [],
Ed Tanous81323992024-02-27 11:26:24 -080059 },
Yoshie Muranakac4e38ab2020-04-09 12:41:27 -070060 ) => {
61 state.ldap.serviceAddress = ServiceAddresses[0];
62 state.ldap.serviceEnabled = ServiceEnabled;
Sandeepa Singhd2422c42021-06-01 15:57:33 +053063 state.ldap.baseDn = BaseDistinguishedNames[0];
Yoshie Muranakac4e38ab2020-04-09 12:41:27 -070064 state.ldap.bindDn = Authentication.Username;
Sandeepa Singhd2422c42021-06-01 15:57:33 +053065 state.ldap.userAttribute = UsernameAttribute;
66 state.ldap.groupsAttribute = GroupsAttribute;
Yoshie Muranakadc3d5412020-04-17 09:39:41 -070067 state.ldap.roleGroups = RemoteRoleMapping;
Yoshie Muranakac4e38ab2020-04-09 12:41:27 -070068 },
69 setActiveDirectoryProperties: (
70 state,
71 {
72 ServiceEnabled,
Sandeepa Singhd2422c42021-06-01 15:57:33 +053073 ServiceAddresses = [],
Yoshie Muranakac4e38ab2020-04-09 12:41:27 -070074 Authentication = {},
Sandeepa Singhd2422c42021-06-01 15:57:33 +053075 LDAPService: {
76 SearchSettings: {
77 BaseDistinguishedNames = [],
78 UsernameAttribute,
79 GroupsAttribute,
80 } = {},
81 } = {},
Derick Montague602e98a2020-10-21 16:20:00 -050082 RemoteRoleMapping = [],
Ed Tanous81323992024-02-27 11:26:24 -080083 },
Yoshie Muranakac4e38ab2020-04-09 12:41:27 -070084 ) => {
85 state.activeDirectory.serviceEnabled = ServiceEnabled;
86 state.activeDirectory.serviceAddress = ServiceAddresses[0];
87 state.activeDirectory.bindDn = Authentication.Username;
Sandeepa Singhd2422c42021-06-01 15:57:33 +053088 state.activeDirectory.baseDn = BaseDistinguishedNames[0];
89 state.activeDirectory.userAttribute = UsernameAttribute;
90 state.activeDirectory.groupsAttribute = GroupsAttribute;
Yoshie Muranakadc3d5412020-04-17 09:39:41 -070091 state.activeDirectory.roleGroups = RemoteRoleMapping;
Derick Montague602e98a2020-10-21 16:20:00 -050092 },
Yoshie Muranakac4e38ab2020-04-09 12:41:27 -070093 },
94 actions: {
Yoshie Muranakae9a59c72020-04-30 12:16:30 -070095 async getAccountSettings({ commit }) {
96 return await api
Yoshie Muranakac4e38ab2020-04-09 12:41:27 -070097 .get('/redfish/v1/AccountService')
98 .then(({ data: { LDAP = {}, ActiveDirectory = {} } }) => {
99 const ldapEnabled = LDAP.ServiceEnabled;
100 const activeDirectoryEnabled = ActiveDirectory.ServiceEnabled;
101
102 commit('setServiceEnabled', ldapEnabled || activeDirectoryEnabled);
103 commit('setLdapProperties', LDAP);
104 commit('setActiveDirectoryProperties', ActiveDirectory);
105 })
Derick Montague602e98a2020-10-21 16:20:00 -0500106 .catch((error) => console.log(error));
Yoshie Muranakac4e38ab2020-04-09 12:41:27 -0700107 },
108 async saveLdapSettings({ state, dispatch }, properties) {
109 const data = { LDAP: properties };
110 if (state.activeDirectory.serviceEnabled) {
111 // Disable Active Directory service if enabled
112 await api.patch('/redfish/v1/AccountService', {
Derick Montague602e98a2020-10-21 16:20:00 -0500113 ActiveDirectory: { ServiceEnabled: false },
Yoshie Muranakac4e38ab2020-04-09 12:41:27 -0700114 });
115 }
116 return await api
117 .patch('/redfish/v1/AccountService', data)
118 .then(() => dispatch('getAccountSettings'))
119 .then(() => i18n.t('pageLdap.toast.successSaveLdapSettings'))
Derick Montague602e98a2020-10-21 16:20:00 -0500120 .catch((error) => {
Yoshie Muranakac4e38ab2020-04-09 12:41:27 -0700121 console.log(error);
122 throw new Error(i18n.t('pageLdap.toast.errorSaveLdapSettings'));
123 });
124 },
125 async saveActiveDirectorySettings({ state, dispatch }, properties) {
126 const data = { ActiveDirectory: properties };
127 if (state.ldap.serviceEnabled) {
128 // Disable LDAP service if enabled
129 await api.patch('/redfish/v1/AccountService', {
Derick Montague602e98a2020-10-21 16:20:00 -0500130 LDAP: { ServiceEnabled: false },
Yoshie Muranakac4e38ab2020-04-09 12:41:27 -0700131 });
132 }
133 return await api
134 .patch('/redfish/v1/AccountService', data)
135 .then(() => dispatch('getAccountSettings'))
136 .then(() => i18n.t('pageLdap.toast.successSaveActiveDirectorySettings'))
Derick Montague602e98a2020-10-21 16:20:00 -0500137 .catch((error) => {
Yoshie Muranakac4e38ab2020-04-09 12:41:27 -0700138 console.log(error);
139 throw new Error(
Ed Tanous81323992024-02-27 11:26:24 -0800140 i18n.t('pageLdap.toast.errorSaveActiveDirectorySettings'),
Yoshie Muranakac4e38ab2020-04-09 12:41:27 -0700141 );
142 });
143 },
144 async saveAccountSettings(
145 { dispatch },
146 {
147 serviceEnabled,
148 serviceAddress,
149 activeDirectoryEnabled,
150 bindDn,
151 bindPassword,
152 baseDn,
153 userIdAttribute,
Derick Montague602e98a2020-10-21 16:20:00 -0500154 groupIdAttribute,
Ed Tanous81323992024-02-27 11:26:24 -0800155 },
Yoshie Muranakac4e38ab2020-04-09 12:41:27 -0700156 ) {
157 const data = {
158 ServiceEnabled: serviceEnabled,
159 ServiceAddresses: [serviceAddress],
160 Authentication: {
161 Username: bindDn,
Derick Montague602e98a2020-10-21 16:20:00 -0500162 Password: bindPassword,
Yoshie Muranakac4e38ab2020-04-09 12:41:27 -0700163 },
164 LDAPService: {
165 SearchSettings: {
Derick Montague602e98a2020-10-21 16:20:00 -0500166 BaseDistinguishedNames: [baseDn],
167 },
168 },
Yoshie Muranakac4e38ab2020-04-09 12:41:27 -0700169 };
170 if (groupIdAttribute)
171 data.LDAPService.SearchSettings.GroupsAttribute = groupIdAttribute;
172 if (userIdAttribute)
173 data.LDAPService.SearchSettings.UsernameAttribute = userIdAttribute;
174
175 if (activeDirectoryEnabled) {
176 return await dispatch('saveActiveDirectorySettings', data);
177 } else {
178 return await dispatch('saveLdapSettings', data);
179 }
Yoshie Muranakadc3d5412020-04-17 09:39:41 -0700180 },
181 async addNewRoleGroup(
182 { dispatch, getters },
Ed Tanous81323992024-02-27 11:26:24 -0800183 { groupName, groupPrivilege },
Yoshie Muranakadc3d5412020-04-17 09:39:41 -0700184 ) {
185 const data = {};
186 const enabledRoleGroups = getters['enabledRoleGroups'];
187 const isActiveDirectoryEnabled = getters['isActiveDirectoryEnabled'];
188 const RemoteRoleMapping = [
189 ...enabledRoleGroups,
190 {
191 LocalRole: groupPrivilege,
Derick Montague602e98a2020-10-21 16:20:00 -0500192 RemoteGroup: groupName,
193 },
Yoshie Muranakadc3d5412020-04-17 09:39:41 -0700194 ];
195 if (isActiveDirectoryEnabled) {
196 data.ActiveDirectory = { RemoteRoleMapping };
197 } else {
198 data.LDAP = { RemoteRoleMapping };
199 }
200 return await api
201 .patch('/redfish/v1/AccountService', data)
202 .then(() => dispatch('getAccountSettings'))
203 .then(() =>
204 i18n.t('pageLdap.toast.successAddRoleGroup', {
Derick Montague602e98a2020-10-21 16:20:00 -0500205 groupName,
Ed Tanous81323992024-02-27 11:26:24 -0800206 }),
Yoshie Muranakadc3d5412020-04-17 09:39:41 -0700207 )
Derick Montague602e98a2020-10-21 16:20:00 -0500208 .catch((error) => {
Yoshie Muranakadc3d5412020-04-17 09:39:41 -0700209 console.log(error);
210 throw new Error(i18n.t('pageLdap.toast.errorAddRoleGroup'));
211 });
212 },
213 async saveRoleGroup({ dispatch, getters }, { groupName, groupPrivilege }) {
214 const data = {};
215 const enabledRoleGroups = getters['enabledRoleGroups'];
216 const isActiveDirectoryEnabled = getters['isActiveDirectoryEnabled'];
Derick Montague602e98a2020-10-21 16:20:00 -0500217 const RemoteRoleMapping = enabledRoleGroups.map((group) => {
Yoshie Muranakadc3d5412020-04-17 09:39:41 -0700218 if (group.RemoteGroup === groupName) {
219 return {
220 RemoteGroup: groupName,
Derick Montague602e98a2020-10-21 16:20:00 -0500221 LocalRole: groupPrivilege,
Yoshie Muranakadc3d5412020-04-17 09:39:41 -0700222 };
223 } else {
224 return {};
225 }
226 });
227 if (isActiveDirectoryEnabled) {
228 data.ActiveDirectory = { RemoteRoleMapping };
229 } else {
230 data.LDAP = { RemoteRoleMapping };
231 }
232 return await api
233 .patch('/redfish/v1/AccountService', data)
234 .then(() => dispatch('getAccountSettings'))
235 .then(() =>
Ed Tanous81323992024-02-27 11:26:24 -0800236 i18n.t('pageLdap.toast.successSaveRoleGroup', { groupName }),
Yoshie Muranakadc3d5412020-04-17 09:39:41 -0700237 )
Derick Montague602e98a2020-10-21 16:20:00 -0500238 .catch((error) => {
Yoshie Muranakadc3d5412020-04-17 09:39:41 -0700239 console.log(error);
240 throw new Error(i18n.t('pageLdap.toast.errorSaveRoleGroup'));
241 });
242 },
243 async deleteRoleGroup({ dispatch, getters }, { roleGroups = [] }) {
244 const data = {};
245 const enabledRoleGroups = getters['enabledRoleGroups'];
246 const isActiveDirectoryEnabled = getters['isActiveDirectoryEnabled'];
Derick Montague602e98a2020-10-21 16:20:00 -0500247 const RemoteRoleMapping = enabledRoleGroups.map((group) => {
Yoshie Muranakadc3d5412020-04-17 09:39:41 -0700248 if (find(roleGroups, { groupName: group.RemoteGroup })) {
249 return null;
250 } else {
251 return {};
252 }
253 });
254 if (isActiveDirectoryEnabled) {
255 data.ActiveDirectory = { RemoteRoleMapping };
256 } else {
257 data.LDAP = { RemoteRoleMapping };
258 }
259 return await api
260 .patch('/redfish/v1/AccountService', data)
261 .then(() => dispatch('getAccountSettings'))
262 .then(() =>
Ed Tanous81323992024-02-27 11:26:24 -0800263 i18n.tc('pageLdap.toast.successDeleteRoleGroup', roleGroups.length),
Yoshie Muranakadc3d5412020-04-17 09:39:41 -0700264 )
Derick Montague602e98a2020-10-21 16:20:00 -0500265 .catch((error) => {
Yoshie Muranakadc3d5412020-04-17 09:39:41 -0700266 console.log(error);
267 throw new Error(
Ed Tanous81323992024-02-27 11:26:24 -0800268 i18n.tc('pageLdap.toast.errorDeleteRoleGroup', roleGroups.length),
Yoshie Muranakadc3d5412020-04-17 09:39:41 -0700269 );
270 });
Derick Montague602e98a2020-10-21 16:20:00 -0500271 },
272 },
Yoshie Muranakac4e38ab2020-04-09 12:41:27 -0700273};
274
275export default LdapStore;