001
014
015 package com.liferay.portlet.portalsettings.action;
016
017 import com.liferay.counter.service.CounterLocalServiceUtil;
018 import com.liferay.portal.kernel.servlet.SessionErrors;
019 import com.liferay.portal.kernel.util.ArrayUtil;
020 import com.liferay.portal.kernel.util.Constants;
021 import com.liferay.portal.kernel.util.ParamUtil;
022 import com.liferay.portal.kernel.util.PropertiesParamUtil;
023 import com.liferay.portal.kernel.util.PropsKeys;
024 import com.liferay.portal.kernel.util.StringPool;
025 import com.liferay.portal.kernel.util.StringUtil;
026 import com.liferay.portal.kernel.util.UnicodeProperties;
027 import com.liferay.portal.security.auth.PrincipalException;
028 import com.liferay.portal.security.ldap.LDAPSettingsUtil;
029 import com.liferay.portal.service.CompanyServiceUtil;
030 import com.liferay.portal.struts.PortletAction;
031 import com.liferay.portal.theme.ThemeDisplay;
032 import com.liferay.portal.util.Portal;
033 import com.liferay.portal.util.PrefsPropsUtil;
034 import com.liferay.portal.util.WebKeys;
035
036 import javax.portlet.ActionRequest;
037 import javax.portlet.ActionResponse;
038 import javax.portlet.PortletConfig;
039 import javax.portlet.PortletPreferences;
040 import javax.portlet.RenderRequest;
041 import javax.portlet.RenderResponse;
042
043 import org.apache.struts.action.ActionForm;
044 import org.apache.struts.action.ActionForward;
045 import org.apache.struts.action.ActionMapping;
046
047
050 public class EditLDAPServerAction extends PortletAction {
051
052 @Override
053 public void processAction(
054 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
055 ActionRequest actionRequest, ActionResponse actionResponse)
056 throws Exception {
057
058 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
059
060 try {
061 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
062 updateLDAPServer(actionRequest);
063 }
064 else if (cmd.equals(Constants.DELETE)) {
065 deleteLDAPServer(actionRequest);
066 }
067
068 sendRedirect(actionRequest, actionResponse);
069 }
070 catch (Exception e) {
071 if (e instanceof PrincipalException) {
072 SessionErrors.add(actionRequest, e.getClass().getName());
073
074 setForward(actionRequest, "portlet.portal_settings.error");
075 }
076 else {
077 throw e;
078 }
079 }
080 }
081
082 @Override
083 public ActionForward render(
084 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
085 RenderRequest renderRequest, RenderResponse renderResponse)
086 throws Exception {
087
088 return mapping.findForward(getForward(
089 renderRequest, "portlet.portal_settings.edit_ldap_server"));
090 }
091
092 protected UnicodeProperties addLDAPServer(
093 long companyId, UnicodeProperties properties)
094 throws Exception {
095
096 String defaultPostfix = LDAPSettingsUtil.getPropertyPostfix(0);
097
098 String[] defaultKeys = new String[_KEYS.length];
099
100 for (int i = 0; i < _KEYS.length; i++) {
101 defaultKeys[i] = _KEYS[i] + defaultPostfix;
102 }
103
104 long ldapServerId = CounterLocalServiceUtil.increment();
105
106 String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
107
108 String[] keys = properties.keySet().toArray(new String[0]);
109
110 for (String key : keys) {
111 if (ArrayUtil.contains(defaultKeys, key)) {
112 String value = properties.remove(key);
113
114 if (key.equals(
115 PropsKeys.LDAP_SECURITY_CREDENTIALS + defaultPostfix) &&
116 value.equals(Portal.TEMP_OBFUSCATION_VALUE)) {
117
118 value = PrefsPropsUtil.getString(
119 PropsKeys.LDAP_SECURITY_CREDENTIALS);
120 }
121
122 properties.setProperty(
123 key.replace(defaultPostfix, postfix), value);
124 }
125 }
126
127 PortletPreferences preferences = PrefsPropsUtil.getPreferences(
128 companyId);
129
130 String ldapServerIds = preferences.getValue(
131 "ldap.server.ids", StringPool.BLANK);
132
133 ldapServerIds = StringUtil.add(
134 ldapServerIds, String.valueOf(ldapServerId));
135
136 properties.setProperty("ldap.server.ids", ldapServerIds);
137
138 return properties;
139 }
140
141 protected void deleteLDAPServer(ActionRequest actionRequest)
142 throws Exception {
143
144 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
145 WebKeys.THEME_DISPLAY);
146
147 long ldapServerId = ParamUtil.getLong(actionRequest, "ldapServerId");
148
149
150
151 String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);
152
153 String[] keys = new String[_KEYS.length];
154
155 for (int i = 0; i < _KEYS.length; i++) {
156 keys[i] = _KEYS[i] + postfix;
157 }
158
159 CompanyServiceUtil.removePreferences(themeDisplay.getCompanyId(), keys);
160
161
162
163 PortletPreferences preferences = PrefsPropsUtil.getPreferences(
164 themeDisplay.getCompanyId());
165
166 UnicodeProperties properties = new UnicodeProperties();
167
168 String ldapServerIds = preferences.getValue(
169 "ldap.server.ids", StringPool.BLANK);
170
171 ldapServerIds = StringUtil.remove(
172 ldapServerIds, String.valueOf(ldapServerId));
173
174 properties.put("ldap.server.ids", ldapServerIds);
175
176 CompanyServiceUtil.updatePreferences(
177 themeDisplay.getCompanyId(), properties);
178 }
179
180 protected void updateLDAPServer(ActionRequest actionRequest)
181 throws Exception {
182
183 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
184 WebKeys.THEME_DISPLAY);
185
186 long ldapServerId = ParamUtil.getLong(actionRequest, "ldapServerId");
187
188 UnicodeProperties properties = PropertiesParamUtil.getProperties(
189 actionRequest, "settings--");
190
191 if (ldapServerId <= 0) {
192 properties = addLDAPServer(themeDisplay.getCompanyId(), properties);
193 }
194
195 CompanyServiceUtil.updatePreferences(
196 themeDisplay.getCompanyId(), properties);
197 }
198
199 private static final String[] _KEYS = {
200 PropsKeys.LDAP_AUTH_SEARCH_FILTER, PropsKeys.LDAP_BASE_DN,
201 PropsKeys.LDAP_BASE_PROVIDER_URL,
202 PropsKeys.LDAP_CONTACT_CUSTOM_MAPPINGS, PropsKeys.LDAP_CONTACT_MAPPINGS,
203 PropsKeys.LDAP_GROUP_DEFAULT_OBJECT_CLASSES,
204 PropsKeys.LDAP_GROUP_MAPPINGS, PropsKeys.LDAP_GROUPS_DN,
205 PropsKeys.LDAP_IMPORT_GROUP_SEARCH_FILTER,
206 PropsKeys.LDAP_IMPORT_USER_SEARCH_FILTER,
207 PropsKeys.LDAP_SECURITY_CREDENTIALS, PropsKeys.LDAP_SECURITY_PRINCIPAL,
208 PropsKeys.LDAP_SERVER_NAME, PropsKeys.LDAP_USER_CUSTOM_MAPPINGS,
209 PropsKeys.LDAP_USER_DEFAULT_OBJECT_CLASSES,
210 PropsKeys.LDAP_USER_MAPPINGS,
211 PropsKeys.LDAP_USERS_DN
212 };
213
214 }