1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.enterpriseadmin.action;
16  
17  import com.liferay.portal.DuplicatePasswordPolicyException;
18  import com.liferay.portal.NoSuchPasswordPolicyException;
19  import com.liferay.portal.PasswordPolicyNameException;
20  import com.liferay.portal.RequiredPasswordPolicyException;
21  import com.liferay.portal.kernel.servlet.SessionErrors;
22  import com.liferay.portal.kernel.util.Constants;
23  import com.liferay.portal.kernel.util.ParamUtil;
24  import com.liferay.portal.security.auth.PrincipalException;
25  import com.liferay.portal.service.PasswordPolicyServiceUtil;
26  import com.liferay.portal.struts.PortletAction;
27  
28  import javax.portlet.ActionRequest;
29  import javax.portlet.ActionResponse;
30  import javax.portlet.PortletConfig;
31  import javax.portlet.RenderRequest;
32  import javax.portlet.RenderResponse;
33  
34  import org.apache.struts.action.ActionForm;
35  import org.apache.struts.action.ActionForward;
36  import org.apache.struts.action.ActionMapping;
37  
38  /**
39   * <a href="EditPasswordPolicyAction.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Scott Lee
42   */
43  public class EditPasswordPolicyAction extends PortletAction {
44  
45      public void processAction(
46              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
47              ActionRequest actionRequest, ActionResponse actionResponse)
48          throws Exception {
49  
50          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
51  
52          try {
53              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
54                  updatePasswordPolicy(actionRequest);
55              }
56              else if (cmd.equals(Constants.DELETE)) {
57                  deletePasswordPolicy(actionRequest);
58              }
59  
60              sendRedirect(actionRequest, actionResponse);
61          }
62          catch (Exception e) {
63              if (e instanceof PrincipalException) {
64                  SessionErrors.add(actionRequest, e.getClass().getName());
65  
66                  setForward(actionRequest, "portlet.enterprise_admin.error");
67              }
68              else if (e instanceof DuplicatePasswordPolicyException ||
69                       e instanceof PasswordPolicyNameException ||
70                       e instanceof NoSuchPasswordPolicyException ||
71                       e instanceof RequiredPasswordPolicyException) {
72  
73                  SessionErrors.add(actionRequest, e.getClass().getName());
74  
75                  if (cmd.equals(Constants.DELETE)) {
76                      actionResponse.sendRedirect(
77                          ParamUtil.getString(actionRequest, "redirect"));
78                  }
79              }
80              else {
81                  throw e;
82              }
83          }
84      }
85  
86      public ActionForward render(
87              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
88              RenderRequest renderRequest, RenderResponse renderResponse)
89          throws Exception {
90  
91          try {
92              ActionUtil.getPasswordPolicy(renderRequest);
93          }
94          catch (Exception e) {
95              if (e instanceof NoSuchPasswordPolicyException ||
96                  e instanceof PrincipalException) {
97  
98                  SessionErrors.add(renderRequest, e.getClass().getName());
99  
100                 return mapping.findForward("portlet.enterprise_admin.error");
101             }
102             else {
103                 throw e;
104             }
105         }
106 
107         return mapping.findForward(getForward(
108             renderRequest, "portlet.enterprise_admin.edit_password_policy"));
109     }
110 
111     protected void deletePasswordPolicy(ActionRequest actionRequest)
112         throws Exception {
113 
114         long passwordPolicyId = ParamUtil.getLong(
115             actionRequest, "passwordPolicyId");
116 
117         PasswordPolicyServiceUtil.deletePasswordPolicy(passwordPolicyId);
118     }
119 
120     protected void updatePasswordPolicy(ActionRequest actionRequest)
121         throws Exception {
122 
123         long passwordPolicyId = ParamUtil.getLong(
124             actionRequest, "passwordPolicyId");
125 
126         String name = ParamUtil.getString(actionRequest, "name");
127         String description = ParamUtil.getString(actionRequest, "description");
128         boolean changeable = ParamUtil.getBoolean(actionRequest, "changeable");
129         boolean changeRequired = ParamUtil.getBoolean(
130             actionRequest, "changeRequired");
131         long minAge = ParamUtil.getLong(actionRequest, "minAge");
132         boolean checkSyntax = ParamUtil.getBoolean(
133             actionRequest, "checkSyntax");
134         boolean allowDictionaryWords = ParamUtil.getBoolean(
135             actionRequest, "allowDictionaryWords");
136         int minAlphanumeric = ParamUtil.getInteger(
137             actionRequest, "minAlphanumeric");
138         int minLength = ParamUtil.getInteger(actionRequest, "minLength");
139         int minLowerCase = ParamUtil.getInteger(actionRequest, "minLowerCase");
140         int minNumbers = ParamUtil.getInteger(actionRequest, "minNumbers");
141         int minSymbols = ParamUtil.getInteger(actionRequest, "minSymbols");
142         int minUpperCase = ParamUtil.getInteger(actionRequest, "minUpperCase");
143         boolean history = ParamUtil.getBoolean(actionRequest, "history");
144         int historyCount = ParamUtil.getInteger(actionRequest, "historyCount");
145         boolean expireable = ParamUtil.getBoolean(actionRequest, "expireable");
146         long maxAge = ParamUtil.getLong(actionRequest, "maxAge");
147         long warningTime = ParamUtil.getLong(actionRequest, "warningTime");
148         int graceLimit = ParamUtil.getInteger(actionRequest, "graceLimit");
149         boolean lockout = ParamUtil.getBoolean(actionRequest, "lockout");
150         int maxFailure = ParamUtil.getInteger(actionRequest, "maxFailure");
151         long lockoutDuration = ParamUtil.getLong(
152             actionRequest, "lockoutDuration");
153         long resetFailureCount = ParamUtil.getLong(
154             actionRequest, "resetFailureCount");
155         long resetTicketMaxAge = ParamUtil.getLong(
156             actionRequest, "resetTicketMaxAge");
157 
158         if (passwordPolicyId <= 0) {
159 
160             // Add password policy
161 
162             PasswordPolicyServiceUtil.addPasswordPolicy(
163                 name, description, changeable, changeRequired, minAge,
164                 checkSyntax, allowDictionaryWords, minAlphanumeric, minLength,
165                 minLowerCase, minNumbers, minSymbols, minUpperCase, history,
166                 historyCount, expireable, maxAge, warningTime, graceLimit,
167                 lockout, maxFailure, lockoutDuration, resetFailureCount,
168                 resetTicketMaxAge);
169         }
170         else {
171 
172             // Update password policy
173 
174             PasswordPolicyServiceUtil.updatePasswordPolicy(
175                 passwordPolicyId, name, description, changeable, changeRequired,
176                 minAge, checkSyntax, allowDictionaryWords, minAlphanumeric,
177                 minLength, minLowerCase, minNumbers, minSymbols, minUpperCase,
178                 history, historyCount, expireable, maxAge, warningTime,
179                 graceLimit, lockout, maxFailure, lockoutDuration,
180                 resetFailureCount, resetTicketMaxAge);
181         }
182     }
183 
184 }