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.Group;
021    import com.liferay.portal.model.User;
022    import com.liferay.portal.security.auth.MembershipPolicyUtil;
023    import com.liferay.portal.security.permission.PermissionChecker;
024    import com.liferay.portal.security.permission.PermissionThreadLocal;
025    import com.liferay.portal.service.UserLocalServiceUtil;
026    
027    import java.util.Set;
028    
029    import javax.portlet.RenderResponse;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class UserGroupChecker extends RowChecker {
035    
036            public UserGroupChecker(RenderResponse renderResponse, Group group) {
037                    super(renderResponse);
038    
039                    _group = group;
040            }
041    
042            @Override
043            public boolean isChecked(Object obj) {
044                    User user = null;
045    
046                    if (obj instanceof User) {
047                            user = (User)obj;
048                    }
049                    else if (obj instanceof Object[]) {
050                            user = (User)((Object[])obj)[0];
051                    }
052                    else {
053                            throw new IllegalArgumentException(obj + " is not a User");
054                    }
055    
056                    try {
057                            return UserLocalServiceUtil.hasGroupUser(
058                                    _group.getGroupId(), user.getUserId());
059                    }
060                    catch (Exception e) {
061                            _log.error(e, e);
062    
063                            return false;
064                    }
065            }
066    
067            @Override
068            public boolean isDisabled(Object obj) {
069                    User user = (User)obj;
070    
071                    Set<Group> mandatoryGroups = MembershipPolicyUtil.getMandatoryGroups(
072                            user);
073    
074                    if ((isChecked(user) && mandatoryGroups.contains(_group)) ||
075                            (!isChecked(user) &&
076                             !MembershipPolicyUtil.isMembershipAllowed(_group, user))) {
077    
078                            return true;
079                    }
080                    else {
081                            try {
082                                    PermissionChecker permissionChecker =
083                                            PermissionThreadLocal.getPermissionChecker();
084    
085                                    if (MembershipPolicyUtil.isMembershipProtected(
086                                                    permissionChecker, _group, user)) {
087    
088                                            return true;
089                                    }
090                            }
091                            catch (Exception e) {
092                                    _log.error(e, e);
093                            }
094                    }
095    
096                    return super.isDisabled(obj);
097            }
098    
099            private static Log _log = LogFactoryUtil.getLog(UserGroupChecker.class);
100    
101            private Group _group;
102    
103    }