001    /**
002     * Copyright (c) 2000-2012 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 mapping, ActionForm form, PortletConfig portletConfig,
048                            ActionRequest actionRequest, ActionResponse actionResponse)
049                    throws Exception {
050    
051                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
052    
053                    try {
054                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
055                                    updatePasswordPolicy(actionRequest);
056                            }
057                            else if (cmd.equals(Constants.DELETE)) {
058                                    deletePasswordPolicy(actionRequest);
059                            }
060    
061                            sendRedirect(actionRequest, actionResponse);
062                    }
063                    catch (Exception e) {
064                            if (e instanceof PrincipalException) {
065                                    SessionErrors.add(actionRequest, e.getClass().getName());
066    
067                                    setForward(
068                                            actionRequest, "portlet.password_policies_admin.error");
069                            }
070                            else if (e instanceof DuplicatePasswordPolicyException ||
071                                             e instanceof PasswordPolicyNameException ||
072                                             e instanceof NoSuchPasswordPolicyException ||
073                                             e instanceof RequiredPasswordPolicyException) {
074    
075                                    SessionErrors.add(actionRequest, e.getClass().getName());
076    
077                                    if (cmd.equals(Constants.DELETE)) {
078                                            String redirect = PortalUtil.escapeRedirect(
079                                                    ParamUtil.getString(actionRequest, "redirect"));
080    
081                                            if (Validator.isNotNull(redirect)) {
082                                                    actionResponse.sendRedirect(redirect);
083                                            }
084                                    }
085                            }
086                            else {
087                                    throw e;
088                            }
089                    }
090            }
091    
092            @Override
093            public ActionForward render(
094                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
095                            RenderRequest renderRequest, RenderResponse renderResponse)
096                    throws Exception {
097    
098                    try {
099                            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(
108                                            "portlet.password_policies_admin.error");
109                            }
110                            else {
111                                    throw e;
112                            }
113                    }
114    
115                    return mapping.findForward(getForward(
116                            renderRequest,
117                            "portlet.password_policies_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 minAlphanumeric = ParamUtil.getInteger(
146                            actionRequest, "minAlphanumeric");
147                    int minLength = ParamUtil.getInteger(actionRequest, "minLength");
148                    int minLowerCase = ParamUtil.getInteger(actionRequest, "minLowerCase");
149                    int minNumbers = ParamUtil.getInteger(actionRequest, "minNumbers");
150                    int minSymbols = ParamUtil.getInteger(actionRequest, "minSymbols");
151                    int minUpperCase = ParamUtil.getInteger(actionRequest, "minUpperCase");
152                    boolean history = ParamUtil.getBoolean(actionRequest, "history");
153                    int historyCount = ParamUtil.getInteger(actionRequest, "historyCount");
154                    boolean expireable = ParamUtil.getBoolean(actionRequest, "expireable");
155                    long maxAge = ParamUtil.getLong(actionRequest, "maxAge");
156                    long warningTime = ParamUtil.getLong(actionRequest, "warningTime");
157                    int graceLimit = ParamUtil.getInteger(actionRequest, "graceLimit");
158                    boolean lockout = ParamUtil.getBoolean(actionRequest, "lockout");
159                    int maxFailure = ParamUtil.getInteger(actionRequest, "maxFailure");
160                    long lockoutDuration = ParamUtil.getLong(
161                            actionRequest, "lockoutDuration");
162                    long resetFailureCount = ParamUtil.getLong(
163                            actionRequest, "resetFailureCount");
164                    long resetTicketMaxAge = ParamUtil.getLong(
165                            actionRequest, "resetTicketMaxAge");
166    
167                    if (passwordPolicyId <= 0) {
168    
169                            // Add password policy
170    
171                            PasswordPolicyServiceUtil.addPasswordPolicy(
172                                    name, description, changeable, changeRequired, minAge,
173                                    checkSyntax, allowDictionaryWords, minAlphanumeric, minLength,
174                                    minLowerCase, minNumbers, minSymbols, minUpperCase, history,
175                                    historyCount, expireable, maxAge, warningTime, graceLimit,
176                                    lockout, maxFailure, lockoutDuration, resetFailureCount,
177                                    resetTicketMaxAge);
178                    }
179                    else {
180    
181                            // Update password policy
182    
183                            PasswordPolicyServiceUtil.updatePasswordPolicy(
184                                    passwordPolicyId, name, description, changeable, changeRequired,
185                                    minAge, checkSyntax, allowDictionaryWords, minAlphanumeric,
186                                    minLength, minLowerCase, minNumbers, minSymbols, minUpperCase,
187                                    history, historyCount, expireable, maxAge, warningTime,
188                                    graceLimit, lockout, maxFailure, lockoutDuration,
189                                    resetFailureCount, resetTicketMaxAge);
190                    }
191            }
192    
193    }