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.DuplicatePasswordPolicyException;
26  import com.liferay.portal.NoSuchPasswordPolicyException;
27  import com.liferay.portal.PasswordPolicyNameException;
28  import com.liferay.portal.RequiredPasswordPolicyException;
29  import com.liferay.portal.kernel.servlet.SessionErrors;
30  import com.liferay.portal.kernel.util.Constants;
31  import com.liferay.portal.kernel.util.ParamUtil;
32  import com.liferay.portal.security.auth.PrincipalException;
33  import com.liferay.portal.service.PasswordPolicyServiceUtil;
34  import com.liferay.portal.struts.PortletAction;
35  
36  import javax.portlet.ActionRequest;
37  import javax.portlet.ActionResponse;
38  import javax.portlet.PortletConfig;
39  import javax.portlet.RenderRequest;
40  import javax.portlet.RenderResponse;
41  
42  import org.apache.struts.action.ActionForm;
43  import org.apache.struts.action.ActionForward;
44  import org.apache.struts.action.ActionMapping;
45  
46  /**
47   * <a href="EditPasswordPolicyAction.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Scott Lee
50   */
51  public class EditPasswordPolicyAction extends PortletAction {
52  
53      public void processAction(
54              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
55              ActionRequest actionRequest, ActionResponse actionResponse)
56          throws Exception {
57  
58          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
59  
60          try {
61              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
62                  updatePasswordPolicy(actionRequest);
63              }
64              else if (cmd.equals(Constants.DELETE)) {
65                  deletePasswordPolicy(actionRequest);
66              }
67  
68              sendRedirect(actionRequest, actionResponse);
69          }
70          catch (Exception e) {
71              if (e instanceof PrincipalException) {
72                  SessionErrors.add(actionRequest, e.getClass().getName());
73  
74                  setForward(actionRequest, "portlet.enterprise_admin.error");
75              }
76              else if (e instanceof DuplicatePasswordPolicyException ||
77                       e instanceof PasswordPolicyNameException ||
78                       e instanceof NoSuchPasswordPolicyException ||
79                       e instanceof RequiredPasswordPolicyException) {
80  
81                  SessionErrors.add(actionRequest, e.getClass().getName());
82  
83                  if (cmd.equals(Constants.DELETE)) {
84                      actionResponse.sendRedirect(
85                          ParamUtil.getString(actionRequest, "redirect"));
86                  }
87              }
88              else {
89                  throw e;
90              }
91          }
92      }
93  
94      public ActionForward render(
95              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
96              RenderRequest renderRequest, RenderResponse renderResponse)
97          throws Exception {
98  
99          try {
100             ActionUtil.getPasswordPolicy(renderRequest);
101         }
102         catch (Exception e) {
103             if (e instanceof NoSuchPasswordPolicyException ||
104                 e instanceof PrincipalException) {
105 
106                 SessionErrors.add(renderRequest, e.getClass().getName());
107 
108                 return mapping.findForward("portlet.enterprise_admin.error");
109             }
110             else {
111                 throw e;
112             }
113         }
114 
115         return mapping.findForward(getForward(
116             renderRequest, "portlet.enterprise_admin.edit_password_policy"));
117     }
118 
119     protected void deletePasswordPolicy(ActionRequest actionRequest)
120         throws Exception {
121 
122         long passwordPolicyId = ParamUtil.getLong(
123             actionRequest, "passwordPolicyId");
124 
125         PasswordPolicyServiceUtil.deletePasswordPolicy(passwordPolicyId);
126     }
127 
128     protected void updatePasswordPolicy(ActionRequest actionRequest)
129         throws Exception {
130 
131         long passwordPolicyId = ParamUtil.getLong(
132             actionRequest, "passwordPolicyId");
133 
134         String name = ParamUtil.getString(actionRequest, "name");
135         String description = ParamUtil.getString(actionRequest, "description");
136         boolean changeable = ParamUtil.getBoolean(actionRequest, "changeable");
137         boolean changeRequired = ParamUtil.getBoolean(
138             actionRequest, "changeRequired");
139         long minAge = ParamUtil.getLong(actionRequest, "minAge");
140         boolean checkSyntax = ParamUtil.getBoolean(
141             actionRequest, "checkSyntax");
142         boolean allowDictionaryWords = ParamUtil.getBoolean(
143             actionRequest, "allowDictionaryWords");
144         int minLength = ParamUtil.getInteger(actionRequest, "minLength");
145         boolean history = ParamUtil.getBoolean(actionRequest, "history");
146         int historyCount = ParamUtil.getInteger(actionRequest, "historyCount");
147         boolean expireable = ParamUtil.getBoolean(actionRequest, "expireable");
148         long maxAge = ParamUtil.getLong(actionRequest, "maxAge");
149         long warningTime = ParamUtil.getLong(actionRequest, "warningTime");
150         int graceLimit = ParamUtil.getInteger(actionRequest, "graceLimit");
151         boolean lockout = ParamUtil.getBoolean(actionRequest, "lockout");
152         int maxFailure = ParamUtil.getInteger(actionRequest, "maxFailure");
153         long lockoutDuration = ParamUtil.getLong(
154             actionRequest, "lockoutDuration");
155         long resetFailureCount = ParamUtil.getLong(
156             actionRequest, "resetFailureCount");
157 
158         if (passwordPolicyId <= 0) {
159 
160             // Add password policy
161 
162             PasswordPolicyServiceUtil.addPasswordPolicy(
163                 name, description, changeable, changeRequired, minAge,
164                 checkSyntax, allowDictionaryWords, minLength, history,
165                 historyCount, expireable, maxAge, warningTime, graceLimit,
166                 lockout, maxFailure, lockoutDuration, resetFailureCount);
167         }
168         else {
169 
170             // Update password policy
171 
172             PasswordPolicyServiceUtil.updatePasswordPolicy(
173                 passwordPolicyId, name, description, changeable, changeRequired,
174                 minAge, checkSyntax, allowDictionaryWords, minLength, history,
175                 historyCount, expireable, maxAge, warningTime, graceLimit,
176                 lockout, maxFailure, lockoutDuration, resetFailureCount);
177         }
178     }
179 
180 }