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