001    /**
002     * Copyright (c) 2000-2010 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.enterpriseadmin.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            public void processAction(
044                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
045                            ActionRequest actionRequest, ActionResponse actionResponse)
046                    throws Exception {
047    
048                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
049    
050                    try {
051                            if (cmd.equals("password_policy_organizations")) {
052                                    updatePasswordPolicyOrganizations(actionRequest);
053                            }
054                            else if (cmd.equals("password_policy_users")) {
055                                    updatePasswordPolicyUsers(actionRequest);
056                            }
057    
058                            if (Validator.isNotNull(cmd)) {
059                                    String redirect = ParamUtil.getString(
060                                            actionRequest, "assignmentsRedirect");
061    
062                                    sendRedirect(actionRequest, actionResponse, redirect);
063                            }
064                    }
065                    catch (Exception e) {
066                            if (e instanceof NoSuchPasswordPolicyException ||
067                                    e instanceof PrincipalException) {
068    
069                                    SessionErrors.add(actionRequest, e.getClass().getName());
070    
071                                    setForward(actionRequest, "portlet.enterprise_admin.error");
072                            }
073                            else {
074                                    throw e;
075                            }
076                    }
077            }
078    
079            public ActionForward render(
080                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
081                            RenderRequest renderRequest, RenderResponse renderResponse)
082                    throws Exception {
083    
084                    try {
085                            ActionUtil.getPasswordPolicy(renderRequest);
086                    }
087                    catch (Exception e) {
088                            if (e instanceof NoSuchPasswordPolicyException ||
089                                    e instanceof PrincipalException) {
090    
091                                    SessionErrors.add(renderRequest, e.getClass().getName());
092    
093                                    return mapping.findForward("portlet.enterprise_admin.error");
094                            }
095                            else {
096                                    throw e;
097                            }
098                    }
099    
100                    return mapping.findForward(getForward(
101                            renderRequest,
102                            "portlet.enterprise_admin.edit_password_policy_assignments"));
103            }
104    
105            protected void updatePasswordPolicyOrganizations(
106                            ActionRequest actionRequest)
107                    throws Exception {
108    
109                    long passwordPolicyId = ParamUtil.getLong(
110                            actionRequest, "passwordPolicyId");
111    
112                    long[] addOrganizationIds = StringUtil.split(
113                            ParamUtil.getString(actionRequest, "addOrganizationIds"), 0L);
114                    long[] removeOrganizationIds = StringUtil.split(
115                            ParamUtil.getString(actionRequest, "removeOrganizationIds"), 0L);
116    
117                    OrganizationServiceUtil.addPasswordPolicyOrganizations(
118                            passwordPolicyId, addOrganizationIds);
119                    OrganizationServiceUtil.unsetPasswordPolicyOrganizations(
120                            passwordPolicyId, removeOrganizationIds);
121            }
122    
123            protected void updatePasswordPolicyUsers(ActionRequest actionRequest)
124                    throws Exception {
125    
126                    long passwordPolicyId = ParamUtil.getLong(
127                            actionRequest, "passwordPolicyId");
128    
129                    long[] addUserIds = StringUtil.split(
130                            ParamUtil.getString(actionRequest, "addUserIds"), 0L);
131                    long[] removeUserIds = StringUtil.split(
132                            ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
133    
134                    UserServiceUtil.addPasswordPolicyUsers(passwordPolicyId, addUserIds);
135                    UserServiceUtil.unsetPasswordPolicyUsers(
136                            passwordPolicyId, removeUserIds);
137            }
138    
139    }