001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.passwordpoliciesadmin.action;
016    
017    import com.liferay.portal.DuplicatePasswordPolicyException;
018    import com.liferay.portal.NoSuchPasswordPolicyException;
019    import com.liferay.portal.PasswordPolicyNameException;
020    import com.liferay.portal.RequiredPasswordPolicyException;
021    import com.liferay.portal.kernel.servlet.SessionErrors;
022    import com.liferay.portal.kernel.util.Constants;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.security.auth.PrincipalException;
026    import com.liferay.portal.service.PasswordPolicyServiceUtil;
027    import com.liferay.portal.struts.PortletAction;
028    import com.liferay.portal.util.PortalUtil;
029    
030    import javax.portlet.ActionRequest;
031    import javax.portlet.ActionResponse;
032    import javax.portlet.PortletConfig;
033    import javax.portlet.RenderRequest;
034    import javax.portlet.RenderResponse;
035    
036    import org.apache.struts.action.ActionForm;
037    import org.apache.struts.action.ActionForward;
038    import org.apache.struts.action.ActionMapping;
039    
040    /**
041     * @author Scott Lee
042     */
043    public class EditPasswordPolicyAction extends PortletAction {
044    
045            @Override
046            public void processAction(
047                            ActionMapping actionMapping, ActionForm actionForm,
048                            PortletConfig portletConfig, ActionRequest actionRequest,
049                            ActionResponse actionResponse)
050                    throws Exception {
051    
052                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
053    
054                    try {
055                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
056                                    updatePasswordPolicy(actionRequest);
057                            }
058                            else if (cmd.equals(Constants.DELETE)) {
059                                    deletePasswordPolicy(actionRequest);
060                            }
061    
062                            sendRedirect(actionRequest, actionResponse);
063                    }
064                    catch (Exception e) {
065                            if (e instanceof PrincipalException) {
066                                    SessionErrors.add(actionRequest, e.getClass());
067    
068                                    setForward(
069                                            actionRequest, "portlet.password_policies_admin.error");
070                            }
071                            else if (e instanceof DuplicatePasswordPolicyException ||
072                                             e instanceof PasswordPolicyNameException ||
073                                             e instanceof NoSuchPasswordPolicyException ||
074                                             e instanceof RequiredPasswordPolicyException) {
075    
076                                    SessionErrors.add(actionRequest, e.getClass());
077    
078                                    if (cmd.equals(Constants.DELETE)) {
079                                            String redirect = PortalUtil.escapeRedirect(
080                                                    ParamUtil.getString(actionRequest, "redirect"));
081    
082                                            if (Validator.isNotNull(redirect)) {
083                                                    actionResponse.sendRedirect(redirect);
084                                            }
085                                    }
086                            }
087                            else {
088                                    throw e;
089                            }
090                    }
091            }
092    
093            @Override
094            public ActionForward render(
095                            ActionMapping actionMapping, ActionForm actionForm,
096                            PortletConfig portletConfig, RenderRequest renderRequest,
097                            RenderResponse renderResponse)
098                    throws Exception {
099    
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());
108    
109                                    return actionMapping.findForward(
110                                            "portlet.password_policies_admin.error");
111                            }
112                            else {
113                                    throw e;
114                            }
115                    }
116    
117                    return actionMapping.findForward(
118                            getForward(
119                                    renderRequest,
120                                    "portlet.password_policies_admin.edit_password_policy"));
121            }
122    
123            protected void deletePasswordPolicy(ActionRequest actionRequest)
124                    throws Exception {
125    
126                    long passwordPolicyId = ParamUtil.getLong(
127                            actionRequest, "passwordPolicyId");
128    
129                    PasswordPolicyServiceUtil.deletePasswordPolicy(passwordPolicyId);
130            }
131    
132            protected void updatePasswordPolicy(ActionRequest actionRequest)
133                    throws Exception {
134    
135                    long passwordPolicyId = ParamUtil.getLong(
136                            actionRequest, "passwordPolicyId");
137    
138                    String name = ParamUtil.getString(actionRequest, "name");
139                    String description = ParamUtil.getString(actionRequest, "description");
140                    boolean changeable = ParamUtil.getBoolean(actionRequest, "changeable");
141                    boolean changeRequired = ParamUtil.getBoolean(
142                            actionRequest, "changeRequired");
143                    long minAge = ParamUtil.getLong(actionRequest, "minAge");
144                    boolean checkSyntax = ParamUtil.getBoolean(
145                            actionRequest, "checkSyntax");
146                    boolean allowDictionaryWords = ParamUtil.getBoolean(
147                            actionRequest, "allowDictionaryWords");
148                    int minAlphanumeric = ParamUtil.getInteger(
149                            actionRequest, "minAlphanumeric");
150                    int minLength = ParamUtil.getInteger(actionRequest, "minLength");
151                    int minLowerCase = ParamUtil.getInteger(actionRequest, "minLowerCase");
152                    int minNumbers = ParamUtil.getInteger(actionRequest, "minNumbers");
153                    int minSymbols = ParamUtil.getInteger(actionRequest, "minSymbols");
154                    int minUpperCase = ParamUtil.getInteger(actionRequest, "minUpperCase");
155                    boolean history = ParamUtil.getBoolean(actionRequest, "history");
156                    int historyCount = ParamUtil.getInteger(actionRequest, "historyCount");
157                    boolean expireable = ParamUtil.getBoolean(actionRequest, "expireable");
158                    long maxAge = ParamUtil.getLong(actionRequest, "maxAge");
159                    long warningTime = ParamUtil.getLong(actionRequest, "warningTime");
160                    int graceLimit = ParamUtil.getInteger(actionRequest, "graceLimit");
161                    boolean lockout = ParamUtil.getBoolean(actionRequest, "lockout");
162                    int maxFailure = ParamUtil.getInteger(actionRequest, "maxFailure");
163                    long lockoutDuration = ParamUtil.getLong(
164                            actionRequest, "lockoutDuration");
165                    long resetFailureCount = ParamUtil.getLong(
166                            actionRequest, "resetFailureCount");
167                    long resetTicketMaxAge = ParamUtil.getLong(
168                            actionRequest, "resetTicketMaxAge");
169    
170                    if (passwordPolicyId <= 0) {
171    
172                            // Add password policy
173    
174                            PasswordPolicyServiceUtil.addPasswordPolicy(
175                                    name, description, changeable, changeRequired, minAge,
176                                    checkSyntax, allowDictionaryWords, minAlphanumeric, minLength,
177                                    minLowerCase, minNumbers, minSymbols, minUpperCase, history,
178                                    historyCount, expireable, maxAge, warningTime, graceLimit,
179                                    lockout, maxFailure, lockoutDuration, resetFailureCount,
180                                    resetTicketMaxAge);
181                    }
182                    else {
183    
184                            // Update password policy
185    
186                            PasswordPolicyServiceUtil.updatePasswordPolicy(
187                                    passwordPolicyId, name, description, changeable, changeRequired,
188                                    minAge, checkSyntax, allowDictionaryWords, minAlphanumeric,
189                                    minLength, minLowerCase, minNumbers, minSymbols, minUpperCase,
190                                    history, historyCount, expireable, maxAge, warningTime,
191                                    graceLimit, lockout, maxFailure, lockoutDuration,
192                                    resetFailureCount, resetTicketMaxAge);
193                    }
194            }
195    
196    }