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.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.Group;
021    import com.liferay.portal.model.Role;
022    import com.liferay.portal.model.User;
023    import com.liferay.portal.security.auth.MembershipPolicyUtil;
024    import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
025    
026    import java.util.Set;
027    
028    import javax.portlet.RenderResponse;
029    
030    /**
031     * @author Jorge Ferrer
032     */
033    public class UserGroupRoleUserChecker extends RowChecker {
034    
035            public UserGroupRoleUserChecker(
036                    RenderResponse renderResponse, Group group, Role role) {
037    
038                    super(renderResponse);
039    
040                    _group = group;
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(), _group.getGroupId(), _role.getRoleId());
051                    }
052                    catch (Exception e) {
053                            _log.error(e, e);
054    
055                            return false;
056                    }
057            }
058    
059            @Override
060            public boolean isDisabled(Object obj) {
061                    User user = (User)obj;
062    
063                    Set<Role> mandatoryRoles = MembershipPolicyUtil.getMandatoryRoles(
064                            _group, user);
065    
066                    if ((isChecked(user) && mandatoryRoles.contains(_role)) ||
067                            (!isChecked(user) &&
068                             !MembershipPolicyUtil.isMembershipAllowed(_group, _role, user))) {
069    
070                            return true;
071                    }
072    
073                    return super.isDisabled(obj);
074            }
075    
076            private static Log _log = LogFactoryUtil.getLog(
077                    UserGroupRoleUserChecker.class);
078    
079            private Group _group;
080            private Role _role;
081    
082    }