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.cache.MultiVMPoolUtil;
018    import com.liferay.portal.kernel.cache.PortalCache;
019    import com.liferay.portal.kernel.lar.ExportImportThreadLocal;
020    import com.liferay.portal.kernel.util.HashUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.util.PropsValues;
023    
024    import java.io.Serializable;
025    
026    /**
027     * @author Charles May
028     * @author Michael Young
029     * @author Shuyang Zhou
030     * @author Connor McKay
031     * @author L??szl?? Csontos
032     */
033    public class PermissionCacheUtil {
034    
035            public static final String PERMISSION_CACHE_NAME =
036                    PermissionCacheUtil.class.getName() + "_PERMISSION";
037    
038            public static final String PERMISSION_CHECKER_BAG_CACHE_NAME =
039                    PermissionCacheUtil.class.getName() + "_PERMISSION_CHECKER_BAG";
040    
041            public static final String RESOURCE_BLOCK_IDS_BAG_CACHE_NAME =
042                    PermissionCacheUtil.class.getName() + "_RESOURCE_BLOCK_IDS_BAG";
043    
044            public static final String USER_PERMISSION_CHECKER_BAG_CACHE_NAME =
045                    PermissionCacheUtil.class.getName() + "_USER_PERMISSION_CHECKER_BAG";
046    
047            public static void clearCache() {
048                    if (ExportImportThreadLocal.isImportInProcess() ||
049                            !PermissionThreadLocal.isFlushEnabled()) {
050    
051                            return;
052                    }
053    
054                    _permissionCheckerBagPortalCache.removeAll();
055                    _permissionPortalCache.removeAll();
056                    _resourceBlockIdsBagCache.removeAll();
057                    _userPermissionCheckerBagPortalCache.removeAll();
058            }
059    
060            public static PermissionCheckerBag getBag(long userId, long groupId) {
061                    BagKey bagKey = new BagKey(userId, groupId);
062    
063                    return _permissionCheckerBagPortalCache.get(bagKey);
064            }
065    
066            public static Boolean getPermission(
067                    long userId, boolean signedIn, long groupId, String name,
068                    String primKey, String actionId) {
069    
070                    PermissionKey permissionKey = new PermissionKey(
071                            userId, signedIn, groupId, name, primKey, actionId);
072    
073                    return _permissionPortalCache.get(permissionKey);
074            }
075    
076            public static ResourceBlockIdsBag getResourceBlockIdsBag(
077                    long companyId, long groupId, long userId, String name) {
078    
079                    ResourceBlockIdsBagKey resourceBlockIdsBagKey =
080                            new ResourceBlockIdsBagKey(companyId, groupId, userId, name);
081    
082                    return _resourceBlockIdsBagCache.get(resourceBlockIdsBagKey);
083            }
084    
085            public static UserPermissionCheckerBag getUserBag(long userId) {
086                    return _userPermissionCheckerBagPortalCache.get(userId);
087            }
088    
089            public static void putBag(
090                    long userId, long groupId, PermissionCheckerBag bag) {
091    
092                    if (bag == null) {
093                            return;
094                    }
095    
096                    BagKey bagKey = new BagKey(userId, groupId);
097    
098                    _permissionCheckerBagPortalCache.put(bagKey, bag);
099            }
100    
101            public static void putPermission(
102                    long userId, boolean signedIn, long groupId, String name,
103                    String primKey, String actionId, Boolean value) {
104    
105                    PermissionKey permissionKey = new PermissionKey(
106                            userId, signedIn, groupId, name, primKey, actionId);
107    
108                    _permissionPortalCache.put(permissionKey, value);
109            }
110    
111            public static void putResourceBlockIdsBag(
112                    long companyId, long groupId, long userId, String name,
113                    ResourceBlockIdsBag resourceBlockIdsBag) {
114    
115                    if (resourceBlockIdsBag == null) {
116                            return;
117                    }
118    
119                    ResourceBlockIdsBagKey resourceBlockIdsBagKey =
120                            new ResourceBlockIdsBagKey(companyId, groupId, userId, name);
121    
122                    _resourceBlockIdsBagCache.put(
123                            resourceBlockIdsBagKey, resourceBlockIdsBag);
124            }
125    
126            public static void putUserBag(
127                    long userId, UserPermissionCheckerBag userPermissionCheckerBag) {
128    
129                    _userPermissionCheckerBagPortalCache.put(
130                            userId, userPermissionCheckerBag);
131            }
132    
133            private static final PortalCache<BagKey, PermissionCheckerBag>
134                    _permissionCheckerBagPortalCache = MultiVMPoolUtil.getCache(
135                            PERMISSION_CHECKER_BAG_CACHE_NAME,
136                            PropsValues.PERMISSIONS_OBJECT_BLOCKING_CACHE);
137            private static final PortalCache<PermissionKey, Boolean>
138                    _permissionPortalCache = MultiVMPoolUtil.getCache(
139                            PERMISSION_CACHE_NAME,
140                            PropsValues.PERMISSIONS_OBJECT_BLOCKING_CACHE);
141            private static final
142                    PortalCache<ResourceBlockIdsBagKey, ResourceBlockIdsBag>
143                            _resourceBlockIdsBagCache = MultiVMPoolUtil.getCache(
144                                    RESOURCE_BLOCK_IDS_BAG_CACHE_NAME,
145                                    PropsValues.PERMISSIONS_OBJECT_BLOCKING_CACHE);
146            private static final PortalCache<Long, UserPermissionCheckerBag>
147                    _userPermissionCheckerBagPortalCache = MultiVMPoolUtil.getCache(
148                            USER_PERMISSION_CHECKER_BAG_CACHE_NAME,
149                            PropsValues.PERMISSIONS_OBJECT_BLOCKING_CACHE);
150    
151            private static class BagKey implements Serializable {
152    
153                    public BagKey(long userId, long groupId) {
154                            _userId = userId;
155                            _groupId = groupId;
156                    }
157    
158                    @Override
159                    public boolean equals(Object obj) {
160                            BagKey bagKey = (BagKey)obj;
161    
162                            if ((bagKey._userId == _userId) && (bagKey._groupId == _groupId)) {
163                                    return true;
164                            }
165                            else {
166                                    return false;
167                            }
168                    }
169    
170                    @Override
171                    public int hashCode() {
172                            return (int)(_userId * 11 + _groupId);
173                    }
174    
175                    private static final long serialVersionUID = 1L;
176    
177                    private final long _groupId;
178                    private final long _userId;
179    
180            }
181    
182            private static class PermissionKey implements Serializable {
183    
184                    public PermissionKey(
185                            long userId, boolean signedIn, long groupId, String name,
186                            String primKey, String actionId) {
187    
188                            _userId = userId;
189                            _signedIn = signedIn;
190                            _groupId = groupId;
191                            _name = name;
192                            _primKey = primKey;
193                            _actionId = actionId;
194                    }
195    
196                    @Override
197                    public boolean equals(Object obj) {
198                            PermissionKey permissionKey = (PermissionKey)obj;
199    
200                            if ((permissionKey._userId == _userId) &&
201                                    (permissionKey._signedIn == _signedIn) &&
202                                    (permissionKey._groupId == _groupId) &&
203                                    Validator.equals(permissionKey._name, _name) &&
204                                    Validator.equals(permissionKey._primKey, _primKey) &&
205                                    Validator.equals(permissionKey._actionId, _actionId)) {
206    
207                                    return true;
208                            }
209                            else {
210                                    return false;
211                            }
212                    }
213    
214                    @Override
215                    public int hashCode() {
216                            int hashCode = HashUtil.hash(0, _userId);
217    
218                            hashCode = HashUtil.hash(hashCode, _signedIn);
219                            hashCode = HashUtil.hash(hashCode, _groupId);
220                            hashCode = HashUtil.hash(hashCode, _name);
221                            hashCode = HashUtil.hash(hashCode, _primKey);
222                            hashCode = HashUtil.hash(hashCode, _actionId);
223    
224                            return hashCode;
225                    }
226    
227                    private static final long serialVersionUID = 1L;
228    
229                    private final String _actionId;
230                    private final long _groupId;
231                    private final String _name;
232                    private final String _primKey;
233                    private final boolean _signedIn;
234                    private final long _userId;
235    
236            }
237    
238            private static class ResourceBlockIdsBagKey implements Serializable {
239    
240                    public ResourceBlockIdsBagKey(
241                            long companyId, long groupId, long userId, String name) {
242    
243                            _companyId = companyId;
244                            _groupId = groupId;
245                            _userId = userId;
246                            _name = name;
247                    }
248    
249                    @Override
250                    public boolean equals(Object obj) {
251                            ResourceBlockIdsBagKey resourceBlockIdsKey =
252                                    (ResourceBlockIdsBagKey)obj;
253    
254                            if ((resourceBlockIdsKey._companyId == _companyId) &&
255                                    (resourceBlockIdsKey._groupId == _groupId) &&
256                                    (resourceBlockIdsKey._userId == _userId) &&
257                                    Validator.equals(resourceBlockIdsKey._name, _name)) {
258    
259                                    return true;
260                            }
261                            else {
262                                    return false;
263                            }
264                    }
265    
266                    @Override
267                    public int hashCode() {
268                            int hashCode = HashUtil.hash(0, _companyId);
269    
270                            hashCode = HashUtil.hash(hashCode, _groupId);
271                            hashCode = HashUtil.hash(hashCode, _userId);
272                            hashCode = HashUtil.hash(hashCode, _name);
273    
274                            return hashCode;
275                    }
276    
277                    private static final long serialVersionUID = 1L;
278    
279                    private final long _companyId;
280                    private final long _groupId;
281                    private final String _name;
282                    private final long _userId;
283    
284            }
285    
286    }