1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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 minLength = ParamUtil.getInteger(actionRequest, "minLength");
137         boolean history = ParamUtil.getBoolean(actionRequest, "history");
138         int historyCount = ParamUtil.getInteger(actionRequest, "historyCount");
139         boolean expireable = ParamUtil.getBoolean(actionRequest, "expireable");
140         long maxAge = ParamUtil.getLong(actionRequest, "maxAge");
141         long warningTime = ParamUtil.getLong(actionRequest, "warningTime");
142         int graceLimit = ParamUtil.getInteger(actionRequest, "graceLimit");
143         boolean lockout = ParamUtil.getBoolean(actionRequest, "lockout");
144         int maxFailure = ParamUtil.getInteger(actionRequest, "maxFailure");
145         long lockoutDuration = ParamUtil.getLong(
146             actionRequest, "lockoutDuration");
147         long resetFailureCount = ParamUtil.getLong(
148             actionRequest, "resetFailureCount");
149 
150         if (passwordPolicyId <= 0) {
151 
152             // Add password policy
153 
154             PasswordPolicyServiceUtil.addPasswordPolicy(
155                 name, description, changeable, changeRequired, minAge,
156                 checkSyntax, allowDictionaryWords, minLength, history,
157                 historyCount, expireable, maxAge, warningTime, graceLimit,
158                 lockout, maxFailure, lockoutDuration, resetFailureCount);
159         }
160         else {
161 
162             // Update password policy
163 
164             PasswordPolicyServiceUtil.updatePasswordPolicy(
165                 passwordPolicyId, name, description, changeable, changeRequired,
166                 minAge, checkSyntax, allowDictionaryWords, minLength, history,
167                 historyCount, expireable, maxAge, warningTime, graceLimit,
168                 lockout, maxFailure, lockoutDuration, resetFailureCount);
169         }
170     }
171 
172 }