001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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     */
034    public class UserOrganizationChecker extends RowChecker {
035    
036            public UserOrganizationChecker(
037                    RenderResponse renderResponse, Organization organization) {
038    
039                    super(renderResponse);
040    
041                    _organization = organization;
042            }
043    
044            @Override
045            public boolean isChecked(Object obj) {
046                    User user = (User)obj;
047    
048                    try {
049                            return UserLocalServiceUtil.hasOrganizationUser(
050                                    _organization.getOrganizationId(), user.getUserId());
051                    }
052                    catch (Exception e) {
053                            _log.error(e, e);
054    
055                            return false;
056                    }
057            }
058    
059            @Override
060            public boolean isDisabled(Object obj) {
061                    if (!PropsValues.ORGANIZATIONS_ASSIGNMENT_STRICT) {
062                            return false;
063                    }
064    
065                    User user = (User)obj;
066    
067                    try {
068                            PermissionChecker permissionChecker =
069                                    PermissionThreadLocal.getPermissionChecker();
070    
071                            return !UserPermissionUtil.contains(
072                                    permissionChecker, user.getUserId(), ActionKeys.UPDATE);
073                    }
074                    catch (Exception e) {
075                            _log.error(e, e);
076    
077                            return false;
078                    }
079            }
080    
081            private static Log _log = LogFactoryUtil.getLog(
082                    UserOrganizationChecker.class);
083    
084            private Organization _organization;
085    
086    }