1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.communities.action;
24  
25  import com.liferay.portal.NoSuchGroupException;
26  import com.liferay.portal.kernel.util.Constants;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.security.auth.PrincipalException;
30  import com.liferay.portal.service.PermissionServiceUtil;
31  import com.liferay.portal.struts.PortletAction;
32  import com.liferay.util.servlet.SessionErrors;
33  
34  import javax.portlet.ActionRequest;
35  import javax.portlet.ActionResponse;
36  import javax.portlet.PortletConfig;
37  import javax.portlet.RenderRequest;
38  import javax.portlet.RenderResponse;
39  
40  import org.apache.struts.action.ActionForm;
41  import org.apache.struts.action.ActionForward;
42  import org.apache.struts.action.ActionMapping;
43  
44  /**
45   * <a href="EditUserPermissionsAction.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Charles May
48   *
49   */
50  public class EditUserPermissionsAction extends PortletAction {
51  
52      public void processAction(
53              ActionMapping mapping, ActionForm form, PortletConfig config,
54              ActionRequest req, ActionResponse res)
55          throws Exception {
56  
57          String cmd = ParamUtil.getString(req, Constants.CMD);
58  
59          try {
60              if (cmd.equals("user_permissions")) {
61                  updateUserPermissions(req);
62              }
63  
64              String redirect = ParamUtil.getString(req, "permissionsRedirect");
65  
66              sendRedirect(req, res, redirect);
67          }
68          catch (Exception e) {
69              if (e instanceof PrincipalException) {
70                  SessionErrors.add(req, e.getClass().getName());
71  
72                  setForward(req, "portlet.communities.error");
73              }
74              else {
75                  throw e;
76              }
77          }
78      }
79  
80      public ActionForward render(
81              ActionMapping mapping, ActionForm form, PortletConfig config,
82              RenderRequest req, RenderResponse res)
83          throws Exception {
84  
85          try {
86              ActionUtil.getGroup(req);
87          }
88          catch (Exception e) {
89              if (e instanceof NoSuchGroupException ||
90                  e instanceof PrincipalException) {
91  
92                  SessionErrors.add(req, e.getClass().getName());
93  
94                  return mapping.findForward("portlet.communities.error");
95              }
96              else {
97                  throw e;
98              }
99          }
100 
101         return mapping.findForward(
102             getForward(req, "portlet.communities.edit_user_permissions"));
103     }
104 
105     protected void updateUserPermissions(ActionRequest req) throws Exception {
106         long groupId = ParamUtil.getLong(req, "groupId");
107         long resourceId = ParamUtil.getLong(req, "resourceId");
108         long userId = ParamUtil.getLong(req, "userIdsPosValue");
109         String[] actionIds = StringUtil.split(
110             ParamUtil.getString(req, "userIdActionIds"));
111 
112         PermissionServiceUtil.setUserPermissions(
113             userId, groupId, actionIds, resourceId);
114     }
115 
116 }