001    /**
002     * Copyright (c) 2000-present 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.EmptyOnClickRowChecker;
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.membershippolicy.UserGroupMembershipPolicyUtil;
023    import com.liferay.portal.service.UserLocalServiceUtil;
024    
025    import javax.portlet.RenderResponse;
026    
027    /**
028     * @author Charles May
029     * @author Pei-Jung Lan
030     */
031    public class SetUserUserGroupChecker extends EmptyOnClickRowChecker {
032    
033            public SetUserUserGroupChecker(
034                    RenderResponse renderResponse, UserGroup userGroup) {
035    
036                    super(renderResponse);
037    
038                    _userGroup = userGroup;
039            }
040    
041            @Override
042            public boolean isChecked(Object obj) {
043                    User user = (User)obj;
044    
045                    try {
046                            return UserLocalServiceUtil.hasUserGroupUser(
047                                    _userGroup.getUserGroupId(), user.getUserId());
048                    }
049                    catch (Exception e) {
050                            _log.error(e, e);
051    
052                            return false;
053                    }
054            }
055    
056            @Override
057            public boolean isDisabled(Object obj) {
058                    User user = (User)obj;
059    
060                    try {
061                            if (isChecked(user) ||
062                                    !UserGroupMembershipPolicyUtil.isMembershipAllowed(
063                                            user.getUserId(), _userGroup.getUserGroupId())) {
064    
065                                    return true;
066                            }
067                    }
068                    catch (Exception e) {
069                            _log.error(e, e);
070                    }
071    
072                    return super.isDisabled(obj);
073            }
074    
075            private static final Log _log = LogFactoryUtil.getLog(
076                    SetUserUserGroupChecker.class);
077    
078            private final UserGroup _userGroup;
079    
080    }