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                    String regex = ParamUtil.getString(actionRequest, "regex");
154                    boolean history = ParamUtil.getBoolean(actionRequest, "history");
155                    int historyCount = ParamUtil.getInteger(actionRequest, "historyCount");
156                    boolean expireable = ParamUtil.getBoolean(actionRequest, "expireable");
157                    long maxAge = ParamUtil.getLong(actionRequest, "maxAge");
158                    long warningTime = ParamUtil.getLong(actionRequest, "warningTime");
159                    int graceLimit = ParamUtil.getInteger(actionRequest, "graceLimit");
160                    boolean lockout = ParamUtil.getBoolean(actionRequest, "lockout");
161                    int maxFailure = ParamUtil.getInteger(actionRequest, "maxFailure");
162                    long lockoutDuration = ParamUtil.getLong(
163                            actionRequest, "lockoutDuration");
164                    long resetFailureCount = ParamUtil.getLong(
165                            actionRequest, "resetFailureCount");
166                    long resetTicketMaxAge = ParamUtil.getLong(
167                            actionRequest, "resetTicketMaxAge");
168    
169                    if (passwordPolicyId <= 0) {
170    
171                            // Add password policy
172    
173                            PasswordPolicyServiceUtil.addPasswordPolicy(
174                                    name, description, changeable, changeRequired, minAge,
175                                    checkSyntax, allowDictionaryWords, minAlphanumeric, minLength,
176                                    minLowerCase, minNumbers, minSymbols, minUpperCase, regex,
177                                    history, historyCount, expireable, maxAge, warningTime,
178                                    graceLimit, lockout, maxFailure, lockoutDuration,
179                                    resetFailureCount, resetTicketMaxAge);
180                    }
181                    else {
182    
183                            // Update password policy
184    
185                            PasswordPolicyServiceUtil.updatePasswordPolicy(
186                                    passwordPolicyId, name, description, changeable, changeRequired,
187                                    minAge, checkSyntax, allowDictionaryWords, minAlphanumeric,
188                                    minLength, minLowerCase, minNumbers, minSymbols, minUpperCase,
189                                    regex, history, historyCount, expireable, maxAge, warningTime,
190                                    graceLimit, lockout, maxFailure, lockoutDuration,
191                                    resetFailureCount, resetTicketMaxAge);
192                    }
193            }
194    
195    }