001/*
002 *  Copyright 2016 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.web.cache.pageelement;
017
018import java.util.Collection;
019import java.util.Map;
020
021import org.ametys.core.observation.Event;
022import org.ametys.core.right.RightManager;
023import org.ametys.web.repository.content.WebContent;
024import org.ametys.web.repository.page.PagesContainer;
025
026/**
027 * Base class for {@link PageElementCachePolicy} based on event ids and site aware ACL context.<br>
028 * When ACL is updated, the returned value will be REMOVE only if reading access has updated.
029 * Implementations should provide a list of event ids for which the cache should be removed.
030 * Be aware that for all other events, the returned value will be KEEP. 
031 */
032public abstract class AbstractACLPageElementCachePolicy extends AbstractSimplePageElementCachePolicy
033{
034    @Override
035    protected boolean _supports(Event event, String workspace)
036    {
037        boolean supports = super._supports(event, workspace);
038        
039        if (supports && event.getId().equals(org.ametys.core.ObservationConstants.EVENT_ACL_UPDATED))
040        {
041            // When ACL was updated, returns true only if the ACL context is site aware and the reading access has been updated
042            Map<String, Object> args = event.getArguments();
043            Object object = args.get(org.ametys.core.ObservationConstants.ARGS_ACL_CONTEXT);
044            @SuppressWarnings("unchecked")
045            Collection<String> profileIds = (Collection<String>) args.get(org.ametys.core.ObservationConstants.ARGS_ACL_PROFILES);
046            
047            supports = object != null && (object instanceof PagesContainer || object instanceof WebContent) && profileIds.contains(RightManager.READER_PROFILE_ID);
048        }
049        
050        return supports;
051    }
052
053}