001    /**
002     * Copyright (c) 2000-2010 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                                    permissionChecker = (PermissionChecker)Class.forName(
048                                            PropsValues.PERMISSIONS_CHECKER).newInstance();
049                            }
050    
051                            permissionChecker.init(user, _CHECK_GUEST);
052    
053                            PermissionThreadLocal.setPermissionChecker(permissionChecker);
054                    }
055                    catch (Exception e) {
056                            _log.error(e);
057                    }
058            }
059    
060            private static boolean _CHECK_GUEST = true;
061    
062            private static Log _log = LogFactoryUtil.getLog(
063                    PermissionCheckerUtil.class);
064    
065    }