001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.portletconfiguration.action;
022:
023: import com.liferay.portal.kernel.util.Constants;
024: import com.liferay.portal.kernel.util.ParamUtil;
025: import com.liferay.portal.kernel.util.StringUtil;
026: import com.liferay.portal.kernel.util.Validator;
027: import com.liferay.portal.model.Layout;
028: import com.liferay.portal.model.Organization;
029: import com.liferay.portal.model.Portlet;
030: import com.liferay.portal.model.Resource;
031: import com.liferay.portal.model.UserGroup;
032: import com.liferay.portal.model.impl.PortletImpl;
033: import com.liferay.portal.security.auth.PrincipalException;
034: import com.liferay.portal.service.PermissionServiceUtil;
035: import com.liferay.portal.service.PortletLocalServiceUtil;
036: import com.liferay.portal.service.ResourceLocalServiceUtil;
037: import com.liferay.portal.servlet.filters.layoutcache.LayoutCacheUtil;
038: import com.liferay.portal.theme.ThemeDisplay;
039: import com.liferay.portal.util.WebKeys;
040: import com.liferay.util.servlet.SessionErrors;
041:
042: import javax.portlet.ActionRequest;
043: import javax.portlet.ActionResponse;
044: import javax.portlet.PortletConfig;
045: import javax.portlet.RenderRequest;
046: import javax.portlet.RenderResponse;
047:
048: import org.apache.struts.action.ActionForm;
049: import org.apache.struts.action.ActionForward;
050: import org.apache.struts.action.ActionMapping;
051:
052: /**
053: * <a href="EditPermissionsAction.java.html"><b><i>View Source</i></b></a>
054: *
055: * @author Brian Wing Shun Chan
056: *
057: */
058: public class EditPermissionsAction extends EditConfigurationAction {
059:
060: public void processAction(ActionMapping mapping, ActionForm form,
061: PortletConfig config, ActionRequest req, ActionResponse res)
062: throws Exception {
063:
064: String cmd = ParamUtil.getString(req, Constants.CMD);
065:
066: try {
067: if (cmd.equals("group_permissions")) {
068: updateGroupPermissions(req);
069: } else if (cmd.equals("guest_permissions")) {
070: updateGuestPermissions(req);
071: } else if (cmd.equals("organization_permissions")) {
072: updateOrganizationPermissions(req);
073: } else if (cmd.equals("role_permissions")) {
074: updateRolePermissions(req);
075: } else if (cmd.equals("user_group_permissions")) {
076: updateUserGroupPermissions(req);
077: } else if (cmd.equals("user_permissions")) {
078: updateUserPermissions(req);
079: }
080:
081: String redirect = ParamUtil.getString(req,
082: "permissionsRedirect");
083:
084: sendRedirect(req, res, redirect);
085: } catch (Exception e) {
086: if (e instanceof PrincipalException) {
087: SessionErrors.add(req, e.getClass().getName());
088:
089: setForward(req, "portlet.portlet_configuration.error");
090: } else {
091: throw e;
092: }
093: }
094: }
095:
096: public ActionForward render(ActionMapping mapping, ActionForm form,
097: PortletConfig config, RenderRequest req, RenderResponse res)
098: throws Exception {
099:
100: ThemeDisplay themeDisplay = (ThemeDisplay) req
101: .getAttribute(WebKeys.THEME_DISPLAY);
102:
103: long groupId = themeDisplay.getPortletGroupId();
104:
105: String portletResource = ParamUtil.getString(req,
106: "portletResource");
107: String modelResource = ParamUtil
108: .getString(req, "modelResource");
109: String resourcePrimKey = ParamUtil.getString(req,
110: "resourcePrimKey");
111:
112: String selResource = portletResource;
113:
114: if (Validator.isNotNull(modelResource)) {
115: selResource = modelResource;
116: }
117:
118: try {
119: PermissionServiceUtil.checkPermission(groupId, selResource,
120: resourcePrimKey);
121: } catch (PrincipalException pe) {
122: SessionErrors.add(req, PrincipalException.class.getName());
123:
124: setForward(req, "portlet.portlet_configuration.error");
125: }
126:
127: Portlet portlet = PortletLocalServiceUtil.getPortletById(
128: themeDisplay.getCompanyId(), portletResource);
129:
130: if (portlet != null) {
131: res.setTitle(getTitle(portlet, req));
132: }
133:
134: return mapping.findForward(getForward(req,
135: "portlet.portlet_configuration.edit_permissions"));
136: }
137:
138: protected void updateGroupPermissions(ActionRequest req)
139: throws Exception {
140: Layout layout = (Layout) req.getAttribute(WebKeys.LAYOUT);
141:
142: long resourceId = ParamUtil.getLong(req, "resourceId");
143: long groupId = ParamUtil.getLong(req, "groupId");
144: String[] actionIds = StringUtil.split(ParamUtil.getString(req,
145: "groupIdActionIds"));
146:
147: PermissionServiceUtil.setGroupPermissions(groupId, actionIds,
148: resourceId);
149:
150: if (!layout.isPrivateLayout()) {
151: Resource resource = ResourceLocalServiceUtil
152: .getResource(resourceId);
153:
154: if (resource.getPrimKey().startsWith(
155: layout.getPlid() + PortletImpl.LAYOUT_SEPARATOR)) {
156:
157: LayoutCacheUtil.clearCache(layout.getCompanyId());
158: }
159: }
160: }
161:
162: protected void updateGuestPermissions(ActionRequest req)
163: throws Exception {
164: ThemeDisplay themeDisplay = (ThemeDisplay) req
165: .getAttribute(WebKeys.THEME_DISPLAY);
166:
167: long resourceId = ParamUtil.getLong(req, "resourceId");
168: String[] actionIds = StringUtil.split(ParamUtil.getString(req,
169: "guestActionIds"));
170:
171: PermissionServiceUtil.setUserPermissions(themeDisplay
172: .getDefaultUserId(), themeDisplay.getPortletGroupId(),
173: actionIds, resourceId);
174: }
175:
176: protected void updateOrganizationPermissions(ActionRequest req)
177: throws Exception {
178:
179: Layout layout = (Layout) req.getAttribute(WebKeys.LAYOUT);
180:
181: long resourceId = ParamUtil.getLong(req, "resourceId");
182: long organizationId = ParamUtil.getLong(req,
183: "organizationIdsPosValue");
184: String[] actionIds = StringUtil.split(ParamUtil.getString(req,
185: "organizationIdActionIds"));
186: //boolean organizationIntersection = ParamUtil.getBoolean(
187: // req, "organizationIntersection");
188:
189: //if (!organizationIntersection) {
190: PermissionServiceUtil.setGroupPermissions(Organization.class
191: .getName(), String.valueOf(organizationId), layout
192: .getGroupId(), actionIds, resourceId);
193: /*}
194: else {
195: PermissionServiceUtil.setOrgGroupPermissions(
196: organizationId, layout.getGroupId(), actionIds, resourceId);
197: }*/
198: }
199:
200: protected void updateRolePermissions(ActionRequest req)
201: throws Exception {
202:
203: ThemeDisplay themeDisplay = (ThemeDisplay) req
204: .getAttribute(WebKeys.THEME_DISPLAY);
205:
206: long resourceId = ParamUtil.getLong(req, "resourceId");
207: long roleId = ParamUtil.getLong(req, "roleIdsPosValue");
208: String[] actionIds = StringUtil.split(ParamUtil.getString(req,
209: "roleIdActionIds"));
210:
211: PermissionServiceUtil.setRolePermissions(roleId, themeDisplay
212: .getPortletGroupId(), actionIds, resourceId);
213: }
214:
215: protected void updateUserGroupPermissions(ActionRequest req)
216: throws Exception {
217:
218: Layout layout = (Layout) req.getAttribute(WebKeys.LAYOUT);
219:
220: long resourceId = ParamUtil.getLong(req, "resourceId");
221: long userGroupId = ParamUtil.getLong(req,
222: "userGroupIdsPosValue");
223: String[] actionIds = StringUtil.split(ParamUtil.getString(req,
224: "userGroupIdActionIds"));
225:
226: PermissionServiceUtil.setGroupPermissions(UserGroup.class
227: .getName(), String.valueOf(userGroupId), layout
228: .getGroupId(), actionIds, resourceId);
229: }
230:
231: protected void updateUserPermissions(ActionRequest req)
232: throws Exception {
233: ThemeDisplay themeDisplay = (ThemeDisplay) req
234: .getAttribute(WebKeys.THEME_DISPLAY);
235:
236: long resourceId = ParamUtil.getLong(req, "resourceId");
237: long userId = ParamUtil.getLong(req, "userIdsPosValue");
238: String[] actionIds = StringUtil.split(ParamUtil.getString(req,
239: "userIdActionIds"));
240:
241: PermissionServiceUtil.setUserPermissions(userId, themeDisplay
242: .getPortletGroupId(), actionIds, resourceId);
243: }
244:
245: }
|