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.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.model.User;
020    import com.liferay.portal.kernel.security.auth.PrincipalThreadLocal;
021    import com.liferay.portal.kernel.security.permission.PermissionChecker;
022    import com.liferay.portal.kernel.security.permission.PermissionThreadLocal;
023    import com.liferay.portal.util.PropsValues;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class PermissionCheckerUtil {
029    
030            public static void setThreadValues(User user) {
031                    if (user == null) {
032                            PrincipalThreadLocal.setName(null);
033                            PermissionThreadLocal.setPermissionChecker(null);
034    
035                            return;
036                    }
037    
038                    long userId = user.getUserId();
039    
040                    String name = String.valueOf(userId);
041    
042                    PrincipalThreadLocal.setName(name);
043    
044                    try {
045                            PermissionChecker permissionChecker =
046                                    PermissionThreadLocal.getPermissionChecker();
047    
048                            if (permissionChecker == null) {
049                                    Class<?> clazz = Class.forName(PropsValues.PERMISSIONS_CHECKER);
050    
051                                    permissionChecker = (PermissionChecker)clazz.newInstance();
052                            }
053    
054                            permissionChecker.init(user);
055    
056                            PermissionThreadLocal.setPermissionChecker(permissionChecker);
057                    }
058                    catch (Exception e) {
059                            _log.error(e, e);
060                    }
061            }
062    
063            private static final Log _log = LogFactoryUtil.getLog(
064                    PermissionCheckerUtil.class);
065    
066    }