001    /**
002     * Copyright (c) 2000-2013 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            @Override
032            public void check(
033                            PermissionChecker permissionChecker, long groupId, long roleId)
034                    throws PortalException, SystemException {
035    
036                    if (!contains(permissionChecker, groupId, roleId)) {
037                            throw new PrincipalException();
038                    }
039            }
040    
041            @Override
042            public boolean contains(
043                            PermissionChecker permissionChecker, long groupId, long roleId)
044                    throws PortalException, SystemException {
045    
046                    Group group = GroupLocalServiceUtil.getGroup(groupId);
047    
048                    if (permissionChecker.isGroupOwner(groupId) ||
049                            GroupPermissionUtil.contains(
050                                    permissionChecker, groupId, ActionKeys.ASSIGN_USER_ROLES) ||
051                            OrganizationPermissionUtil.contains(
052                                    permissionChecker, group.getOrganizationId(),
053                                    ActionKeys.ASSIGN_USER_ROLES) ||
054                            RolePermissionUtil.contains(
055                                    permissionChecker, groupId, roleId,
056                                    ActionKeys.ASSIGN_MEMBERS)) {
057    
058                            return true;
059                    }
060                    else {
061                            return false;
062                    }
063            }
064    
065    }