001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.permission;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.model.Group;
020    import com.liferay.portal.model.User;
021    import com.liferay.portal.security.auth.PrincipalException;
022    import com.liferay.portal.security.permission.ActionKeys;
023    import com.liferay.portal.security.permission.PermissionChecker;
024    import com.liferay.portal.service.GroupLocalServiceUtil;
025    import com.liferay.portal.service.UserLocalServiceUtil;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     * @author Raymond Augé
030     */
031    public class GroupPermissionImpl implements GroupPermission {
032    
033            public void check(
034                            PermissionChecker permissionChecker, Group group, String actionId)
035                    throws PortalException, SystemException {
036    
037                    if (!contains(permissionChecker, group, actionId)) {
038                            throw new PrincipalException();
039                    }
040            }
041    
042            public void check(
043                            PermissionChecker permissionChecker, long groupId, String actionId)
044                    throws PortalException, SystemException {
045    
046                    if (!contains(permissionChecker, groupId, actionId)) {
047                            throw new PrincipalException();
048                    }
049            }
050    
051            public boolean contains(
052                            PermissionChecker permissionChecker, Group group, String actionId)
053                    throws PortalException, SystemException {
054    
055                    long groupId = group.getGroupId();
056    
057                    if (group.isStagingGroup()) {
058                            group = group.getLiveGroup();
059                    }
060    
061                    if (group.isUser()) {
062    
063                            // An individual user would never reach this block because he would
064                            // be an administrator of his own layouts. However, a user who
065                            // manages a set of organizations may be modifying pages of a user
066                            // he manages.
067    
068                            User user = UserLocalServiceUtil.getUserById(group.getClassPK());
069    
070                            if (UserPermissionUtil.contains(
071                                            permissionChecker, user.getUserId(),
072                                            user.getOrganizationIds(), ActionKeys.UPDATE)) {
073    
074                                    return true;
075                            }
076                    }
077    
078                    if (actionId.equals(ActionKeys.ADD_LAYOUT)) {
079                            if (permissionChecker.hasPermission(
080                                            groupId, Group.class.getName(), groupId,
081                                            ActionKeys.MANAGE_LAYOUTS)) {
082    
083                                    return true;
084                            }
085                    }
086                    else if ((actionId.equals(ActionKeys.EXPORT_IMPORT_LAYOUTS) ||
087                                      actionId.equals(ActionKeys.EXPORT_IMPORT_PORTLET_INFO)) &&
088                                     permissionChecker.hasPermission(
089                                             groupId, Group.class.getName(), groupId,
090                                             ActionKeys.PUBLISH_STAGING)) {
091    
092                            return true;
093                    }
094                    else if (actionId.equals(ActionKeys.VIEW_STAGING) &&
095                                     (permissionChecker.hasPermission(
096                                             groupId, Group.class.getName(), groupId,
097                                             ActionKeys.MANAGE_LAYOUTS) ||
098                                      permissionChecker.hasPermission(
099                                             groupId, Group.class.getName(), groupId,
100                                             ActionKeys.MANAGE_STAGING) ||
101                                      permissionChecker.hasPermission(
102                                             groupId, Group.class.getName(), groupId,
103                                             ActionKeys.PUBLISH_STAGING) ||
104                                      permissionChecker.hasPermission(
105                                             groupId, Group.class.getName(), groupId,
106                                             ActionKeys.UPDATE))) {
107    
108                            return true;
109                    }
110    
111                    // Group id must be set so that users can modify their personal pages
112    
113                    return permissionChecker.hasPermission(
114                            groupId, Group.class.getName(), groupId, actionId);
115            }
116    
117            public boolean contains(
118                            PermissionChecker permissionChecker, long groupId, String actionId)
119                    throws PortalException, SystemException {
120    
121                    Group group = GroupLocalServiceUtil.getGroup(groupId);
122    
123                    return contains(permissionChecker, group, actionId);
124            }
125    
126    }