001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.security.auth.PrincipalException;
021    import com.liferay.portal.security.permission.ActionKeys;
022    import com.liferay.portal.security.permission.PermissionChecker;
023    import com.liferay.portal.service.GroupLocalServiceUtil;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     * @author Julio Camarero
028     */
029    public class UserGroupRolePermissionImpl implements UserGroupRolePermission {
030    
031            public void check(
032                            PermissionChecker permissionChecker, long groupId, long roleId)
033                    throws PortalException, SystemException {
034    
035                    if (!contains(permissionChecker, groupId, roleId)) {
036                            throw new PrincipalException();
037                    }
038            }
039    
040            public boolean contains(
041                            PermissionChecker permissionChecker, long groupId, long roleId)
042                    throws PortalException, SystemException {
043    
044                    Group group = GroupLocalServiceUtil.getGroup(groupId);
045    
046                    if (permissionChecker.isGroupOwner(groupId) ||
047                            GroupPermissionUtil.contains(
048                                    permissionChecker, groupId, ActionKeys.ASSIGN_USER_ROLES) ||
049                            OrganizationPermissionUtil.contains(
050                                    permissionChecker, group.getOrganizationId(),
051                                    ActionKeys.ASSIGN_USER_ROLES) ||
052                            RolePermissionUtil.contains(
053                                    permissionChecker, groupId, roleId,
054                                    ActionKeys.ASSIGN_MEMBERS)) {
055    
056                            return true;
057                    }
058                    else {
059                            return false;
060                    }
061            }
062    
063    }