1   /**
2    * Copyright (c) 2000-2007 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.util.Constants;
29  import com.liferay.portal.kernel.util.ParamUtil;
30  import com.liferay.portal.security.auth.PrincipalException;
31  import com.liferay.portal.service.PasswordPolicyServiceUtil;
32  import com.liferay.portal.struts.PortletAction;
33  import com.liferay.util.servlet.SessionErrors;
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 config,
55              ActionRequest req, ActionResponse res)
56          throws Exception {
57  
58          String cmd = ParamUtil.getString(req, Constants.CMD);
59  
60          try {
61              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
62                  updatePasswordPolicy(req);
63              }
64              else if (cmd.equals(Constants.DELETE)) {
65                  deletePasswordPolicy(req);
66              }
67  
68              sendRedirect(req, res);
69          }
70          catch (Exception e) {
71              if (e instanceof PrincipalException) {
72                  SessionErrors.add(req, e.getClass().getName());
73  
74                  setForward(req, "portlet.enterprise_admin.error");
75              }
76              else if (e instanceof PasswordPolicyNameException ||
77                       e instanceof NoSuchPasswordPolicyException ||
78                       e instanceof RequiredPasswordPolicyException) {
79  
80                  SessionErrors.add(req, e.getClass().getName());
81  
82                  if (cmd.equals(Constants.DELETE)) {
83                      res.sendRedirect(ParamUtil.getString(req, "redirect"));
84                  }
85              }
86              else {
87                  throw e;
88              }
89          }
90      }
91  
92      public ActionForward render(
93              ActionMapping mapping, ActionForm form, PortletConfig config,
94              RenderRequest req, RenderResponse res)
95          throws Exception {
96  
97          try {
98              ActionUtil.getPasswordPolicy(req);
99          }
100         catch (Exception e) {
101             if (e instanceof NoSuchPasswordPolicyException ||
102                 e instanceof PrincipalException) {
103 
104                 SessionErrors.add(req, 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(
114             getForward(req, "portlet.enterprise_admin.edit_password_policy"));
115     }
116 
117     protected void deletePasswordPolicy(ActionRequest req) throws Exception {
118         long passwordPolicyId = ParamUtil.getLong(req, "passwordPolicyId");
119 
120         PasswordPolicyServiceUtil.deletePasswordPolicy(passwordPolicyId);
121     }
122 
123     protected void updatePasswordPolicy(ActionRequest req) throws Exception {
124         long passwordPolicyId = ParamUtil.getLong(req, "passwordPolicyId");
125 
126         String name = ParamUtil.getString(req, "name");
127         String description = ParamUtil.getString(req, "description");
128         boolean changeable = ParamUtil.getBoolean(req, "changeable");
129         boolean changeRequired = ParamUtil.getBoolean(req, "changeRequired");
130         long minAge = ParamUtil.getLong(req, "minAge");
131         boolean checkSyntax = ParamUtil.getBoolean(req, "checkSyntax");
132         boolean allowDictionaryWords = ParamUtil.getBoolean(
133             req, "allowDictionaryWords");
134         int minLength = ParamUtil.getInteger(req, "minLength");
135         boolean history = ParamUtil.getBoolean(req, "history");
136         int historyCount = ParamUtil.getInteger(req, "historyCount");
137         boolean expireable = ParamUtil.getBoolean(req, "expireable");
138         long maxAge = ParamUtil.getLong(req, "maxAge");
139         long warningTime = ParamUtil.getLong(req, "warningTime");
140         int graceLimit = ParamUtil.getInteger(req, "graceLimit");
141         boolean lockout = ParamUtil.getBoolean(req, "lockout");
142         int maxFailure = ParamUtil.getInteger(req, "maxFailure");
143         long lockoutDuration = ParamUtil.getLong(req, "lockoutDuration");
144         long resetFailureCount = ParamUtil.getLong(req, "resetFailureCount");
145 
146         if (passwordPolicyId <= 0) {
147 
148             // Add password policy
149 
150             PasswordPolicyServiceUtil.addPasswordPolicy(
151                 name, description, changeable, changeRequired, minAge,
152                 checkSyntax, allowDictionaryWords, minLength, history,
153                 historyCount, expireable, maxAge, warningTime, graceLimit,
154                 lockout, maxFailure, lockoutDuration, resetFailureCount);
155         }
156         else {
157 
158             // Update password policy
159 
160             PasswordPolicyServiceUtil.updatePasswordPolicy(
161                 passwordPolicyId, name, description, changeable, changeRequired,
162                 minAge, checkSyntax, allowDictionaryWords, minLength, history,
163                 historyCount, expireable, maxAge, warningTime, graceLimit,
164                 lockout, maxFailure, lockoutDuration, resetFailureCount);
165         }
166     }
167 
168 }