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.NoSuchPasswordPolicyException;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.util.Constants;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.security.auth.PrincipalException;
024    import com.liferay.portal.service.OrganizationServiceUtil;
025    import com.liferay.portal.service.UserServiceUtil;
026    import com.liferay.portal.struts.PortletAction;
027    
028    import javax.portlet.ActionRequest;
029    import javax.portlet.ActionResponse;
030    import javax.portlet.PortletConfig;
031    import javax.portlet.RenderRequest;
032    import javax.portlet.RenderResponse;
033    
034    import org.apache.struts.action.ActionForm;
035    import org.apache.struts.action.ActionForward;
036    import org.apache.struts.action.ActionMapping;
037    
038    /**
039     * @author Scott Lee
040     */
041    public class EditPasswordPolicyAssignmentsAction extends PortletAction {
042    
043            @Override
044            public void processAction(
045                            ActionMapping actionMapping, ActionForm actionForm,
046                            PortletConfig portletConfig, ActionRequest actionRequest,
047                            ActionResponse actionResponse)
048                    throws Exception {
049    
050                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
051    
052                    try {
053                            if (cmd.equals("password_policy_organizations")) {
054                                    updatePasswordPolicyOrganizations(actionRequest);
055                            }
056                            else if (cmd.equals("password_policy_users")) {
057                                    updatePasswordPolicyUsers(actionRequest);
058                            }
059    
060                            if (Validator.isNotNull(cmd)) {
061                                    String redirect = ParamUtil.getString(
062                                            actionRequest, "assignmentsRedirect");
063    
064                                    sendRedirect(actionRequest, actionResponse, redirect);
065                            }
066                    }
067                    catch (Exception e) {
068                            if (e instanceof NoSuchPasswordPolicyException ||
069                                    e instanceof PrincipalException) {
070    
071                                    SessionErrors.add(actionRequest, e.getClass());
072    
073                                    setForward(
074                                            actionRequest, "portlet.password_policies_admin.error");
075                            }
076                            else {
077                                    throw e;
078                            }
079                    }
080            }
081    
082            @Override
083            public ActionForward render(
084                            ActionMapping actionMapping, ActionForm actionForm,
085                            PortletConfig portletConfig, RenderRequest renderRequest,
086                            RenderResponse renderResponse)
087                    throws Exception {
088    
089                    try {
090                            ActionUtil.getPasswordPolicy(renderRequest);
091                    }
092                    catch (Exception e) {
093                            if (e instanceof NoSuchPasswordPolicyException ||
094                                    e instanceof PrincipalException) {
095    
096                                    SessionErrors.add(renderRequest, e.getClass());
097    
098                                    return actionMapping.findForward(
099                                            "portlet.password_policies_admin.error");
100                            }
101                            else {
102                                    throw e;
103                            }
104                    }
105    
106                    return actionMapping.findForward(
107                            getForward(
108                                    renderRequest,
109                                    "portlet.password_policies_admin." +
110                                            "edit_password_policy_assignments"));
111            }
112    
113            protected void updatePasswordPolicyOrganizations(
114                            ActionRequest actionRequest)
115                    throws Exception {
116    
117                    long passwordPolicyId = ParamUtil.getLong(
118                            actionRequest, "passwordPolicyId");
119    
120                    long[] addOrganizationIds = StringUtil.split(
121                            ParamUtil.getString(actionRequest, "addOrganizationIds"), 0L);
122                    long[] removeOrganizationIds = StringUtil.split(
123                            ParamUtil.getString(actionRequest, "removeOrganizationIds"), 0L);
124    
125                    OrganizationServiceUtil.addPasswordPolicyOrganizations(
126                            passwordPolicyId, addOrganizationIds);
127                    OrganizationServiceUtil.unsetPasswordPolicyOrganizations(
128                            passwordPolicyId, removeOrganizationIds);
129            }
130    
131            protected void updatePasswordPolicyUsers(ActionRequest actionRequest)
132                    throws Exception {
133    
134                    long passwordPolicyId = ParamUtil.getLong(
135                            actionRequest, "passwordPolicyId");
136    
137                    long[] addUserIds = StringUtil.split(
138                            ParamUtil.getString(actionRequest, "addUserIds"), 0L);
139                    long[] removeUserIds = StringUtil.split(
140                            ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
141    
142                    UserServiceUtil.addPasswordPolicyUsers(passwordPolicyId, addUserIds);
143                    UserServiceUtil.unsetPasswordPolicyUsers(
144                            passwordPolicyId, removeUserIds);
145            }
146    
147    }