001    /**
002     * Copyright (c) 2000-2013 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.portlet.sites.search;
016    
017    import com.liferay.portal.kernel.dao.search.RowChecker;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.model.Organization;
021    import com.liferay.portal.model.Role;
022    import com.liferay.portal.model.User;
023    import com.liferay.portal.security.membershippolicy.OrganizationMembershipPolicyUtil;
024    import com.liferay.portal.security.permission.PermissionChecker;
025    import com.liferay.portal.security.permission.PermissionThreadLocal;
026    import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
027    
028    import javax.portlet.RenderResponse;
029    
030    /**
031     * @author Roberto D??az
032     */
033    public class OrganizationRoleUserChecker extends RowChecker {
034    
035            public OrganizationRoleUserChecker(
036                    RenderResponse renderResponse, Organization organization, Role role) {
037    
038                    super(renderResponse);
039    
040                    _organization = organization;
041                    _role = role;
042            }
043    
044            @Override
045            public boolean isChecked(Object obj) {
046                    User user = (User)obj;
047    
048                    try {
049                            return UserGroupRoleLocalServiceUtil.hasUserGroupRole(
050                                    user.getUserId(), _organization.getGroupId(),
051                                    _role.getRoleId());
052                    }
053                    catch (Exception e) {
054                            _log.error(e, e);
055    
056                            return false;
057                    }
058            }
059    
060            @Override
061            public boolean isDisabled(Object obj) {
062                    User user = (User)obj;
063    
064                    try {
065                            PermissionChecker permissionChecker =
066                                    PermissionThreadLocal.getPermissionChecker();
067    
068                            if (isChecked(user)) {
069                                    if (OrganizationMembershipPolicyUtil.isRoleProtected(
070                                                    permissionChecker, user.getUserId(),
071                                                    _organization.getOrganizationId(), _role.getRoleId()) ||
072                                            OrganizationMembershipPolicyUtil.isRoleRequired(
073                                                    user.getUserId(), _organization.getOrganizationId(),
074                                                    _role.getRoleId())) {
075    
076                                            return true;
077                                    }
078                            }
079                            else {
080                                    if (!OrganizationMembershipPolicyUtil.isRoleAllowed(
081                                                    user.getUserId(), _organization.getOrganizationId(),
082                                                    _role.getRoleId())) {
083    
084                                            return true;
085                                    }
086                            }
087                    }
088                    catch (Exception e) {
089                            _log.error(e, e);
090                    }
091    
092                    return super.isDisabled(obj);
093            }
094    
095            private static Log _log = LogFactoryUtil.getLog(
096                    OrganizationRoleUserChecker.class);
097    
098            private Organization _organization;
099            private Role _role;
100    
101    }