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.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    }