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.portal.service.permission;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.security.permission.ActionKeys;
026: import com.liferay.portal.kernel.security.permission.PermissionChecker;
027: import com.liferay.portal.kernel.util.StringMaker;
028: import com.liferay.portal.model.Layout;
029: import com.liferay.portal.model.Portlet;
030: import com.liferay.portal.model.impl.PortletImpl;
031: import com.liferay.portal.security.auth.PrincipalException;
032: import com.liferay.portal.security.permission.ResourceActionsUtil;
033: import com.liferay.portal.service.LayoutLocalServiceUtil;
034:
035: import java.util.List;
036:
037: import org.apache.commons.logging.Log;
038: import org.apache.commons.logging.LogFactory;
039:
040: /**
041: * <a href="PortletPermissionImpl.java.html"><b><i>View Source</i></b></a>
042: *
043: * @author Brian Wing Shun Chan
044: *
045: */
046: public class PortletPermissionImpl implements PortletPermission {
047:
048: public static final boolean DEFAULT_STRICT = false;
049:
050: public void check(PermissionChecker permissionChecker,
051: String portletId, String actionId) throws PortalException,
052: SystemException {
053:
054: if (!contains(permissionChecker, portletId, actionId)) {
055: throw new PrincipalException();
056: }
057: }
058:
059: public void check(PermissionChecker permissionChecker, long plid,
060: String portletId, String actionId) throws PortalException,
061: SystemException {
062:
063: check(permissionChecker, plid, portletId, actionId,
064: DEFAULT_STRICT);
065: }
066:
067: public void check(PermissionChecker permissionChecker, long plid,
068: String portletId, String actionId, boolean strict)
069: throws PortalException, SystemException {
070:
071: if (!contains(permissionChecker, plid, portletId, actionId,
072: strict)) {
073: throw new PrincipalException();
074: }
075: }
076:
077: public boolean contains(PermissionChecker permissionChecker,
078: String portletId, String actionId) throws PortalException,
079: SystemException {
080:
081: return contains(permissionChecker, 0, portletId, actionId);
082: }
083:
084: public boolean contains(PermissionChecker permissionChecker,
085: long plid, String portletId, String actionId)
086: throws PortalException, SystemException {
087:
088: return contains(permissionChecker, plid, portletId, actionId,
089: DEFAULT_STRICT);
090: }
091:
092: public boolean contains(PermissionChecker permissionChecker,
093: long plid, String portletId, String actionId, boolean strict)
094: throws PortalException, SystemException {
095:
096: long groupId = 0;
097: String name = null;
098: String primKey = null;
099:
100: if (plid > 0) {
101: Layout layout = LayoutLocalServiceUtil.getLayout(plid);
102:
103: groupId = layout.getGroupId();
104: name = PortletImpl.getRootPortletId(portletId);
105: primKey = getPrimaryKey(plid, portletId);
106:
107: if (!strict) {
108: if (LayoutPermissionUtil.contains(permissionChecker,
109: groupId, layout.isPrivateLayout(), layout
110: .getLayoutId(), ActionKeys.UPDATE)
111: && hasLayoutManagerPermission(portletId,
112: actionId)) {
113:
114: return true;
115: }
116: }
117: } else {
118: name = portletId;
119: primKey = portletId;
120: }
121:
122: return permissionChecker.hasPermission(groupId, name, primKey,
123: actionId);
124: }
125:
126: public boolean contains(PermissionChecker permissionChecker,
127: long plid, Portlet portlet, String actionId)
128: throws PortalException, SystemException {
129:
130: return contains(permissionChecker, plid, portlet, actionId,
131: DEFAULT_STRICT);
132: }
133:
134: public boolean contains(PermissionChecker permissionChecker,
135: long plid, Portlet portlet, String actionId, boolean strict)
136: throws PortalException, SystemException {
137:
138: boolean value = contains(permissionChecker, plid, portlet
139: .getPortletId(), actionId, strict);
140:
141: if (value) {
142: return true;
143: } else {
144: if (portlet.isSystem() && actionId.equals(ActionKeys.VIEW)) {
145: return true;
146: } else {
147: return false;
148: }
149: }
150: }
151:
152: public String getPrimaryKey(long plid, String portletId) {
153: StringMaker sm = new StringMaker();
154:
155: sm.append(plid);
156: sm.append(PortletImpl.LAYOUT_SEPARATOR);
157: sm.append(portletId);
158:
159: return sm.toString();
160: }
161:
162: public boolean hasLayoutManagerPermission(String portletId,
163: String actionId) {
164:
165: try {
166: return hasLayoutManagerPermissionImpl(portletId, actionId);
167: } catch (Exception e) {
168: _log.error(e, e);
169:
170: return false;
171: }
172: }
173:
174: protected boolean hasLayoutManagerPermissionImpl(String portletId,
175: String actionId) throws SystemException {
176:
177: portletId = PortletImpl.getRootPortletId(portletId);
178:
179: List layoutManagerActions = ResourceActionsUtil
180: .getPortletResourceLayoutManagerActions(portletId);
181:
182: return layoutManagerActions.contains(actionId);
183: }
184:
185: private static Log _log = LogFactory
186: .getLog(PortletPermissionImpl.class);
187:
188: }
|