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.usersadmin.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.Organization;
021    import com.liferay.portal.model.User;
022    import com.liferay.portal.security.permission.ActionKeys;
023    import com.liferay.portal.security.permission.PermissionChecker;
024    import com.liferay.portal.security.permission.PermissionThreadLocal;
025    import com.liferay.portal.service.UserLocalServiceUtil;
026    import com.liferay.portal.service.permission.UserPermissionUtil;
027    import com.liferay.portal.util.PropsValues;
028    
029    import javax.portlet.RenderResponse;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     * @author Jorge Ferrer
034     */
035    public class UserOrganizationChecker extends RowChecker {
036    
037            public UserOrganizationChecker(
038                    RenderResponse renderResponse, Organization organization) {
039    
040                    super(renderResponse);
041    
042                    _organization = organization;
043            }
044    
045            @Override
046            public boolean isChecked(Object obj) {
047                    User user = (User)obj;
048    
049                    try {
050                            return UserLocalServiceUtil.hasOrganizationUser(
051                                    _organization.getOrganizationId(), user.getUserId());
052                    }
053                    catch (Exception e) {
054                            _log.error(e, e);
055    
056                            return false;
057                    }
058            }
059    
060            @Override
061            public boolean isDisabled(Object obj) {
062                    if (!PropsValues.ORGANIZATIONS_ASSIGNMENT_STRICT) {
063                            return false;
064                    }
065    
066                    User user = (User)obj;
067    
068                    try {
069                            PermissionChecker permissionChecker =
070                                    PermissionThreadLocal.getPermissionChecker();
071    
072                            return !UserPermissionUtil.contains(
073                                    permissionChecker, user.getUserId(), ActionKeys.UPDATE);
074                    }
075                    catch (Exception e) {
076                            _log.error(e, e);
077    
078                            return false;
079                    }
080            }
081    
082            private static Log _log = LogFactoryUtil.getLog(
083                    UserOrganizationChecker.class);
084    
085            private Organization _organization;
086    
087    }