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.Group;
021    import com.liferay.portal.model.Role;
022    import com.liferay.portal.model.User;
023    import com.liferay.portal.security.membershippolicy.SiteMembershipPolicyUtil;
024    import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
025    
026    import javax.portlet.RenderResponse;
027    
028    /**
029     * @author Jorge Ferrer
030     */
031    public class UserGroupRoleUserChecker extends RowChecker {
032    
033            public UserGroupRoleUserChecker(
034                    RenderResponse renderResponse, Group group, Role role) {
035    
036                    super(renderResponse);
037    
038                    _group = group;
039                    _role = role;
040            }
041    
042            @Override
043            public boolean isChecked(Object obj) {
044                    User user = (User)obj;
045    
046                    try {
047                            return UserGroupRoleLocalServiceUtil.hasUserGroupRole(
048                                    user.getUserId(), _group.getGroupId(), _role.getRoleId());
049                    }
050                    catch (Exception e) {
051                            _log.error(e, e);
052    
053                            return false;
054                    }
055            }
056    
057            @Override
058            public boolean isDisabled(Object obj) {
059                    User user = (User)obj;
060    
061                    try {
062                            if (isChecked(user)) {
063                                    if (SiteMembershipPolicyUtil.isRoleRequired(
064                                                    user.getUserId(), _group.getGroupId(),
065                                                    _role.getRoleId())) {
066    
067                                            return true;
068                                    }
069                            }
070                            else {
071                                    if (!SiteMembershipPolicyUtil.isRoleAllowed(
072                                                    user.getUserId(), _group.getGroupId(),
073                                                    _role.getRoleId())) {
074    
075                                            return true;
076                                    }
077                            }
078                    }
079                    catch (Exception e) {
080                            _log.error(e, e);
081                    }
082    
083                    return super.isDisabled(obj);
084            }
085    
086            private static Log _log = LogFactoryUtil.getLog(
087                    UserGroupRoleUserChecker.class);
088    
089            private Group _group;
090            private Role _role;
091    
092    }