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 UserGroupRoleRoleChecker extends RowChecker {
034    
035            public UserGroupRoleRoleChecker(
036                    RenderResponse renderResponse, User user, Group group) {
037    
038                    super(renderResponse);
039    
040                    _user = user;
041                    _group = group;
042            }
043    
044            @Override
045            public boolean isChecked(Object obj) {
046                    Role role = (Role)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                    Role role = (Role)obj;
062    
063                    Set<Role> mandatoryRoles = MembershipPolicyUtil.getMandatoryRoles(
064                            _group, _user);
065    
066                    if ((isChecked(role) && mandatoryRoles.contains(role)) ||
067                            (!isChecked(role) &&
068                             !MembershipPolicyUtil.isMembershipAllowed(
069                                    _group, role, _user))) {
070    
071                            return true;
072                    }
073    
074                    return super.isDisabled(obj);
075            }
076    
077            private static Log _log = LogFactoryUtil.getLog(
078                    UserGroupRoleRoleChecker.class);
079    
080            private Group _group;
081            private User _user;
082    
083    }