1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.enterpriseadmin.action;
21  
22  import com.liferay.portal.DuplicatePasswordPolicyException;
23  import com.liferay.portal.NoSuchPasswordPolicyException;
24  import com.liferay.portal.PasswordPolicyNameException;
25  import com.liferay.portal.RequiredPasswordPolicyException;
26  import com.liferay.portal.kernel.servlet.SessionErrors;
27  import com.liferay.portal.kernel.util.Constants;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.security.auth.PrincipalException;
30  import com.liferay.portal.service.PasswordPolicyServiceUtil;
31  import com.liferay.portal.struts.PortletAction;
32  
33  import javax.portlet.ActionRequest;
34  import javax.portlet.ActionResponse;
35  import javax.portlet.PortletConfig;
36  import javax.portlet.RenderRequest;
37  import javax.portlet.RenderResponse;
38  
39  import org.apache.struts.action.ActionForm;
40  import org.apache.struts.action.ActionForward;
41  import org.apache.struts.action.ActionMapping;
42  
43  /**
44   * <a href="EditPasswordPolicyAction.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Scott Lee
47   *
48   */
49  public class EditPasswordPolicyAction extends PortletAction {
50  
51      public void processAction(
52              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
53              ActionRequest actionRequest, ActionResponse actionResponse)
54          throws Exception {
55  
56          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
57  
58          try {
59              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
60                  updatePasswordPolicy(actionRequest);
61              }
62              else if (cmd.equals(Constants.DELETE)) {
63                  deletePasswordPolicy(actionRequest);
64              }
65  
66              sendRedirect(actionRequest, actionResponse);
67          }
68          catch (Exception e) {
69              if (e instanceof PrincipalException) {
70                  SessionErrors.add(actionRequest, e.getClass().getName());
71  
72                  setForward(actionRequest, "portlet.enterprise_admin.error");
73              }
74              else if (e instanceof DuplicatePasswordPolicyException ||
75                       e instanceof PasswordPolicyNameException ||
76                       e instanceof NoSuchPasswordPolicyException ||
77                       e instanceof RequiredPasswordPolicyException) {
78  
79                  SessionErrors.add(actionRequest, e.getClass().getName());
80  
81                  if (cmd.equals(Constants.DELETE)) {
82                      actionResponse.sendRedirect(
83                          ParamUtil.getString(actionRequest, "redirect"));
84                  }
85              }
86              else {
87                  throw e;
88              }
89          }
90      }
91  
92      public ActionForward render(
93              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
94              RenderRequest renderRequest, RenderResponse renderResponse)
95          throws Exception {
96  
97          try {
98              ActionUtil.getPasswordPolicy(renderRequest);
99          }
100         catch (Exception e) {
101             if (e instanceof NoSuchPasswordPolicyException ||
102                 e instanceof PrincipalException) {
103 
104                 SessionErrors.add(renderRequest, e.getClass().getName());
105 
106                 return mapping.findForward("portlet.enterprise_admin.error");
107             }
108             else {
109                 throw e;
110             }
111         }
112 
113         return mapping.findForward(getForward(
114             renderRequest, "portlet.enterprise_admin.edit_password_policy"));
115     }
116 
117     protected void deletePasswordPolicy(ActionRequest actionRequest)
118         throws Exception {
119 
120         long passwordPolicyId = ParamUtil.getLong(
121             actionRequest, "passwordPolicyId");
122 
123         PasswordPolicyServiceUtil.deletePasswordPolicy(passwordPolicyId);
124     }
125 
126     protected void updatePasswordPolicy(ActionRequest actionRequest)
127         throws Exception {
128 
129         long passwordPolicyId = ParamUtil.getLong(
130             actionRequest, "passwordPolicyId");
131 
132         String name = ParamUtil.getString(actionRequest, "name");
133         String description = ParamUtil.getString(actionRequest, "description");
134         boolean changeable = ParamUtil.getBoolean(actionRequest, "changeable");
135         boolean changeRequired = ParamUtil.getBoolean(
136             actionRequest, "changeRequired");
137         long minAge = ParamUtil.getLong(actionRequest, "minAge");
138         boolean checkSyntax = ParamUtil.getBoolean(
139             actionRequest, "checkSyntax");
140         boolean allowDictionaryWords = ParamUtil.getBoolean(
141             actionRequest, "allowDictionaryWords");
142         int minLength = ParamUtil.getInteger(actionRequest, "minLength");
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 
156         if (passwordPolicyId <= 0) {
157 
158             // Add password policy
159 
160             PasswordPolicyServiceUtil.addPasswordPolicy(
161                 name, description, changeable, changeRequired, minAge,
162                 checkSyntax, allowDictionaryWords, minLength, history,
163                 historyCount, expireable, maxAge, warningTime, graceLimit,
164                 lockout, maxFailure, lockoutDuration, resetFailureCount);
165         }
166         else {
167 
168             // Update password policy
169 
170             PasswordPolicyServiceUtil.updatePasswordPolicy(
171                 passwordPolicyId, name, description, changeable, changeRequired,
172                 minAge, checkSyntax, allowDictionaryWords, minLength, history,
173                 historyCount, expireable, maxAge, warningTime, graceLimit,
174                 lockout, maxFailure, lockoutDuration, resetFailureCount);
175         }
176     }
177 
178 }