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.enterpriseadmin.action;
022:
023: import com.liferay.portal.NoSuchRoleException;
024: import com.liferay.portal.kernel.util.ArrayUtil;
025: import com.liferay.portal.kernel.util.Constants;
026: import com.liferay.portal.kernel.util.ParamUtil;
027: import com.liferay.portal.kernel.util.StringUtil;
028: import com.liferay.portal.kernel.util.Validator;
029: import com.liferay.portal.model.Role;
030: import com.liferay.portal.model.impl.GroupImpl;
031: import com.liferay.portal.model.impl.ResourceImpl;
032: import com.liferay.portal.model.impl.RoleImpl;
033: import com.liferay.portal.security.auth.PrincipalException;
034: import com.liferay.portal.security.permission.ResourceActionsUtil;
035: import com.liferay.portal.security.permission.comparator.ActionComparator;
036: import com.liferay.portal.service.PermissionServiceUtil;
037: import com.liferay.portal.service.RoleServiceUtil;
038: import com.liferay.portal.struts.PortletAction;
039: import com.liferay.portal.theme.ThemeDisplay;
040: import com.liferay.portal.util.WebKeys;
041: import com.liferay.util.servlet.SessionErrors;
042: import com.liferay.util.servlet.SessionMessages;
043:
044: import java.util.Collections;
045: import java.util.HashMap;
046: import java.util.Iterator;
047: import java.util.List;
048: import java.util.Map;
049:
050: import javax.portlet.ActionRequest;
051: import javax.portlet.ActionResponse;
052: import javax.portlet.PortletConfig;
053: import javax.portlet.RenderRequest;
054: import javax.portlet.RenderResponse;
055:
056: import org.apache.struts.action.ActionForm;
057: import org.apache.struts.action.ActionForward;
058: import org.apache.struts.action.ActionMapping;
059:
060: /**
061: * <a href="EditRolePermissionsAction.java.html"><b><i>View Source</i></b></a>
062: *
063: * @author Brian Wing Shun Chan
064: * @author Jorge Ferrer
065: *
066: */
067: public class EditRolePermissionsAction extends PortletAction {
068:
069: public void processAction(ActionMapping mapping, ActionForm form,
070: PortletConfig config, ActionRequest req, ActionResponse res)
071: throws Exception {
072:
073: String cmd = ParamUtil.getString(req, Constants.CMD);
074:
075: try {
076: if (cmd.equals("actions")) {
077: updateActions(req, res);
078: } else if (cmd.equals("delete_permission")) {
079: deletePermission(req, res);
080: }
081: } catch (Exception e) {
082: if (e instanceof NoSuchRoleException
083: || e instanceof PrincipalException) {
084:
085: SessionErrors.add(req, e.getClass().getName());
086:
087: setForward(req, "portlet.enterprise_admin.error");
088: } else {
089: throw e;
090: }
091: }
092: }
093:
094: public ActionForward render(ActionMapping mapping, ActionForm form,
095: PortletConfig config, RenderRequest req, RenderResponse res)
096: throws Exception {
097:
098: try {
099: ActionUtil.getRole(req);
100: } catch (Exception e) {
101: if (e instanceof NoSuchRoleException
102: || e instanceof PrincipalException) {
103:
104: SessionErrors.add(req, e.getClass().getName());
105:
106: return mapping
107: .findForward("portlet.enterprise_admin.error");
108: } else {
109: throw e;
110: }
111: }
112:
113: return mapping.findForward(getForward(req,
114: "portlet.enterprise_admin.edit_role_permissions"));
115: }
116:
117: protected void deletePermission(ActionRequest req,
118: ActionResponse res) throws Exception {
119:
120: ThemeDisplay themeDisplay = (ThemeDisplay) req
121: .getAttribute(WebKeys.THEME_DISPLAY);
122:
123: long roleId = ParamUtil.getLong(req, "roleId");
124: long permissionId = ParamUtil.getLong(req, "permissionId");
125:
126: PermissionServiceUtil.unsetRolePermission(roleId, themeDisplay
127: .getPortletGroupId(), permissionId);
128:
129: // Send redirect
130:
131: SessionMessages.add(req, "permissionDeleted");
132:
133: String redirect = ParamUtil.getString(req, "redirect");
134:
135: res.sendRedirect(redirect);
136: }
137:
138: protected void updateActions(ActionRequest req, ActionResponse res)
139: throws Exception {
140:
141: ThemeDisplay themeDisplay = (ThemeDisplay) req
142: .getAttribute(WebKeys.THEME_DISPLAY);
143:
144: long roleId = ParamUtil.getLong(req, "roleId");
145:
146: String portletResource = ParamUtil.getString(req,
147: "portletResource");
148: String[] modelResources = StringUtil.split(ParamUtil.getString(
149: req, "modelResources"));
150:
151: Map resourceActionsMap = new HashMap();
152:
153: if (Validator.isNotNull(portletResource)) {
154: resourceActionsMap.put(portletResource, ResourceActionsUtil
155: .getResourceActions(themeDisplay.getCompanyId(),
156: portletResource, null));
157: }
158:
159: for (int i = 0; i < modelResources.length; i++) {
160: resourceActionsMap.put(modelResources[i],
161: ResourceActionsUtil.getResourceActions(themeDisplay
162: .getCompanyId(), null, modelResources[i]));
163: }
164:
165: Iterator itr = resourceActionsMap.keySet().iterator();
166:
167: while (itr.hasNext()) {
168: String selResource = (String) itr.next();
169:
170: List actions = (List) resourceActionsMap.get(selResource);
171:
172: Collections.sort(actions, new ActionComparator(themeDisplay
173: .getCompanyId(), themeDisplay.getLocale()));
174:
175: Role role = RoleServiceUtil.getRole(roleId);
176:
177: for (int i = 0; i < actions.size(); i++) {
178: String actionId = (String) actions.get(i);
179:
180: int scope = ParamUtil.getInteger(req, "scope"
181: + selResource + actionId);
182:
183: if (scope == ResourceImpl.SCOPE_COMPANY) {
184: PermissionServiceUtil.setRolePermission(roleId,
185: themeDisplay.getPortletGroupId(),
186: selResource, scope, String
187: .valueOf(themeDisplay
188: .getCompanyId()), actionId);
189: } else if (scope == ResourceImpl.SCOPE_GROUP) {
190: if ((role.getType() == RoleImpl.TYPE_COMMUNITY)
191: || (role.getType() == RoleImpl.TYPE_ORGANIZATION)) {
192:
193: PermissionServiceUtil
194: .setRolePermission(
195: roleId,
196: themeDisplay
197: .getPortletGroupId(),
198: selResource,
199: ResourceImpl.SCOPE_GROUP_TEMPLATE,
200: String
201: .valueOf(GroupImpl.DEFAULT_PARENT_GROUP_ID),
202: actionId);
203: } else {
204: String[] groupIds = StringUtil.split(ParamUtil
205: .getString(req, "groupIds"
206: + selResource + actionId));
207:
208: if (groupIds.length == 0) {
209: SessionErrors.add(req,
210: "missingGroupIdsForAction");
211: return;
212: }
213:
214: groupIds = ArrayUtil.distinct(groupIds);
215:
216: PermissionServiceUtil.unsetRolePermissions(
217: roleId, themeDisplay
218: .getPortletGroupId(),
219: selResource, ResourceImpl.SCOPE_GROUP,
220: actionId);
221:
222: for (int j = 0; j < groupIds.length; j++) {
223: PermissionServiceUtil.setRolePermission(
224: roleId, themeDisplay
225: .getPortletGroupId(),
226: selResource,
227: ResourceImpl.SCOPE_GROUP,
228: groupIds[j], actionId);
229: }
230: }
231: } else {
232:
233: // Remove company, group template, and group permissions
234:
235: PermissionServiceUtil.unsetRolePermissions(roleId,
236: themeDisplay.getPortletGroupId(),
237: selResource, ResourceImpl.SCOPE_COMPANY,
238: actionId);
239:
240: PermissionServiceUtil
241: .unsetRolePermissions(roleId, themeDisplay
242: .getPortletGroupId(), selResource,
243: ResourceImpl.SCOPE_GROUP_TEMPLATE,
244: actionId);
245:
246: PermissionServiceUtil.unsetRolePermissions(roleId,
247: themeDisplay.getPortletGroupId(),
248: selResource, ResourceImpl.SCOPE_GROUP,
249: actionId);
250: }
251: }
252: }
253:
254: // Send redirect
255:
256: SessionMessages.add(req, "permissionsUpdated");
257:
258: String redirect = ParamUtil.getString(req, "redirect") + "&"
259: + Constants.CMD + "=" + Constants.VIEW;
260:
261: res.sendRedirect(redirect);
262: }
263:
264: }
|