001    /**
002     * Copyright (c) 2000-2013 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.model.BaseModelListener;
018    import com.liferay.portal.model.ResourcePermission;
019    import com.liferay.portal.model.impl.ResourcePermissionModelImpl;
020    
021    /**
022     * @author Preston Crary
023     */
024    public class ResourcePermissionModelListener
025            extends BaseModelListener<ResourcePermission> {
026    
027            @Override
028            public void onAfterCreate(ResourcePermission resourcePermission) {
029                    _clearCache(resourcePermission);
030            }
031    
032            @Override
033            public void onAfterRemove(ResourcePermission resourcePermission) {
034                    _clearCache(resourcePermission);
035            }
036    
037            @Override
038            public void onAfterUpdate(ResourcePermission resourcePermission) {
039                    _clearCache(resourcePermission);
040            }
041    
042            @Override
043            public void onBeforeUpdate(ResourcePermission resourcePermission) {
044                    ResourcePermissionModelImpl resourcePermissionModelImpl =
045                            (ResourcePermissionModelImpl)resourcePermission;
046    
047                    long columnBitmask = resourcePermissionModelImpl.getColumnBitmask();
048    
049                    if ((columnBitmask & _CLEAR_ON_BEFORE_BITMASK) != 0) {
050                            PermissionCacheUtil.clearResourcePermissionCache(
051                                    resourcePermissionModelImpl.getOriginalScope(),
052                                    resourcePermissionModelImpl.getOriginalName(),
053                                    resourcePermissionModelImpl.getOriginalPrimKey());
054                    }
055            }
056    
057            private void _clearCache(ResourcePermission resourcePermission) {
058                    if (resourcePermission != null) {
059                            PermissionCacheUtil.clearResourcePermissionCache(
060                                    resourcePermission.getScope(), resourcePermission.getName(),
061                                    resourcePermission.getPrimKey());
062                    }
063            }
064    
065            private static final long _CLEAR_ON_BEFORE_BITMASK =
066                    ResourcePermissionModelImpl.NAME_COLUMN_BITMASK |
067                            ResourcePermissionModelImpl.PRIMKEY_COLUMN_BITMASK;
068    
069    }