1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.NoSuchPasswordPolicyException;
26  import com.liferay.portal.PasswordPolicyNameException;
27  import com.liferay.portal.RequiredPasswordPolicyException;
28  import com.liferay.portal.kernel.servlet.SessionErrors;
29  import com.liferay.portal.kernel.util.Constants;
30  import com.liferay.portal.kernel.util.ParamUtil;
31  import com.liferay.portal.security.auth.PrincipalException;
32  import com.liferay.portal.service.PasswordPolicyServiceUtil;
33  import com.liferay.portal.struts.PortletAction;
34  
35  import javax.portlet.ActionRequest;
36  import javax.portlet.ActionResponse;
37  import javax.portlet.PortletConfig;
38  import javax.portlet.RenderRequest;
39  import javax.portlet.RenderResponse;
40  
41  import org.apache.struts.action.ActionForm;
42  import org.apache.struts.action.ActionForward;
43  import org.apache.struts.action.ActionMapping;
44  
45  /**
46   * <a href="EditPasswordPolicyAction.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Scott Lee
49   *
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 PasswordPolicyNameException ||
77                       e instanceof NoSuchPasswordPolicyException ||
78                       e instanceof RequiredPasswordPolicyException) {
79  
80                  SessionErrors.add(actionRequest, e.getClass().getName());
81  
82                  if (cmd.equals(Constants.DELETE)) {
83                      actionResponse.sendRedirect(
84                          ParamUtil.getString(actionRequest, "redirect"));
85                  }
86              }
87              else {
88                  throw e;
89              }
90          }
91      }
92  
93      public ActionForward render(
94              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
95              RenderRequest renderRequest, RenderResponse renderResponse)
96          throws Exception {
97  
98          try {
99              ActionUtil.getPasswordPolicy(renderRequest);
100         }
101         catch (Exception e) {
102             if (e instanceof NoSuchPasswordPolicyException ||
103                 e instanceof PrincipalException) {
104 
105                 SessionErrors.add(renderRequest, e.getClass().getName());
106 
107                 return mapping.findForward("portlet.enterprise_admin.error");
108             }
109             else {
110                 throw e;
111             }
112         }
113 
114         return mapping.findForward(getForward(
115             renderRequest, "portlet.enterprise_admin.edit_password_policy"));
116     }
117 
118     protected void deletePasswordPolicy(ActionRequest actionRequest)
119         throws Exception {
120 
121         long passwordPolicyId = ParamUtil.getLong(
122             actionRequest, "passwordPolicyId");
123 
124         PasswordPolicyServiceUtil.deletePasswordPolicy(passwordPolicyId);
125     }
126 
127     protected void updatePasswordPolicy(ActionRequest actionRequest)
128         throws Exception {
129 
130         long passwordPolicyId = ParamUtil.getLong(
131             actionRequest, "passwordPolicyId");
132 
133         String name = ParamUtil.getString(actionRequest, "name");
134         String description = ParamUtil.getString(actionRequest, "description");
135         boolean changeable = ParamUtil.getBoolean(actionRequest, "changeable");
136         boolean changeRequired = ParamUtil.getBoolean(
137             actionRequest, "changeRequired");
138         long minAge = ParamUtil.getLong(actionRequest, "minAge");
139         boolean checkSyntax = ParamUtil.getBoolean(
140             actionRequest, "checkSyntax");
141         boolean allowDictionaryWords = ParamUtil.getBoolean(
142             actionRequest, "allowDictionaryWords");
143         int minLength = ParamUtil.getInteger(actionRequest, "minLength");
144         boolean history = ParamUtil.getBoolean(actionRequest, "history");
145         int historyCount = ParamUtil.getInteger(actionRequest, "historyCount");
146         boolean expireable = ParamUtil.getBoolean(actionRequest, "expireable");
147         long maxAge = ParamUtil.getLong(actionRequest, "maxAge");
148         long warningTime = ParamUtil.getLong(actionRequest, "warningTime");
149         int graceLimit = ParamUtil.getInteger(actionRequest, "graceLimit");
150         boolean lockout = ParamUtil.getBoolean(actionRequest, "lockout");
151         int maxFailure = ParamUtil.getInteger(actionRequest, "maxFailure");
152         long lockoutDuration = ParamUtil.getLong(
153             actionRequest, "lockoutDuration");
154         long resetFailureCount = ParamUtil.getLong(
155             actionRequest, "resetFailureCount");
156 
157         if (passwordPolicyId <= 0) {
158 
159             // Add password policy
160 
161             PasswordPolicyServiceUtil.addPasswordPolicy(
162                 name, description, changeable, changeRequired, minAge,
163                 checkSyntax, allowDictionaryWords, minLength, history,
164                 historyCount, expireable, maxAge, warningTime, graceLimit,
165                 lockout, maxFailure, lockoutDuration, resetFailureCount);
166         }
167         else {
168 
169             // Update password policy
170 
171             PasswordPolicyServiceUtil.updatePasswordPolicy(
172                 passwordPolicyId, name, description, changeable, changeRequired,
173                 minAge, checkSyntax, allowDictionaryWords, minLength, history,
174                 historyCount, expireable, maxAge, warningTime, graceLimit,
175                 lockout, maxFailure, lockoutDuration, resetFailureCount);
176         }
177     }
178 
179 }