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());
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());
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());
106    
107                                    return mapping.findForward(
108                                            "portlet.password_policies_admin.error");
109                            }
110                            else {
111                                    throw e;
112                            }
113                    }
114    
115                    return mapping.findForward(
116                            getForward(
117                                    renderRequest,
118                                    "portlet.password_policies_admin.edit_password_policy"));
119            }
120    
121            protected void deletePasswordPolicy(ActionRequest actionRequest)
122                    throws Exception {
123    
124                    long passwordPolicyId = ParamUtil.getLong(
125                            actionRequest, "passwordPolicyId");
126    
127                    PasswordPolicyServiceUtil.deletePasswordPolicy(passwordPolicyId);
128            }
129    
130            protected void updatePasswordPolicy(ActionRequest actionRequest)
131                    throws Exception {
132    
133                    long passwordPolicyId = ParamUtil.getLong(
134                            actionRequest, "passwordPolicyId");
135    
136                    String name = ParamUtil.getString(actionRequest, "name");
137                    String description = ParamUtil.getString(actionRequest, "description");
138                    boolean changeable = ParamUtil.getBoolean(actionRequest, "changeable");
139                    boolean changeRequired = ParamUtil.getBoolean(
140                            actionRequest, "changeRequired");
141                    long minAge = ParamUtil.getLong(actionRequest, "minAge");
142                    boolean checkSyntax = ParamUtil.getBoolean(
143                            actionRequest, "checkSyntax");
144                    boolean allowDictionaryWords = ParamUtil.getBoolean(
145                            actionRequest, "allowDictionaryWords");
146                    int minAlphanumeric = ParamUtil.getInteger(
147                            actionRequest, "minAlphanumeric");
148                    int minLength = ParamUtil.getInteger(actionRequest, "minLength");
149                    int minLowerCase = ParamUtil.getInteger(actionRequest, "minLowerCase");
150                    int minNumbers = ParamUtil.getInteger(actionRequest, "minNumbers");
151                    int minSymbols = ParamUtil.getInteger(actionRequest, "minSymbols");
152                    int minUpperCase = ParamUtil.getInteger(actionRequest, "minUpperCase");
153                    boolean history = ParamUtil.getBoolean(actionRequest, "history");
154                    int historyCount = ParamUtil.getInteger(actionRequest, "historyCount");
155                    boolean expireable = ParamUtil.getBoolean(actionRequest, "expireable");
156                    long maxAge = ParamUtil.getLong(actionRequest, "maxAge");
157                    long warningTime = ParamUtil.getLong(actionRequest, "warningTime");
158                    int graceLimit = ParamUtil.getInteger(actionRequest, "graceLimit");
159                    boolean lockout = ParamUtil.getBoolean(actionRequest, "lockout");
160                    int maxFailure = ParamUtil.getInteger(actionRequest, "maxFailure");
161                    long lockoutDuration = ParamUtil.getLong(
162                            actionRequest, "lockoutDuration");
163                    long resetFailureCount = ParamUtil.getLong(
164                            actionRequest, "resetFailureCount");
165                    long resetTicketMaxAge = ParamUtil.getLong(
166                            actionRequest, "resetTicketMaxAge");
167    
168                    if (passwordPolicyId <= 0) {
169    
170                            // Add password policy
171    
172                            PasswordPolicyServiceUtil.addPasswordPolicy(
173                                    name, description, changeable, changeRequired, minAge,
174                                    checkSyntax, allowDictionaryWords, minAlphanumeric, minLength,
175                                    minLowerCase, minNumbers, minSymbols, minUpperCase, history,
176                                    historyCount, expireable, maxAge, warningTime, graceLimit,
177                                    lockout, maxFailure, lockoutDuration, resetFailureCount,
178                                    resetTicketMaxAge);
179                    }
180                    else {
181    
182                            // Update password policy
183    
184                            PasswordPolicyServiceUtil.updatePasswordPolicy(
185                                    passwordPolicyId, name, description, changeable, changeRequired,
186                                    minAge, checkSyntax, allowDictionaryWords, minAlphanumeric,
187                                    minLength, minLowerCase, minNumbers, minSymbols, minUpperCase,
188                                    history, historyCount, expireable, maxAge, warningTime,
189                                    graceLimit, lockout, maxFailure, lockoutDuration,
190                                    resetFailureCount, resetTicketMaxAge);
191                    }
192            }
193    
194    }