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.util.AutoResetThreadLocal;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    
021    import java.util.HashSet;
022    import java.util.Set;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     * @author Raymond Aug??
027     */
028    public class PermissionThreadLocal {
029    
030            public static PermissionChecker getPermissionChecker() {
031                    return _permissionChecker.get();
032            }
033    
034            public static boolean isAddResource() {
035                    return _addResource.get();
036            }
037    
038            public static boolean isFlushResourceBlockEnabled(
039                    long companyId, long groupId, String name) {
040    
041                    Set<String> set = _flushResourceBlockEnabled.get();
042    
043                    StringBundler sb = new StringBundler(5);
044    
045                    sb.append(companyId);
046                    sb.append(StringPool.UNDERLINE);
047                    sb.append(groupId);
048                    sb.append(StringPool.UNDERLINE);
049                    sb.append(name);
050    
051                    return !set.contains(sb.toString());
052            }
053    
054            public static boolean isFlushResourcePermissionEnabled(
055                    String resourceName, String primKey) {
056    
057                    Set<String> set = _flushResourcePermissionEnabled.get();
058    
059                    return !set.contains(resourceName + StringPool.UNDERLINE + primKey);
060            }
061    
062            public static void setAddResource(boolean addResource) {
063                    _addResource.set(addResource);
064            }
065    
066            public static void setFlushResourceBlockEnabled(
067                    long companyId, long groupId, String name, boolean enabled) {
068    
069                    Set<String> set = _flushResourceBlockEnabled.get();
070    
071                    StringBundler sb = new StringBundler(5);
072    
073                    sb.append(companyId);
074                    sb.append(StringPool.UNDERLINE);
075                    sb.append(groupId);
076                    sb.append(StringPool.UNDERLINE);
077                    sb.append(name);
078    
079                    if (enabled) {
080                            set.remove(sb.toString());
081                    }
082                    else {
083                            set.add(sb.toString());
084                    }
085            }
086    
087            public static void setFlushResourcePermissionEnabled(
088                    String resourceName, String primKey, boolean enabled) {
089    
090                    Set<String> set = _flushResourcePermissionEnabled.get();
091    
092                    if (enabled) {
093                            set.remove(resourceName + StringPool.UNDERLINE + primKey);
094                    }
095                    else {
096                            set.add(resourceName + StringPool.UNDERLINE + primKey);
097                    }
098            }
099    
100            public static void setPermissionChecker(
101                    PermissionChecker permissionChecker) {
102    
103                    _permissionChecker.set(permissionChecker);
104            }
105    
106            private static final ThreadLocal<Boolean> _addResource =
107                    new AutoResetThreadLocal<>(
108                            PermissionThreadLocal.class + "._addResource", true);
109            private static final ThreadLocal<Set<String>> _flushResourceBlockEnabled =
110                    new AutoResetThreadLocal<Set<String>>(
111                            PermissionThreadLocal.class + "._flushResourceBlockEnabled",
112                            new HashSet<String>());
113            private static final ThreadLocal<Set<String>>
114                    _flushResourcePermissionEnabled = new AutoResetThreadLocal<Set<String>>(
115                            PermissionThreadLocal.class +
116                                    "._flushResourcePermissionEnabled",
117                            new HashSet<String>());
118    
119            private static final ThreadLocal<PermissionChecker> _permissionChecker =
120                    new AutoResetThreadLocal<PermissionChecker>(
121                            PermissionThreadLocal.class + "._permissionChecker") {
122    
123                            @Override
124                            protected PermissionChecker copy(
125                                    PermissionChecker permissionChecker) {
126    
127                                    return permissionChecker;
128                            }
129    
130                    };
131    
132    }