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.portal.security.permission;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
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.security.auth.PrincipalThreadLocal;
022    import com.liferay.portal.util.PropsValues;
023    import com.liferay.registry.collections.ServiceTrackerCollections;
024    import com.liferay.registry.collections.ServiceTrackerMap;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class PermissionCheckerUtil {
030    
031            public static Boolean containsResourcePermission(
032                            PermissionChecker permissionChecker, String className, long classPK,
033                            String actionId)
034                    throws PortalException {
035    
036                    ResourcePermissionChecker resourcePermissionChecker =
037                            _serviceTrackerMap.getService(className);
038    
039                    if (resourcePermissionChecker == null) {
040                            return null;
041                    }
042    
043                    Boolean resource = resourcePermissionChecker.checkResource(
044                            permissionChecker, classPK, actionId);
045    
046                    if (resource != null) {
047                            return resource.booleanValue();
048                    }
049    
050                    return null;
051            }
052    
053            public static void setThreadValues(User user) {
054                    if (user == null) {
055                            PrincipalThreadLocal.setName(null);
056                            PermissionThreadLocal.setPermissionChecker(null);
057    
058                            return;
059                    }
060    
061                    long userId = user.getUserId();
062    
063                    String name = String.valueOf(userId);
064    
065                    PrincipalThreadLocal.setName(name);
066    
067                    try {
068                            PermissionChecker permissionChecker =
069                                    PermissionThreadLocal.getPermissionChecker();
070    
071                            if (permissionChecker == null) {
072                                    Class<?> clazz = Class.forName(PropsValues.PERMISSIONS_CHECKER);
073    
074                                    permissionChecker = (PermissionChecker)clazz.newInstance();
075                            }
076    
077                            permissionChecker.init(user);
078    
079                            PermissionThreadLocal.setPermissionChecker(permissionChecker);
080                    }
081                    catch (Exception e) {
082                            _log.error(e, e);
083                    }
084            }
085    
086            private static final Log _log = LogFactoryUtil.getLog(
087                    PermissionCheckerUtil.class);
088    
089            private static final ServiceTrackerMap<String, ResourcePermissionChecker>
090                    _serviceTrackerMap = ServiceTrackerCollections.singleValueMap(
091                            ResourcePermissionChecker.class, "model.class.name");
092    
093            static {
094                    _serviceTrackerMap.open();
095            }
096    
097    }