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.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    }