001    /**
002     * Copyright (c) 2000-2010 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.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    }