1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class EditSettingsAction extends PortletAction {
57  
58      public void processAction(
59              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
60              ActionRequest actionRequest, ActionResponse actionResponse)
61          throws Exception {
62  
63          long companyId = PortalUtil.getCompanyId(actionRequest);
64          long userId = PortalUtil.getUserId(actionRequest);
65  
66          if (!RoleLocalServiceUtil.hasUserRole(
67                  userId, companyId, RoleConstants.ADMINISTRATOR, true)) {
68  
69              SessionErrors.add(
70                  actionRequest, PrincipalException.class.getName());
71  
72              setForward(actionRequest, "portlet.enterprise_admin.error");
73  
74              return;
75          }
76  
77          PortletPreferences prefs = PrefsPropsUtil.getPreferences(companyId);
78  
79          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
80  
81          if (cmd.equals("updateCAS")) {
82              updateCAS(actionRequest, companyId, prefs);
83          }
84          else if (cmd.equals("updateDefaultGroupsAndRoles")) {
85              updateDefaultGroupsAndRoles(actionRequest, prefs);
86          }
87          else if (cmd.equals("updateEmails")) {
88              updateEmails(actionRequest, prefs);
89          }
90          else if (cmd.equals("updateLdap")) {
91              updateLdap(actionRequest, companyId, prefs);
92          }
93          else if (cmd.equals("updateMailHostNames")) {
94              updateMailHostNames(actionRequest, prefs);
95          }
96          else if (cmd.equals("updateNtlm")) {
97              updateNtlm(actionRequest, companyId, prefs);
98          }
99          else if (cmd.equals("updateOpenId")) {
100             updateOpenId(actionRequest, prefs);
101         }
102         else if (cmd.equals("updateOpenSSO")) {
103             updateOpenSSO(actionRequest, companyId, prefs);
104         }
105         else if (cmd.equals("updateReservedUsers")) {
106             updateReservedUsers(actionRequest, prefs);
107         }
108         else if (cmd.equals("updateSecurity")) {
109             updateSecurity(actionRequest);
110         }
111         else if (cmd.equals("updateSiteMinder")) {
112             updateSiteMinder(actionRequest, companyId, prefs);
113         }
114 
115         if (SessionErrors.isEmpty(actionRequest)) {
116             if (!cmd.equals("updateLdap") && !cmd.equals("updateSecurity")) {
117                 prefs.store();
118             }
119 
120             sendRedirect(actionRequest, actionResponse);
121         }
122         else {
123             setForward(actionRequest, "portlet.enterprise_admin.view");
124         }
125     }
126 
127     protected void updateCAS(
128             ActionRequest actionRequest, long companyId,
129             PortletPreferences prefs)
130         throws Exception {
131 
132         boolean enabled = ParamUtil.getBoolean(actionRequest, "enabled");
133         boolean importFromLdap = ParamUtil.getBoolean(
134             actionRequest, "importFromLdap");
135         String loginUrl = ParamUtil.getString(actionRequest, "loginUrl");
136         String logoutUrl = ParamUtil.getString(actionRequest, "logoutUrl");
137         String serverName = ParamUtil.getString(actionRequest, "serverName");
138         String serviceUrl = ParamUtil.getString(actionRequest, "serviceUrl");
139         String validateUrl = ParamUtil.getString(actionRequest, "validateUrl");
140 
141         prefs.setValue(
142             PropsKeys.CAS_AUTH_ENABLED, String.valueOf(enabled));
143         prefs.setValue(
144             PropsKeys.CAS_IMPORT_FROM_LDAP, String.valueOf(importFromLdap));
145         prefs.setValue(PropsKeys.CAS_LOGIN_URL, loginUrl);
146         prefs.setValue(PropsKeys.CAS_LOGOUT_URL, logoutUrl);
147         prefs.setValue(PropsKeys.CAS_SERVER_NAME, serverName);
148         prefs.setValue(PropsKeys.CAS_SERVICE_URL, serviceUrl);
149         prefs.setValue(PropsKeys.CAS_VALIDATE_URL, validateUrl);
150 
151         prefs.store();
152 
153         CASFilter.reload(companyId);
154     }
155 
156     protected void updateDefaultGroupsAndRoles(
157             ActionRequest actionRequest, PortletPreferences prefs)
158         throws Exception {
159 
160         String defaultGroupNames = ParamUtil.getString(
161             actionRequest, "defaultGroupNames");
162         String defaultRoleNames = ParamUtil.getString(
163             actionRequest, "defaultRoleNames");
164         String defaultUserGroupNames = ParamUtil.getString(
165             actionRequest, "defaultUserGroupNames");
166 
167         prefs.setValue(PropsKeys.ADMIN_DEFAULT_GROUP_NAMES, defaultGroupNames);
168         prefs.setValue(PropsKeys.ADMIN_DEFAULT_ROLE_NAMES, defaultRoleNames);
169         prefs.setValue(
170             PropsKeys.ADMIN_DEFAULT_USER_GROUP_NAMES, defaultUserGroupNames);
171     }
172 
173     protected void updateEmails(
174             ActionRequest actionRequest, PortletPreferences prefs)
175         throws Exception {
176 
177         String tabs3 = ParamUtil.getString(actionRequest, "tabs3");
178 
179         if (tabs3.equals("account-created-notification")) {
180             String emailUserAddedEnabled = ParamUtil.getString(
181                 actionRequest, "emailUserAddedEnabled");
182             String emailUserAddedSubject = ParamUtil.getString(
183                 actionRequest, "emailUserAddedSubject");
184             String emailUserAddedBody = ParamUtil.getString(
185                 actionRequest, "emailUserAddedBody");
186 
187             if (Validator.isNull(emailUserAddedSubject)) {
188                 SessionErrors.add(actionRequest, "emailUserAddedSubject");
189             }
190             else if (Validator.isNull(emailUserAddedBody)) {
191                 SessionErrors.add(actionRequest, "emailUserAddedBody");
192             }
193             else {
194                 prefs.setValue(
195                     PropsKeys.ADMIN_EMAIL_USER_ADDED_ENABLED,
196                     emailUserAddedEnabled);
197                 prefs.setValue(
198                     PropsKeys.ADMIN_EMAIL_USER_ADDED_SUBJECT,
199                     emailUserAddedSubject);
200                 prefs.setValue(
201                     PropsKeys.ADMIN_EMAIL_USER_ADDED_BODY, emailUserAddedBody);
202             }
203         }
204         else if (tabs3.equals("password-changed-notification")) {
205             String emailPasswordSentEnabled = ParamUtil.getString(
206                 actionRequest, "emailPasswordSentEnabled");
207             String emailPasswordSentSubject = ParamUtil.getString(
208                 actionRequest, "emailPasswordSentSubject");
209             String emailPasswordSentBody = ParamUtil.getString(
210                 actionRequest, "emailPasswordSentBody");
211 
212             if (Validator.isNull(emailPasswordSentSubject)) {
213                 SessionErrors.add(actionRequest, "emailPasswordSentSubject");
214             }
215             else if (Validator.isNull(emailPasswordSentBody)) {
216                 SessionErrors.add(actionRequest, "emailPasswordSentBody");
217             }
218             else {
219                 prefs.setValue(
220                     PropsKeys.ADMIN_EMAIL_PASSWORD_SENT_ENABLED,
221                     emailPasswordSentEnabled);
222                 prefs.setValue(
223                     PropsKeys.ADMIN_EMAIL_PASSWORD_SENT_SUBJECT,
224                     emailPasswordSentSubject);
225                 prefs.setValue(
226                     PropsKeys.ADMIN_EMAIL_PASSWORD_SENT_BODY,
227                     emailPasswordSentBody);
228             }
229         }
230         else {
231             String emailFromName = ParamUtil.getString(
232                 actionRequest, "emailFromName");
233             String emailFromAddress = ParamUtil.getString(
234                 actionRequest, "emailFromAddress");
235 
236             if (Validator.isNull(emailFromName)) {
237                 SessionErrors.add(actionRequest, "emailFromName");
238             }
239             else if (!Validator.isEmailAddress(emailFromAddress)) {
240                 SessionErrors.add(actionRequest, "emailFromAddress");
241             }
242             else {
243                 prefs.setValue(PropsKeys.ADMIN_EMAIL_FROM_NAME, emailFromName);
244                 prefs.setValue(
245                     PropsKeys.ADMIN_EMAIL_FROM_ADDRESS, emailFromAddress);
246             }
247         }
248     }
249 
250     protected void updateLdap(
251             ActionRequest actionRequest, long companyId,
252             PortletPreferences prefs)
253         throws Exception {
254 
255         boolean enabled = ParamUtil.getBoolean(actionRequest, "enabled");
256         boolean required = ParamUtil.getBoolean(actionRequest, "required");
257         String baseProviderURL = ParamUtil.getString(
258             actionRequest, "baseProviderURL");
259         String baseDN = ParamUtil.getString(actionRequest, "baseDN");
260         String principal = ParamUtil.getString(actionRequest, "principal");
261         String credentials = ParamUtil.getString(actionRequest, "credentials");
262         String searchFilter = ParamUtil.getString(
263             actionRequest, "searchFilter");
264         String userDefaultObjectClasses = ParamUtil.getString(
265             actionRequest, "userDefaultObjectClasses");
266 
267         String userMappings =
268             "screenName=" +
269                 ParamUtil.getString(actionRequest, "userMappingScreenName") +
270             "\npassword=" +
271                 ParamUtil.getString(actionRequest, "userMappingPassword") +
272             "\nemailAddress=" +
273                 ParamUtil.getString(actionRequest, "userMappingEmailAddress") +
274             "\nfullName=" +
275                 ParamUtil.getString(actionRequest, "userMappingFullName") +
276             "\nfirstName=" +
277                 ParamUtil.getString(actionRequest, "userMappingFirstName") +
278             "\nlastName=" +
279                 ParamUtil.getString(actionRequest, "userMappingLastName") +
280             "\njobTitle=" +
281                 ParamUtil.getString(actionRequest, "userMappingJobTitle") +
282             "\ngroup=" + ParamUtil.getString(actionRequest, "userMappingGroup");
283 
284         String groupMappings =
285             "groupName=" +
286                 ParamUtil.getString(actionRequest, "groupMappingGroupName") +
287             "\ndescription=" +
288                 ParamUtil.getString(actionRequest, "groupMappingDescription") +
289             "\nuser=" + ParamUtil.getString(actionRequest, "groupMappingUser");
290 
291         boolean importEnabled = ParamUtil.getBoolean(
292             actionRequest, "importEnabled");
293         boolean importOnStartup = ParamUtil.getBoolean(
294             actionRequest, "importOnStartup");
295         long importInterval = ParamUtil.getLong(
296             actionRequest, "importInterval");
297         String importUserSearchFilter = ParamUtil.getString(
298             actionRequest, "importUserSearchFilter");
299         String importGroupSearchFilter = ParamUtil.getString(
300             actionRequest, "importGroupSearchFilter");
301         boolean exportEnabled = ParamUtil.getBoolean(
302             actionRequest, "exportEnabled");
303         String usersDN = ParamUtil.getString(actionRequest, "usersDN");
304         String groupsDN = ParamUtil.getString(actionRequest, "groupsDN");
305         boolean passwordPolicyEnabled = ParamUtil.getBoolean(
306             actionRequest, "passwordPolicyEnabled");
307 
308         try {
309             if (enabled) {
310                 String fullProviderURL = LDAPUtil.getFullProviderURL(
311                     baseProviderURL, baseDN);
312 
313                 PortalLDAPUtil.getContext(
314                     companyId, fullProviderURL, principal, credentials);
315             }
316         }
317         catch (Exception e) {
318             SessionErrors.add(actionRequest, "ldapAuthentication");
319 
320             return;
321         }
322 
323         prefs.setValue(PropsKeys.LDAP_AUTH_ENABLED, String.valueOf(enabled));
324         prefs.setValue(PropsKeys.LDAP_AUTH_REQUIRED, String.valueOf(required));
325         prefs.setValue(PropsKeys.LDAP_BASE_PROVIDER_URL, baseProviderURL);
326         prefs.setValue(PropsKeys.LDAP_BASE_DN, baseDN);
327         prefs.setValue(PropsKeys.LDAP_SECURITY_PRINCIPAL, principal);
328         prefs.setValue(PropsKeys.LDAP_SECURITY_CREDENTIALS, credentials);
329         prefs.setValue(PropsKeys.LDAP_AUTH_SEARCH_FILTER, searchFilter);
330         prefs.setValue(
331             PropsKeys.LDAP_USER_DEFAULT_OBJECT_CLASSES,
332             userDefaultObjectClasses);
333         prefs.setValue(PropsKeys.LDAP_USER_MAPPINGS, userMappings);
334         prefs.setValue(PropsKeys.LDAP_GROUP_MAPPINGS, groupMappings);
335         prefs.setValue(
336             PropsKeys.LDAP_IMPORT_ENABLED, String.valueOf(importEnabled));
337         prefs.setValue(
338             PropsKeys.LDAP_IMPORT_ON_STARTUP, String.valueOf(importOnStartup));
339         prefs.setValue(
340             PropsKeys.LDAP_IMPORT_INTERVAL, String.valueOf(importInterval));
341         prefs.setValue(
342             PropsKeys.LDAP_IMPORT_USER_SEARCH_FILTER, importUserSearchFilter);
343         prefs.setValue(
344             PropsKeys.LDAP_IMPORT_GROUP_SEARCH_FILTER, importGroupSearchFilter);
345         prefs.setValue(
346             PropsKeys.LDAP_EXPORT_ENABLED, String.valueOf(exportEnabled));
347         prefs.setValue(PropsKeys.LDAP_USERS_DN, usersDN);
348         prefs.setValue(PropsKeys.LDAP_GROUPS_DN, groupsDN);
349         prefs.setValue(
350             PropsKeys.LDAP_PASSWORD_POLICY_ENABLED,
351             String.valueOf(passwordPolicyEnabled));
352 
353         prefs.store();
354     }
355 
356     protected void updateMailHostNames(
357             ActionRequest actionRequest, PortletPreferences prefs)
358         throws Exception {
359 
360         String mailHostNames = ParamUtil.getString(
361             actionRequest, "mailHostNames");
362 
363         prefs.setValue(PropsKeys.ADMIN_MAIL_HOST_NAMES, mailHostNames);
364     }
365 
366     protected void updateNtlm(
367             ActionRequest actionRequest, long companyId,
368             PortletPreferences prefs)
369         throws Exception {
370 
371         boolean enabled = ParamUtil.getBoolean(actionRequest, "enabled");
372         String domainController = ParamUtil.getString(
373             actionRequest, "domainController");
374         String domain = ParamUtil.getString(actionRequest, "domain");
375 
376         prefs.setValue(
377             PropsKeys.NTLM_AUTH_ENABLED, String.valueOf(enabled));
378         prefs.setValue(PropsKeys.NTLM_DOMAIN_CONTROLLER, domainController);
379         prefs.setValue(PropsKeys.NTLM_DOMAIN, domain);
380 
381         prefs.store();
382     }
383 
384     protected void updateOpenId(
385             ActionRequest actionRequest, PortletPreferences prefs)
386         throws Exception {
387 
388         boolean enabled = ParamUtil.getBoolean(actionRequest, "enabled");
389 
390         prefs.setValue(PropsKeys.OPEN_ID_AUTH_ENABLED, String.valueOf(enabled));
391 
392         prefs.store();
393     }
394 
395     protected void updateOpenSSO(
396             ActionRequest actionRequest, long companyId,
397             PortletPreferences prefs)
398         throws Exception {
399 
400         boolean enabled = ParamUtil.getBoolean(actionRequest, "enabled");
401         String loginUrl = ParamUtil.getString(actionRequest, "loginUrl");
402         String logoutUrl = ParamUtil.getString(actionRequest, "logoutUrl");
403         String serviceUrl = ParamUtil.getString(actionRequest, "serviceUrl");
404         String screenName = ParamUtil.getString(
405             actionRequest, "screenNameAttr");
406         String emailAddress = ParamUtil.getString(
407             actionRequest, "emailAddressAttr");
408         String firstName = ParamUtil.getString(actionRequest, "firstNameAttr");
409         String lastName = ParamUtil.getString(actionRequest, "lastNameAttr");
410 
411         prefs.setValue(
412             PropsKeys.OPEN_SSO_AUTH_ENABLED, String.valueOf(enabled));
413         prefs.setValue(PropsKeys.OPEN_SSO_LOGIN_URL, loginUrl);
414         prefs.setValue(PropsKeys.OPEN_SSO_LOGOUT_URL, logoutUrl);
415         prefs.setValue(PropsKeys.OPEN_SSO_SERVICE_URL, serviceUrl);
416         prefs.setValue(PropsKeys.OPEN_SSO_SCREEN_NAME_ATTR, screenName);
417         prefs.setValue(PropsKeys.OPEN_SSO_EMAIL_ADDRESS_ATTR, emailAddress);
418         prefs.setValue(PropsKeys.OPEN_SSO_FIRST_NAME_ATTR, firstName);
419         prefs.setValue(PropsKeys.OPEN_SSO_LAST_NAME_ATTR, lastName);
420 
421         prefs.store();
422     }
423 
424     protected void updateReservedUsers(
425             ActionRequest actionRequest, PortletPreferences prefs)
426         throws Exception {
427 
428         String reservedScreenNames = ParamUtil.getString(
429             actionRequest, "reservedScreenNames");
430         String reservedEmailAddresses = ParamUtil.getString(
431             actionRequest, "reservedEmailAddresses");
432 
433         prefs.setValue(
434             PropsKeys.ADMIN_RESERVED_SCREEN_NAMES, reservedScreenNames);
435         prefs.setValue(
436             PropsKeys.ADMIN_RESERVED_EMAIL_ADDRESSES, reservedEmailAddresses);
437     }
438 
439     protected void updateSecurity(ActionRequest actionRequest)
440         throws Exception {
441 
442         Company company = PortalUtil.getCompany(actionRequest);
443 
444         String authType = ParamUtil.getString(actionRequest, "authType");
445         boolean autoLogin = ParamUtil.getBoolean(actionRequest, "autoLogin");
446         boolean sendPassword = ParamUtil.getBoolean(
447             actionRequest, "sendPassword");
448         boolean strangers = ParamUtil.getBoolean(actionRequest, "strangers");
449         boolean strangersWithMx = ParamUtil.getBoolean(
450             actionRequest, "strangersWithMx");
451         boolean strangersVerify = ParamUtil.getBoolean(
452             actionRequest, "strangersVerify");
453 
454         CompanyServiceUtil.updateSecurity(
455             company.getCompanyId(), authType, autoLogin, sendPassword,
456             strangers, strangersWithMx, strangersVerify,
457             company.isCommunityLogo());
458     }
459 
460     protected void updateSiteMinder(
461             ActionRequest actionRequest, long companyId,
462             PortletPreferences prefs)
463         throws Exception {
464 
465         boolean enabled = ParamUtil.getBoolean(actionRequest, "enabled");
466         boolean importFromLdap = ParamUtil.getBoolean(
467             actionRequest, "importFromLdap");
468         String userHeader = ParamUtil.getString(actionRequest, "userHeader");
469 
470         prefs.setValue(
471             PropsKeys.SITEMINDER_AUTH_ENABLED, String.valueOf(enabled));
472         prefs.setValue(
473             PropsKeys.SITEMINDER_IMPORT_FROM_LDAP,
474             String.valueOf(importFromLdap));
475         prefs.setValue(PropsKeys.SITEMINDER_USER_HEADER, userHeader);
476 
477         prefs.store();
478     }
479 
480 }