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.rolesadmin.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.kernel.model.Role;
021    import com.liferay.portal.kernel.model.User;
022    import com.liferay.portal.kernel.security.membershippolicy.RoleMembershipPolicyUtil;
023    import com.liferay.portal.kernel.service.UserLocalServiceUtil;
024    
025    import javax.portlet.RenderResponse;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     * @author Drew Brokke
030     */
031    public class SetUserRoleChecker extends EmptyOnClickRowChecker {
032    
033            public SetUserRoleChecker(RenderResponse renderResponse, Role role) {
034                    super(renderResponse);
035    
036                    _role = role;
037            }
038    
039            @Override
040            public boolean isChecked(Object obj) {
041                    User user = (User)obj;
042    
043                    try {
044                            return UserLocalServiceUtil.hasRoleUser(
045                                    _role.getRoleId(), user.getUserId());
046                    }
047                    catch (Exception e) {
048                            _log.error(e, e);
049    
050                            return false;
051                    }
052            }
053    
054            @Override
055            public boolean isDisabled(Object obj) {
056                    User user = (User)obj;
057    
058                    try {
059                            if (isChecked(user) ||
060                                    !RoleMembershipPolicyUtil.isRoleAllowed(
061                                            user.getUserId(), _role.getRoleId())) {
062    
063                                    return true;
064                            }
065                    }
066                    catch (Exception e) {
067                            _log.error(e, e);
068                    }
069    
070                    return super.isDisabled(obj);
071            }
072    
073            private static final Log _log = LogFactoryUtil.getLog(
074                    SetUserRoleChecker.class);
075    
076            private final Role _role;
077    
078    }