1   /**
2    * Copyright (c) 2000-2008 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.impl.RoleImpl;
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, RoleImpl.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 prefs = PrefsPropsUtil.getPreferences(companyId);
79  
80          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
81  
82          if (cmd.equals("updateCAS")) {
83              updateCAS(actionRequest, companyId, prefs);
84          }
85          else if (cmd.equals("updateDefaultGroupsAndRoles")) {
86              updateDefaultGroupsAndRoles(actionRequest, prefs);
87          }
88          else if (cmd.equals("updateEmails")) {
89              updateEmails(actionRequest, prefs);
90          }
91          else if (cmd.equals("updateLdap")) {
92              updateLdap(actionRequest, companyId, prefs);
93          }
94          else if (cmd.equals("updateMailHostNames")) {
95              updateMailHostNames(actionRequest, prefs);
96          }
97          else if (cmd.equals("updateNtlm")) {
98              updateNtlm(actionRequest, companyId, prefs);
99          }
100         else if (cmd.equals("updateOpenId")) {
101             updateOpenId(actionRequest, prefs);
102         }
103         else if (cmd.equals("updateOpenSSO")) {
104             updateOpenSSO(actionRequest, companyId, prefs);
105         }
106         else if (cmd.equals("updateReservedUsers")) {
107             updateReservedUsers(actionRequest, prefs);
108         }
109         else if (cmd.equals("updateSecurity")) {
110             updateSecurity(actionRequest);
111         }
112 
113         if (SessionErrors.isEmpty(actionRequest)) {
114             if (!cmd.equals("updateLdap") && !cmd.equals("updateSecurity")) {
115                 prefs.store();
116             }
117 
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 prefs)
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         prefs.setValue(
140             PropsKeys.CAS_AUTH_ENABLED, String.valueOf(enabled));
141         prefs.setValue(
142             PropsKeys.CAS_IMPORT_FROM_LDAP, String.valueOf(importFromLdap));
143         prefs.setValue(PropsKeys.CAS_LOGIN_URL, loginUrl);
144         prefs.setValue(PropsKeys.CAS_LOGOUT_URL, logoutUrl);
145         prefs.setValue(PropsKeys.CAS_SERVER_NAME, serverName);
146         prefs.setValue(PropsKeys.CAS_SERVICE_URL, serviceUrl);
147         prefs.setValue(PropsKeys.CAS_VALIDATE_URL, validateUrl);
148 
149         prefs.store();
150 
151         CASFilter.reload(companyId);
152     }
153 
154     protected void updateDefaultGroupsAndRoles(
155             ActionRequest actionRequest, PortletPreferences prefs)
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         prefs.setValue(PropsKeys.ADMIN_DEFAULT_GROUP_NAMES, defaultGroupNames);
166         prefs.setValue(PropsKeys.ADMIN_DEFAULT_ROLE_NAMES, defaultRoleNames);
167         prefs.setValue(
168             PropsKeys.ADMIN_DEFAULT_USER_GROUP_NAMES, defaultUserGroupNames);
169     }
170 
171     protected void updateEmails(
172             ActionRequest actionRequest, PortletPreferences prefs)
173         throws Exception {
174 
175         String tabs3 = ParamUtil.getString(actionRequest, "tabs3");
176 
177         if (tabs3.equals("account-created-notification")) {
178             String emailUserAddedEnabled = ParamUtil.getString(
179                 actionRequest, "emailUserAddedEnabled");
180             String emailUserAddedSubject = ParamUtil.getString(
181                 actionRequest, "emailUserAddedSubject");
182             String emailUserAddedBody = ParamUtil.getString(
183                 actionRequest, "emailUserAddedBody");
184 
185             if (Validator.isNull(emailUserAddedSubject)) {
186                 SessionErrors.add(actionRequest, "emailUserAddedSubject");
187             }
188             else if (Validator.isNull(emailUserAddedBody)) {
189                 SessionErrors.add(actionRequest, "emailUserAddedBody");
190             }
191             else {
192                 prefs.setValue(
193                     PropsKeys.ADMIN_EMAIL_USER_ADDED_ENABLED,
194                     emailUserAddedEnabled);
195                 prefs.setValue(
196                     PropsKeys.ADMIN_EMAIL_USER_ADDED_SUBJECT,
197                     emailUserAddedSubject);
198                 prefs.setValue(
199                     PropsKeys.ADMIN_EMAIL_USER_ADDED_BODY, emailUserAddedBody);
200             }
201         }
202         else if (tabs3.equals("password-changed-notification")) {
203             String emailPasswordSentEnabled = ParamUtil.getString(
204                 actionRequest, "emailPasswordSentEnabled");
205             String emailPasswordSentSubject = ParamUtil.getString(
206                 actionRequest, "emailPasswordSentSubject");
207             String emailPasswordSentBody = ParamUtil.getString(
208                 actionRequest, "emailPasswordSentBody");
209 
210             if (Validator.isNull(emailPasswordSentSubject)) {
211                 SessionErrors.add(actionRequest, "emailPasswordSentSubject");
212             }
213             else if (Validator.isNull(emailPasswordSentBody)) {
214                 SessionErrors.add(actionRequest, "emailPasswordSentBody");
215             }
216             else {
217                 prefs.setValue(
218                     PropsKeys.ADMIN_EMAIL_PASSWORD_SENT_ENABLED,
219                     emailPasswordSentEnabled);
220                 prefs.setValue(
221                     PropsKeys.ADMIN_EMAIL_PASSWORD_SENT_SUBJECT,
222                     emailPasswordSentSubject);
223                 prefs.setValue(
224                     PropsKeys.ADMIN_EMAIL_PASSWORD_SENT_BODY,
225                     emailPasswordSentBody);
226             }
227         }
228         else {
229             String emailFromName = ParamUtil.getString(
230                 actionRequest, "emailFromName");
231             String emailFromAddress = ParamUtil.getString(
232                 actionRequest, "emailFromAddress");
233 
234             if (Validator.isNull(emailFromName)) {
235                 SessionErrors.add(actionRequest, "emailFromName");
236             }
237             else if (!Validator.isEmailAddress(emailFromAddress)) {
238                 SessionErrors.add(actionRequest, "emailFromAddress");
239             }
240             else {
241                 prefs.setValue(PropsKeys.ADMIN_EMAIL_FROM_NAME, emailFromName);
242                 prefs.setValue(
243                     PropsKeys.ADMIN_EMAIL_FROM_ADDRESS, emailFromAddress);
244             }
245         }
246     }
247 
248     protected void updateLdap(
249             ActionRequest actionRequest, long companyId,
250             PortletPreferences prefs)
251         throws Exception {
252 
253         boolean enabled = ParamUtil.getBoolean(actionRequest, "enabled");
254         boolean required = ParamUtil.getBoolean(actionRequest, "required");
255         String baseProviderURL = ParamUtil.getString(
256             actionRequest, "baseProviderURL");
257         String baseDN = ParamUtil.getString(actionRequest, "baseDN");
258         String principal = ParamUtil.getString(actionRequest, "principal");
259         String credentials = ParamUtil.getString(actionRequest, "credentials");
260         String searchFilter = ParamUtil.getString(
261             actionRequest, "searchFilter");
262         String userDefaultObjectClasses = ParamUtil.getString(
263             actionRequest, "userDefaultObjectClasses");
264 
265         String userMappings =
266             "screenName=" +
267                 ParamUtil.getString(actionRequest, "userMappingScreenName") +
268             "\npassword=" +
269                 ParamUtil.getString(actionRequest, "userMappingPassword") +
270             "\nemailAddress=" +
271                 ParamUtil.getString(actionRequest, "userMappingEmailAddress") +
272             "\nfullName=" +
273                 ParamUtil.getString(actionRequest, "userMappingFullName") +
274             "\nfirstName=" +
275                 ParamUtil.getString(actionRequest, "userMappingFirstName") +
276             "\nlastName=" +
277                 ParamUtil.getString(actionRequest, "userMappingLastName") +
278             "\njobTitle=" +
279                 ParamUtil.getString(actionRequest, "userMappingJobTitle") +
280             "\ngroup=" + ParamUtil.getString(actionRequest, "userMappingGroup");
281 
282         String groupMappings =
283             "groupName=" +
284                 ParamUtil.getString(actionRequest, "groupMappingGroupName") +
285             "\ndescription=" +
286                 ParamUtil.getString(actionRequest, "groupMappingDescription") +
287             "\nuser=" + ParamUtil.getString(actionRequest, "groupMappingUser");
288 
289         boolean importEnabled = ParamUtil.getBoolean(
290             actionRequest, "importEnabled");
291         boolean importOnStartup = ParamUtil.getBoolean(
292             actionRequest, "importOnStartup");
293         long importInterval = ParamUtil.getLong(
294             actionRequest, "importInterval");
295         String importUserSearchFilter = ParamUtil.getString(
296             actionRequest, "importUserSearchFilter");
297         String importGroupSearchFilter = ParamUtil.getString(
298             actionRequest, "importGroupSearchFilter");
299         boolean exportEnabled = ParamUtil.getBoolean(
300             actionRequest, "exportEnabled");
301         String usersDN = ParamUtil.getString(actionRequest, "usersDN");
302         String groupsDN = ParamUtil.getString(actionRequest, "groupsDN");
303         boolean passwordPolicyEnabled = ParamUtil.getBoolean(
304             actionRequest, "passwordPolicyEnabled");
305 
306         try {
307             if (enabled) {
308                 String fullProviderURL = LDAPUtil.getFullProviderURL(
309                     baseProviderURL, baseDN);
310 
311                 PortalLDAPUtil.getContext(
312                     companyId, fullProviderURL, principal, credentials);
313             }
314         }
315         catch (Exception e) {
316             SessionErrors.add(actionRequest, "ldapAuthentication");
317 
318             return;
319         }
320 
321         prefs.setValue(PropsKeys.LDAP_AUTH_ENABLED, String.valueOf(enabled));
322         prefs.setValue(PropsKeys.LDAP_AUTH_REQUIRED, String.valueOf(required));
323         prefs.setValue(PropsKeys.LDAP_BASE_PROVIDER_URL, baseProviderURL);
324         prefs.setValue(PropsKeys.LDAP_BASE_DN, baseDN);
325         prefs.setValue(PropsKeys.LDAP_SECURITY_PRINCIPAL, principal);
326         prefs.setValue(PropsKeys.LDAP_SECURITY_CREDENTIALS, credentials);
327         prefs.setValue(PropsKeys.LDAP_AUTH_SEARCH_FILTER, searchFilter);
328         prefs.setValue(
329             PropsKeys.LDAP_USER_DEFAULT_OBJECT_CLASSES,
330             userDefaultObjectClasses);
331         prefs.setValue(PropsKeys.LDAP_USER_MAPPINGS, userMappings);
332         prefs.setValue(PropsKeys.LDAP_GROUP_MAPPINGS, groupMappings);
333         prefs.setValue(
334             PropsKeys.LDAP_IMPORT_ENABLED, String.valueOf(importEnabled));
335         prefs.setValue(
336             PropsKeys.LDAP_IMPORT_ON_STARTUP, String.valueOf(importOnStartup));
337         prefs.setValue(
338             PropsKeys.LDAP_IMPORT_INTERVAL, String.valueOf(importInterval));
339         prefs.setValue(
340             PropsKeys.LDAP_IMPORT_USER_SEARCH_FILTER, importUserSearchFilter);
341         prefs.setValue(
342             PropsKeys.LDAP_IMPORT_GROUP_SEARCH_FILTER, importGroupSearchFilter);
343         prefs.setValue(
344             PropsKeys.LDAP_EXPORT_ENABLED, String.valueOf(exportEnabled));
345         prefs.setValue(PropsKeys.LDAP_USERS_DN, usersDN);
346         prefs.setValue(PropsKeys.LDAP_GROUPS_DN, groupsDN);
347         prefs.setValue(
348             PropsKeys.LDAP_PASSWORD_POLICY_ENABLED,
349             String.valueOf(passwordPolicyEnabled));
350 
351         prefs.store();
352     }
353 
354     protected void updateMailHostNames(
355             ActionRequest actionRequest, PortletPreferences prefs)
356         throws Exception {
357 
358         String mailHostNames = ParamUtil.getString(
359             actionRequest, "mailHostNames");
360 
361         prefs.setValue(PropsKeys.ADMIN_MAIL_HOST_NAMES, mailHostNames);
362     }
363 
364     protected void updateNtlm(
365             ActionRequest actionRequest, long companyId,
366             PortletPreferences prefs)
367         throws Exception {
368 
369         boolean enabled = ParamUtil.getBoolean(actionRequest, "enabled");
370         String domainController = ParamUtil.getString(
371             actionRequest, "domainController");
372         String domain = ParamUtil.getString(actionRequest, "domain");
373 
374         prefs.setValue(
375             PropsKeys.NTLM_AUTH_ENABLED, String.valueOf(enabled));
376         prefs.setValue(PropsKeys.NTLM_DOMAIN_CONTROLLER, domainController);
377         prefs.setValue(PropsKeys.NTLM_DOMAIN, domain);
378 
379         prefs.store();
380     }
381 
382     protected void updateOpenId(
383             ActionRequest actionRequest, PortletPreferences prefs)
384         throws Exception {
385 
386         boolean enabled = ParamUtil.getBoolean(actionRequest, "enabled");
387 
388         prefs.setValue(PropsKeys.OPEN_ID_AUTH_ENABLED, String.valueOf(enabled));
389 
390         prefs.store();
391     }
392 
393     protected void updateOpenSSO(
394             ActionRequest actionRequest, long companyId,
395             PortletPreferences prefs)
396         throws Exception {
397 
398         boolean enabled = ParamUtil.getBoolean(actionRequest, "enabled");
399         String loginUrl = ParamUtil.getString(actionRequest, "loginUrl");
400         String logoutUrl = ParamUtil.getString(actionRequest, "logoutUrl");
401         String serviceUrl = ParamUtil.getString(actionRequest, "serviceUrl");
402         String screenName = ParamUtil.getString(
403             actionRequest, "screenNameAttr");
404         String emailAddress = ParamUtil.getString(
405             actionRequest, "emailAddressAttr");
406         String firstName = ParamUtil.getString(actionRequest, "firstNameAttr");
407         String lastName = ParamUtil.getString(actionRequest, "lastNameAttr");
408 
409         prefs.setValue(
410             PropsKeys.OPEN_SSO_AUTH_ENABLED, String.valueOf(enabled));
411         prefs.setValue(PropsKeys.OPEN_SSO_LOGIN_URL, loginUrl);
412         prefs.setValue(PropsKeys.OPEN_SSO_LOGOUT_URL, logoutUrl);
413         prefs.setValue(PropsKeys.OPEN_SSO_SERVICE_URL, serviceUrl);
414         prefs.setValue(PropsKeys.OPEN_SSO_SCREEN_NAME_ATTR, screenName);
415         prefs.setValue(PropsKeys.OPEN_SSO_EMAIL_ADDRESS_ATTR, emailAddress);
416         prefs.setValue(PropsKeys.OPEN_SSO_FIRST_NAME_ATTR, firstName);
417         prefs.setValue(PropsKeys.OPEN_SSO_LAST_NAME_ATTR, lastName);
418 
419         prefs.store();
420     }
421 
422     protected void updateReservedUsers(
423             ActionRequest actionRequest, PortletPreferences prefs)
424         throws Exception {
425 
426         String reservedScreenNames = ParamUtil.getString(
427             actionRequest, "reservedScreenNames");
428         String reservedEmailAddresses = ParamUtil.getString(
429             actionRequest, "reservedEmailAddresses");
430 
431         prefs.setValue(
432             PropsKeys.ADMIN_RESERVED_SCREEN_NAMES, reservedScreenNames);
433         prefs.setValue(
434             PropsKeys.ADMIN_RESERVED_EMAIL_ADDRESSES, reservedEmailAddresses);
435     }
436 
437     protected void updateSecurity(ActionRequest actionRequest)
438         throws Exception {
439 
440         Company company = PortalUtil.getCompany(actionRequest);
441 
442         String authType = ParamUtil.getString(actionRequest, "authType");
443         boolean autoLogin = ParamUtil.getBoolean(actionRequest, "autoLogin");
444         boolean sendPassword = ParamUtil.getBoolean(
445             actionRequest, "sendPassword");
446         boolean strangers = ParamUtil.getBoolean(actionRequest, "strangers");
447         boolean strangersWithMx = ParamUtil.getBoolean(
448             actionRequest, "strangersWithMx");
449         boolean strangersVerify = ParamUtil.getBoolean(
450             actionRequest, "strangersVerify");
451 
452         CompanyServiceUtil.updateSecurity(
453             company.getCompanyId(), authType, autoLogin, sendPassword,
454             strangers, strangersWithMx, strangersVerify,
455             company.isCommunityLogo());
456     }
457 
458 }