1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.enterpriseadmin.action;
24  
25  import com.liferay.portal.kernel.servlet.SessionErrors;
26  import com.liferay.portal.kernel.util.Constants;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.model.Company;
30  import com.liferay.portal.model.RoleConstants;
31  import com.liferay.portal.security.auth.PrincipalException;
32  import com.liferay.portal.security.ldap.PortalLDAPUtil;
33  import com.liferay.portal.service.CompanyServiceUtil;
34  import com.liferay.portal.service.RoleLocalServiceUtil;
35  import com.liferay.portal.servlet.filters.sso.cas.CASFilter;
36  import com.liferay.portal.struts.PortletAction;
37  import com.liferay.portal.util.PortalUtil;
38  import com.liferay.portal.util.PrefsPropsUtil;
39  import com.liferay.portal.util.PropsKeys;
40  import com.liferay.util.ldap.LDAPUtil;
41  
42  import javax.portlet.ActionRequest;
43  import javax.portlet.ActionResponse;
44  import javax.portlet.PortletConfig;
45  import javax.portlet.PortletPreferences;
46  
47  import org.apache.struts.action.ActionForm;
48  import org.apache.struts.action.ActionMapping;
49  
50  /**
51   * <a href="EditSettingsAction.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   * @author Scott Lee
55   *
56   */
57  public class EditSettingsAction extends PortletAction {
58  
59      public void processAction(
60              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
61              ActionRequest actionRequest, ActionResponse actionResponse)
62          throws Exception {
63  
64          long companyId = PortalUtil.getCompanyId(actionRequest);
65          long userId = PortalUtil.getUserId(actionRequest);
66  
67          if (!RoleLocalServiceUtil.hasUserRole(
68                  userId, companyId, RoleConstants.ADMINISTRATOR, true)) {
69  
70              SessionErrors.add(
71                  actionRequest, PrincipalException.class.getName());
72  
73              setForward(actionRequest, "portlet.enterprise_admin.error");
74  
75              return;
76          }
77  
78          PortletPreferences preferences = PrefsPropsUtil.getPreferences(
79              companyId);
80  
81          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
82  
83          if (cmd.equals("updateCAS")) {
84              updateCAS(actionRequest, companyId, preferences);
85          }
86          else if (cmd.equals("updateDefaultGroupsAndRoles")) {
87              updateDefaultGroupsAndRoles(actionRequest, preferences);
88          }
89          else if (cmd.equals("updateEmails")) {
90              updateEmails(actionRequest, preferences);
91          }
92          else if (cmd.equals("updateLdap")) {
93              updateLdap(actionRequest, companyId, preferences);
94          }
95          else if (cmd.equals("updateMailHostNames")) {
96              updateMailHostNames(actionRequest, preferences);
97          }
98          else if (cmd.equals("updateNtlm")) {
99              updateNtlm(actionRequest, companyId, preferences);
100         }
101         else if (cmd.equals("updateOpenId")) {
102             updateOpenId(actionRequest, preferences);
103         }
104         else if (cmd.equals("updateOpenSSO")) {
105             updateOpenSSO(actionRequest, companyId, preferences);
106         }
107         else if (cmd.equals("updateReservedUsers")) {
108             updateReservedUsers(actionRequest, preferences);
109         }
110         else if (cmd.equals("updateSecurity")) {
111             updateSecurity(actionRequest);
112         }
113         else if (cmd.equals("updateSiteMinder")) {
114             updateSiteMinder(actionRequest, companyId, preferences);
115         }
116 
117         if (SessionErrors.isEmpty(actionRequest)) {
118             sendRedirect(actionRequest, actionResponse);
119         }
120         else {
121             setForward(actionRequest, "portlet.enterprise_admin.view");
122         }
123     }
124 
125     protected void updateCAS(
126             ActionRequest actionRequest, long companyId,
127             PortletPreferences preferences)
128         throws Exception {
129 
130         boolean enabled = ParamUtil.getBoolean(actionRequest, "enabled");
131         boolean importFromLdap = ParamUtil.getBoolean(
132             actionRequest, "importFromLdap");
133         String loginUrl = ParamUtil.getString(actionRequest, "loginUrl");
134         String logoutUrl = ParamUtil.getString(actionRequest, "logoutUrl");
135         String serverName = ParamUtil.getString(actionRequest, "serverName");
136         String serviceUrl = ParamUtil.getString(actionRequest, "serviceUrl");
137         String validateUrl = ParamUtil.getString(actionRequest, "validateUrl");
138 
139         preferences.setValue(
140             PropsKeys.CAS_AUTH_ENABLED, String.valueOf(enabled));
141         preferences.setValue(
142             PropsKeys.CAS_IMPORT_FROM_LDAP, String.valueOf(importFromLdap));
143         preferences.setValue(PropsKeys.CAS_LOGIN_URL, loginUrl);
144         preferences.setValue(PropsKeys.CAS_LOGOUT_URL, logoutUrl);
145         preferences.setValue(PropsKeys.CAS_SERVER_NAME, serverName);
146         preferences.setValue(PropsKeys.CAS_SERVICE_URL, serviceUrl);
147         preferences.setValue(PropsKeys.CAS_VALIDATE_URL, validateUrl);
148 
149         preferences.store();
150 
151         CASFilter.reload(companyId);
152     }
153 
154     protected void updateDefaultGroupsAndRoles(
155             ActionRequest actionRequest, PortletPreferences preferences)
156         throws Exception {
157 
158         String defaultGroupNames = ParamUtil.getString(
159             actionRequest, "defaultGroupNames");
160         String defaultRoleNames = ParamUtil.getString(
161             actionRequest, "defaultRoleNames");
162         String defaultUserGroupNames = ParamUtil.getString(
163             actionRequest, "defaultUserGroupNames");
164 
165         preferences.setValue(
166             PropsKeys.ADMIN_DEFAULT_GROUP_NAMES, defaultGroupNames);
167         preferences.setValue(
168             PropsKeys.ADMIN_DEFAULT_ROLE_NAMES, defaultRoleNames);
169         preferences.setValue(
170             PropsKeys.ADMIN_DEFAULT_USER_GROUP_NAMES, defaultUserGroupNames);
171 
172         preferences.store();
173     }
174 
175     protected void updateEmails(
176             ActionRequest actionRequest, PortletPreferences preferences)
177         throws Exception {
178 
179         String tabs3 = ParamUtil.getString(actionRequest, "tabs3");
180 
181         if (tabs3.equals("account-created-notification")) {
182             String emailUserAddedEnabled = ParamUtil.getString(
183                 actionRequest, "emailUserAddedEnabled");
184             String emailUserAddedSubject = ParamUtil.getString(
185                 actionRequest, "emailUserAddedSubject");
186             String emailUserAddedBody = ParamUtil.getString(
187                 actionRequest, "emailUserAddedBody");
188 
189             if (Validator.isNull(emailUserAddedSubject)) {
190                 SessionErrors.add(actionRequest, "emailUserAddedSubject");
191             }
192             else if (Validator.isNull(emailUserAddedBody)) {
193                 SessionErrors.add(actionRequest, "emailUserAddedBody");
194             }
195             else {
196                 preferences.setValue(
197                     PropsKeys.ADMIN_EMAIL_USER_ADDED_ENABLED,
198                     emailUserAddedEnabled);
199                 preferences.setValue(
200                     PropsKeys.ADMIN_EMAIL_USER_ADDED_SUBJECT,
201                     emailUserAddedSubject);
202                 preferences.setValue(
203                     PropsKeys.ADMIN_EMAIL_USER_ADDED_BODY, emailUserAddedBody);
204             }
205         }
206         else if (tabs3.equals("password-changed-notification")) {
207             String emailPasswordSentEnabled = ParamUtil.getString(
208                 actionRequest, "emailPasswordSentEnabled");
209             String emailPasswordSentSubject = ParamUtil.getString(
210                 actionRequest, "emailPasswordSentSubject");
211             String emailPasswordSentBody = ParamUtil.getString(
212                 actionRequest, "emailPasswordSentBody");
213 
214             if (Validator.isNull(emailPasswordSentSubject)) {
215                 SessionErrors.add(actionRequest, "emailPasswordSentSubject");
216             }
217             else if (Validator.isNull(emailPasswordSentBody)) {
218                 SessionErrors.add(actionRequest, "emailPasswordSentBody");
219             }
220             else {
221                 preferences.setValue(
222                     PropsKeys.ADMIN_EMAIL_PASSWORD_SENT_ENABLED,
223                     emailPasswordSentEnabled);
224                 preferences.setValue(
225                     PropsKeys.ADMIN_EMAIL_PASSWORD_SENT_SUBJECT,
226                     emailPasswordSentSubject);
227                 preferences.setValue(
228                     PropsKeys.ADMIN_EMAIL_PASSWORD_SENT_BODY,
229                     emailPasswordSentBody);
230             }
231         }
232         else {
233             String emailFromName = ParamUtil.getString(
234                 actionRequest, "emailFromName");
235             String emailFromAddress = ParamUtil.getString(
236                 actionRequest, "emailFromAddress");
237 
238             if (Validator.isNull(emailFromName)) {
239                 SessionErrors.add(actionRequest, "emailFromName");
240             }
241             else if (!Validator.isEmailAddress(emailFromAddress)) {
242                 SessionErrors.add(actionRequest, "emailFromAddress");
243             }
244             else {
245                 preferences.setValue(
246                     PropsKeys.ADMIN_EMAIL_FROM_NAME, emailFromName);
247                 preferences.setValue(
248                     PropsKeys.ADMIN_EMAIL_FROM_ADDRESS, emailFromAddress);
249             }
250         }
251 
252         preferences.store();
253     }
254 
255     protected void updateLdap(
256             ActionRequest actionRequest, long companyId,
257             PortletPreferences preferences)
258         throws Exception {
259 
260         boolean enabled = ParamUtil.getBoolean(actionRequest, "enabled");
261         boolean required = ParamUtil.getBoolean(actionRequest, "required");
262         String baseProviderURL = ParamUtil.getString(
263             actionRequest, "baseProviderURL");
264         String baseDN = ParamUtil.getString(actionRequest, "baseDN");
265         String principal = ParamUtil.getString(actionRequest, "principal");
266         String credentials = ParamUtil.getString(actionRequest, "credentials");
267         String searchFilter = ParamUtil.getString(
268             actionRequest, "searchFilter");
269         String userDefaultObjectClasses = ParamUtil.getString(
270             actionRequest, "userDefaultObjectClasses");
271 
272         String userMappings =
273             "screenName=" +
274                 ParamUtil.getString(actionRequest, "userMappingScreenName") +
275             "\npassword=" +
276                 ParamUtil.getString(actionRequest, "userMappingPassword") +
277             "\nemailAddress=" +
278                 ParamUtil.getString(actionRequest, "userMappingEmailAddress") +
279             "\nfullName=" +
280                 ParamUtil.getString(actionRequest, "userMappingFullName") +
281             "\nfirstName=" +
282                 ParamUtil.getString(actionRequest, "userMappingFirstName") +
283             "\nlastName=" +
284                 ParamUtil.getString(actionRequest, "userMappingLastName") +
285             "\njobTitle=" +
286                 ParamUtil.getString(actionRequest, "userMappingJobTitle") +
287             "\ngroup=" + ParamUtil.getString(actionRequest, "userMappingGroup");
288 
289         String groupMappings =
290             "groupName=" +
291                 ParamUtil.getString(actionRequest, "groupMappingGroupName") +
292             "\ndescription=" +
293                 ParamUtil.getString(actionRequest, "groupMappingDescription") +
294             "\nuser=" + ParamUtil.getString(actionRequest, "groupMappingUser");
295 
296         boolean importEnabled = ParamUtil.getBoolean(
297             actionRequest, "importEnabled");
298         boolean importOnStartup = ParamUtil.getBoolean(
299             actionRequest, "importOnStartup");
300         long importInterval = ParamUtil.getLong(
301             actionRequest, "importInterval");
302         String importUserSearchFilter = ParamUtil.getString(
303             actionRequest, "importUserSearchFilter");
304         String importGroupSearchFilter = ParamUtil.getString(
305             actionRequest, "importGroupSearchFilter");
306         boolean exportEnabled = ParamUtil.getBoolean(
307             actionRequest, "exportEnabled");
308         String usersDN = ParamUtil.getString(actionRequest, "usersDN");
309         String groupsDN = ParamUtil.getString(actionRequest, "groupsDN");
310         boolean passwordPolicyEnabled = ParamUtil.getBoolean(
311             actionRequest, "passwordPolicyEnabled");
312 
313         try {
314             if (enabled) {
315                 String fullProviderURL = LDAPUtil.getFullProviderURL(
316                     baseProviderURL, baseDN);
317 
318                 PortalLDAPUtil.getContext(
319                     companyId, fullProviderURL, principal, credentials);
320             }
321         }
322         catch (Exception e) {
323             SessionErrors.add(actionRequest, "ldapAuthentication");
324 
325             return;
326         }
327 
328         preferences.setValue(
329             PropsKeys.LDAP_AUTH_ENABLED, String.valueOf(enabled));
330         preferences.setValue(
331             PropsKeys.LDAP_AUTH_REQUIRED, String.valueOf(required));
332         preferences.setValue(PropsKeys.LDAP_BASE_PROVIDER_URL, baseProviderURL);
333         preferences.setValue(PropsKeys.LDAP_BASE_DN, baseDN);
334         preferences.setValue(PropsKeys.LDAP_SECURITY_PRINCIPAL, principal);
335         preferences.setValue(PropsKeys.LDAP_SECURITY_CREDENTIALS, credentials);
336         preferences.setValue(PropsKeys.LDAP_AUTH_SEARCH_FILTER, searchFilter);
337         preferences.setValue(
338             PropsKeys.LDAP_USER_DEFAULT_OBJECT_CLASSES,
339             userDefaultObjectClasses);
340         preferences.setValue(PropsKeys.LDAP_USER_MAPPINGS, userMappings);
341         preferences.setValue(PropsKeys.LDAP_GROUP_MAPPINGS, groupMappings);
342         preferences.setValue(
343             PropsKeys.LDAP_IMPORT_ENABLED, String.valueOf(importEnabled));
344         preferences.setValue(
345             PropsKeys.LDAP_IMPORT_ON_STARTUP, String.valueOf(importOnStartup));
346         preferences.setValue(
347             PropsKeys.LDAP_IMPORT_INTERVAL, String.valueOf(importInterval));
348         preferences.setValue(
349             PropsKeys.LDAP_IMPORT_USER_SEARCH_FILTER, importUserSearchFilter);
350         preferences.setValue(
351             PropsKeys.LDAP_IMPORT_GROUP_SEARCH_FILTER, importGroupSearchFilter);
352         preferences.setValue(
353             PropsKeys.LDAP_EXPORT_ENABLED, String.valueOf(exportEnabled));
354         preferences.setValue(PropsKeys.LDAP_USERS_DN, usersDN);
355         preferences.setValue(PropsKeys.LDAP_GROUPS_DN, groupsDN);
356         preferences.setValue(
357             PropsKeys.LDAP_PASSWORD_POLICY_ENABLED,
358             String.valueOf(passwordPolicyEnabled));
359 
360         preferences.store();
361     }
362 
363     protected void updateMailHostNames(
364             ActionRequest actionRequest, PortletPreferences preferences)
365         throws Exception {
366 
367         String mailHostNames = ParamUtil.getString(
368             actionRequest, "mailHostNames");
369 
370         preferences.setValue(PropsKeys.ADMIN_MAIL_HOST_NAMES, mailHostNames);
371 
372         preferences.store();
373     }
374 
375     protected void updateNtlm(
376             ActionRequest actionRequest, long companyId,
377             PortletPreferences preferences)
378         throws Exception {
379 
380         boolean enabled = ParamUtil.getBoolean(actionRequest, "enabled");
381         String domainController = ParamUtil.getString(
382             actionRequest, "domainController");
383         String domain = ParamUtil.getString(actionRequest, "domain");
384 
385         preferences.setValue(
386             PropsKeys.NTLM_AUTH_ENABLED, String.valueOf(enabled));
387         preferences.setValue(
388             PropsKeys.NTLM_DOMAIN_CONTROLLER, domainController);
389         preferences.setValue(PropsKeys.NTLM_DOMAIN, domain);
390 
391         preferences.store();
392     }
393 
394     protected void updateOpenId(
395             ActionRequest actionRequest, PortletPreferences preferences)
396         throws Exception {
397 
398         boolean enabled = ParamUtil.getBoolean(actionRequest, "enabled");
399 
400         preferences.setValue(
401             PropsKeys.OPEN_ID_AUTH_ENABLED, String.valueOf(enabled));
402 
403         preferences.store();
404     }
405 
406     protected void updateOpenSSO(
407             ActionRequest actionRequest, long companyId,
408             PortletPreferences preferences)
409         throws Exception {
410 
411         boolean enabled = ParamUtil.getBoolean(actionRequest, "enabled");
412         String loginUrl = ParamUtil.getString(actionRequest, "loginUrl");
413         String logoutUrl = ParamUtil.getString(actionRequest, "logoutUrl");
414         String serviceUrl = ParamUtil.getString(actionRequest, "serviceUrl");
415         String screenName = ParamUtil.getString(
416             actionRequest, "screenNameAttr");
417         String emailAddress = ParamUtil.getString(
418             actionRequest, "emailAddressAttr");
419         String firstName = ParamUtil.getString(actionRequest, "firstNameAttr");
420         String lastName = ParamUtil.getString(actionRequest, "lastNameAttr");
421 
422         preferences.setValue(
423             PropsKeys.OPEN_SSO_AUTH_ENABLED, String.valueOf(enabled));
424         preferences.setValue(PropsKeys.OPEN_SSO_LOGIN_URL, loginUrl);
425         preferences.setValue(PropsKeys.OPEN_SSO_LOGOUT_URL, logoutUrl);
426         preferences.setValue(PropsKeys.OPEN_SSO_SERVICE_URL, serviceUrl);
427         preferences.setValue(PropsKeys.OPEN_SSO_SCREEN_NAME_ATTR, screenName);
428         preferences.setValue(
429             PropsKeys.OPEN_SSO_EMAIL_ADDRESS_ATTR, emailAddress);
430         preferences.setValue(PropsKeys.OPEN_SSO_FIRST_NAME_ATTR, firstName);
431         preferences.setValue(PropsKeys.OPEN_SSO_LAST_NAME_ATTR, lastName);
432 
433         preferences.store();
434     }
435 
436     protected void updateReservedUsers(
437             ActionRequest actionRequest, PortletPreferences preferences)
438         throws Exception {
439 
440         String reservedScreenNames = ParamUtil.getString(
441             actionRequest, "reservedScreenNames");
442         String reservedEmailAddresses = ParamUtil.getString(
443             actionRequest, "reservedEmailAddresses");
444 
445         preferences.setValue(
446             PropsKeys.ADMIN_RESERVED_SCREEN_NAMES, reservedScreenNames);
447         preferences.setValue(
448             PropsKeys.ADMIN_RESERVED_EMAIL_ADDRESSES, reservedEmailAddresses);
449 
450         preferences.store();
451     }
452 
453     protected void updateSecurity(ActionRequest actionRequest)
454         throws Exception {
455 
456         Company company = PortalUtil.getCompany(actionRequest);
457 
458         String authType = ParamUtil.getString(actionRequest, "authType");
459         boolean autoLogin = ParamUtil.getBoolean(actionRequest, "autoLogin");
460         boolean sendPassword = ParamUtil.getBoolean(
461             actionRequest, "sendPassword");
462         boolean strangers = ParamUtil.getBoolean(actionRequest, "strangers");
463         boolean strangersWithMx = ParamUtil.getBoolean(
464             actionRequest, "strangersWithMx");
465         boolean strangersVerify = ParamUtil.getBoolean(
466             actionRequest, "strangersVerify");
467 
468         CompanyServiceUtil.updateSecurity(
469             company.getCompanyId(), authType, autoLogin, sendPassword,
470             strangers, strangersWithMx, strangersVerify,
471             company.isCommunityLogo());
472     }
473 
474     protected void updateSiteMinder(
475             ActionRequest actionRequest, long companyId,
476             PortletPreferences preferences)
477         throws Exception {
478 
479         boolean enabled = ParamUtil.getBoolean(actionRequest, "enabled");
480         boolean importFromLdap = ParamUtil.getBoolean(
481             actionRequest, "importFromLdap");
482         String userHeader = ParamUtil.getString(actionRequest, "userHeader");
483 
484         preferences.setValue(
485             PropsKeys.SITEMINDER_AUTH_ENABLED, String.valueOf(enabled));
486         preferences.setValue(
487             PropsKeys.SITEMINDER_IMPORT_FROM_LDAP,
488             String.valueOf(importFromLdap));
489         preferences.setValue(PropsKeys.SITEMINDER_USER_HEADER, userHeader);
490 
491         preferences.store();
492     }
493 
494 }