1
14
15 package com.liferay.portlet.enterpriseadmin.action;
16
17 import com.liferay.counter.service.CounterLocalServiceUtil;
18 import com.liferay.portal.kernel.servlet.SessionErrors;
19 import com.liferay.portal.kernel.util.ArrayUtil;
20 import com.liferay.portal.kernel.util.Constants;
21 import com.liferay.portal.kernel.util.ParamUtil;
22 import com.liferay.portal.kernel.util.PropertiesParamUtil;
23 import com.liferay.portal.kernel.util.PropsKeys;
24 import com.liferay.portal.kernel.util.StringPool;
25 import com.liferay.portal.kernel.util.StringUtil;
26 import com.liferay.portal.kernel.util.UnicodeProperties;
27 import com.liferay.portal.security.auth.PrincipalException;
28 import com.liferay.portal.security.ldap.LDAPSettingsUtil;
29 import com.liferay.portal.service.CompanyServiceUtil;
30 import com.liferay.portal.struts.PortletAction;
31 import com.liferay.portal.theme.ThemeDisplay;
32 import com.liferay.portal.util.PrefsPropsUtil;
33 import com.liferay.portal.util.WebKeys;
34
35 import javax.portlet.ActionRequest;
36 import javax.portlet.ActionResponse;
37 import javax.portlet.PortletConfig;
38 import javax.portlet.PortletPreferences;
39 import javax.portlet.RenderRequest;
40 import javax.portlet.RenderResponse;
41
42 import org.apache.struts.action.ActionForm;
43 import org.apache.struts.action.ActionForward;
44 import org.apache.struts.action.ActionMapping;
45
46
51 public class EditLDAPServerAction extends PortletAction {
52
53 public void processAction(
54 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
55 ActionRequest actionRequest, ActionResponse actionResponse)
56 throws Exception {
57
58 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
59
60 try {
61 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
62 updateLDAPServer(actionRequest);
63 }
64 else if (cmd.equals(Constants.DELETE)) {
65 deleteLDAPServer(actionRequest);
66 }
67
68 sendRedirect(actionRequest, actionResponse);
69 }
70 catch (Exception e) {
71 if (e instanceof PrincipalException) {
72 SessionErrors.add(actionRequest, e.getClass().getName());
73
74 setForward(actionRequest, "portlet.enterprise_admin.error");
75 }
76 else {
77 throw e;
78 }
79 }
80 }
81
82 public ActionForward render(
83 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
84 RenderRequest renderRequest, RenderResponse renderResponse)
85 throws Exception {
86
87 return mapping.findForward(getForward(
88 renderRequest, "portlet.enterprise_admin.edit_ldap_server"));
89 }
90
91 protected UnicodeProperties addLDAPServer(
92 long companyId, UnicodeProperties properties)
93 throws Exception {
94
95 long ldapServerId = CounterLocalServiceUtil.increment();
96
97 String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
98
99 String[] keys = properties.keySet().toArray(new String[0]);
100
101 for (String key : keys) {
102 if (ArrayUtil.contains(_KEYS, key)) {
103 String value = properties.remove(key);
104
105 properties.setProperty(key + postfix, value);
106 }
107 }
108
109 PortletPreferences preferences = PrefsPropsUtil.getPreferences(
110 companyId);
111
112 String ldapServerIds = preferences.getValue(
113 "ldap.server.ids", StringPool.BLANK);
114
115 ldapServerIds = StringUtil.add(
116 ldapServerIds, String.valueOf(ldapServerId));
117
118 properties.setProperty("ldap.server.ids", ldapServerIds);
119
120 return properties;
121 }
122
123 protected void deleteLDAPServer(ActionRequest actionRequest)
124 throws Exception {
125
126 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
127 WebKeys.THEME_DISPLAY);
128
129 long ldapServerId = ParamUtil.getLong(actionRequest, "ldapServerId");
130
131
133 String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
134
135 String[] keys = new String[_KEYS.length];
136
137 for (int i = 0; i < _KEYS.length; i++) {
138 keys[i] = _KEYS[i] + postfix;
139 }
140
141 CompanyServiceUtil.removePreferences(
142 themeDisplay.getCompanyId(), keys);
143
144
146 PortletPreferences preferences = PrefsPropsUtil.getPreferences(
147 themeDisplay.getCompanyId());
148
149 UnicodeProperties properties = new UnicodeProperties();
150
151 String ldapServerIds = preferences.getValue(
152 "ldap.server.ids", StringPool.BLANK);
153
154 ldapServerIds = StringUtil.remove(
155 ldapServerIds, String.valueOf(ldapServerId));
156
157 properties.put("ldap.server.ids", ldapServerIds);
158
159 CompanyServiceUtil.updatePreferences(
160 themeDisplay.getCompanyId(), properties);
161 }
162
163 protected void updateLDAPServer(ActionRequest actionRequest)
164 throws Exception {
165
166 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
167 WebKeys.THEME_DISPLAY);
168
169 long ldapServerId = ParamUtil.getLong(actionRequest, "ldapServerId");
170
171 UnicodeProperties properties = PropertiesParamUtil.getProperties(
172 actionRequest, "settings(");
173
174 if (ldapServerId <= 0) {
175 properties = addLDAPServer(
176 themeDisplay.getCompanyId(), properties);
177 }
178
179 CompanyServiceUtil.updatePreferences(
180 themeDisplay.getCompanyId(), properties);
181 }
182
183 private final String[] _KEYS = {
184 PropsKeys.LDAP_AUTH_SEARCH_FILTER,
185 PropsKeys.LDAP_BASE_DN,
186 PropsKeys.LDAP_BASE_PROVIDER_URL,
187 PropsKeys.LDAP_GROUP_MAPPINGS,
188 PropsKeys.LDAP_GROUPS_DN,
189 PropsKeys.LDAP_IMPORT_GROUP_SEARCH_FILTER,
190 PropsKeys.LDAP_IMPORT_USER_SEARCH_FILTER,
191 PropsKeys.LDAP_SECURITY_CREDENTIALS,
192 PropsKeys.LDAP_SECURITY_PRINCIPAL,
193 PropsKeys.LDAP_SERVER_NAME,
194 PropsKeys.LDAP_USER_DEFAULT_OBJECT_CLASSES,
195 PropsKeys.LDAP_USER_MAPPINGS,
196 PropsKeys.LDAP_USERS_DN
197 };
198
199 }