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.usergroupsadmin.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.User;
021    import com.liferay.portal.model.UserGroup;
022    import com.liferay.portal.security.auth.MembershipPolicyUtil;
023    import com.liferay.portal.service.UserLocalServiceUtil;
024    
025    import java.util.Set;
026    
027    import javax.portlet.RenderResponse;
028    
029    /**
030     * @author Charles May
031     */
032    public class UserUserGroupChecker extends RowChecker {
033    
034            public UserUserGroupChecker(
035                    RenderResponse renderResponse, UserGroup userGroup) {
036    
037                    super(renderResponse);
038    
039                    _userGroup = userGroup;
040            }
041    
042            @Override
043            public boolean isChecked(Object obj) {
044                    User user = (User)obj;
045    
046                    try {
047                            return UserLocalServiceUtil.hasUserGroupUser(
048                                    _userGroup.getUserGroupId(), user.getUserId());
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                    Set<UserGroup> mandatoryGroups =
062                            MembershipPolicyUtil.getMandatoryUserGroups(user);
063    
064                    if ((isChecked(user) && mandatoryGroups.contains(_userGroup)) ||
065                            (!isChecked(user) &&
066                             !MembershipPolicyUtil.isMembershipAllowed(_userGroup, user))) {
067    
068                            return true;
069                    }
070    
071                    return super.isDisabled(obj);
072            }
073    
074            private static Log _log = LogFactoryUtil.getLog(UserUserGroupChecker.class);
075    
076            private UserGroup _userGroup;
077    
078    }