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.NoSuchResourceException;
024: import com.liferay.portal.PortalException;
025: import com.liferay.portal.SystemException;
026: import com.liferay.portal.kernel.security.permission.ActionKeys;
027: import com.liferay.portal.kernel.security.permission.PermissionChecker;
028: import com.liferay.portal.model.Layout;
029: import com.liferay.portal.model.impl.LayoutImpl;
030: import com.liferay.portal.model.impl.ResourceImpl;
031: import com.liferay.portal.security.auth.PrincipalException;
032: import com.liferay.portal.service.LayoutLocalServiceUtil;
033: import com.liferay.portal.service.ResourceLocalServiceUtil;
034:
035: /**
036: * <a href="LayoutPermissionImpl.java.html"><b><i>View Source</i></b></a>
037: *
038: * @author Charles May
039: * @author Brian Wing Shun Chan
040: *
041: */
042: public class LayoutPermissionImpl implements LayoutPermission {
043:
044: public void check(PermissionChecker permissionChecker, long plid,
045: String actionId) throws PortalException, SystemException {
046:
047: if (!contains(permissionChecker, plid, actionId)) {
048: throw new PrincipalException();
049: }
050: }
051:
052: public void check(PermissionChecker permissionChecker,
053: long groupId, boolean privateLayout, long layoutId,
054: String actionId) throws PortalException, SystemException {
055:
056: if (!contains(permissionChecker, groupId, privateLayout,
057: layoutId, actionId)) {
058:
059: throw new PrincipalException();
060: }
061: }
062:
063: public void check(PermissionChecker permissionChecker,
064: Layout layout, String actionId) throws PortalException,
065: SystemException {
066:
067: if (!contains(permissionChecker, layout, actionId)) {
068: throw new PrincipalException();
069: }
070: }
071:
072: public boolean contains(PermissionChecker permissionChecker,
073: long plid, String actionId) throws PortalException,
074: SystemException {
075:
076: Layout layout = LayoutLocalServiceUtil.getLayout(plid);
077:
078: return contains(permissionChecker, layout, actionId);
079: }
080:
081: public boolean contains(PermissionChecker permissionChecker,
082: long groupId, boolean privateLayout, long layoutId,
083: String actionId) throws PortalException, SystemException {
084:
085: if (layoutId == LayoutImpl.DEFAULT_PARENT_LAYOUT_ID) {
086: if (GroupPermissionUtil.contains(permissionChecker,
087: groupId, ActionKeys.MANAGE_LAYOUTS)) {
088:
089: return true;
090: } else {
091: return false;
092: }
093: } else {
094: Layout layout = LayoutLocalServiceUtil.getLayout(groupId,
095: privateLayout, layoutId);
096:
097: return contains(permissionChecker, layout, actionId);
098: }
099: }
100:
101: public boolean contains(PermissionChecker permissionChecker,
102: Layout layout, String actionId) throws PortalException,
103: SystemException {
104:
105: if (GroupPermissionUtil.contains(permissionChecker, layout
106: .getGroupId(), ActionKeys.MANAGE_LAYOUTS)) {
107:
108: return true;
109: }
110:
111: try {
112: ResourceLocalServiceUtil.getResource(layout.getCompanyId(),
113: Layout.class.getName(),
114: ResourceImpl.SCOPE_INDIVIDUAL, String
115: .valueOf(layout.getPlid()));
116: } catch (NoSuchResourceException nsre) {
117: boolean addCommunityPermission = true;
118: boolean addGuestPermission = true;
119:
120: if (layout.isPrivateLayout()) {
121: addGuestPermission = false;
122: }
123:
124: ResourceLocalServiceUtil.addResources(
125: layout.getCompanyId(), layout.getGroupId(), 0,
126: Layout.class.getName(), layout.getPlid(), false,
127: addCommunityPermission, addGuestPermission);
128: }
129:
130: return permissionChecker.hasPermission(layout.getGroupId(),
131: Layout.class.getName(), layout.getPlid(), actionId);
132: }
133:
134: }
|