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