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.model.BaseModelListener;
018    import com.liferay.portal.kernel.model.ResourceBlock;
019    import com.liferay.portal.kernel.model.ResourceConstants;
020    import com.liferay.portal.model.impl.ResourceBlockModelImpl;
021    
022    /**
023     * @author Preston Crary
024     */
025    public class ResourceBlockModelListener
026            extends BaseModelListener<ResourceBlock> {
027    
028            @Override
029            public void onAfterCreate(ResourceBlock resourceBlock) {
030                    _clearCache(resourceBlock);
031            }
032    
033            @Override
034            public void onAfterRemove(ResourceBlock resourceBlock) {
035                    _clearCache(resourceBlock);
036            }
037    
038            @Override
039            public void onAfterUpdate(ResourceBlock resourceBlock) {
040                    _clearCache(resourceBlock);
041            }
042    
043            @Override
044            public void onBeforeUpdate(ResourceBlock resourceBlock) {
045                    ResourceBlockModelImpl resourceBlockModelImpl =
046                            (ResourceBlockModelImpl)resourceBlock;
047    
048                    long columnBitmask = resourceBlockModelImpl.getColumnBitmask();
049    
050                    if ((columnBitmask & _CLEAR_ON_BEFORE_BITMASK) != 0) {
051                            PermissionCacheUtil.clearResourceBlockCache(
052                                    resourceBlockModelImpl.getOriginalCompanyId(),
053                                    resourceBlockModelImpl.getOriginalGroupId(),
054                                    resourceBlockModelImpl.getOriginalName());
055                    }
056            }
057    
058            private void _clearCache(ResourceBlock resourceBlock) {
059                    if (resourceBlock != null) {
060                            PermissionCacheUtil.clearResourceBlockCache(
061                                    resourceBlock.getCompanyId(), resourceBlock.getGroupId(),
062                                    resourceBlock.getName());
063    
064                            PermissionCacheUtil.clearResourcePermissionCache(
065                                    ResourceConstants.SCOPE_INDIVIDUAL, resourceBlock.getName(),
066                                    String.valueOf(resourceBlock.getPrimaryKey()));
067                    }
068            }
069    
070            private static final long _CLEAR_ON_BEFORE_BITMASK =
071                    ResourceBlockModelImpl.COMPANYID_COLUMN_BITMASK |
072                            ResourceBlockModelImpl.GROUPID_COLUMN_BITMASK |
073                                    ResourceBlockModelImpl.NAME_COLUMN_BITMASK;
074    
075    }