001/*
002 *  Copyright 2022 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.usermanagement.observer;
017
018import java.util.Collection;
019import java.util.Map;
020import java.util.Set;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025import org.apache.commons.collections.CollectionUtils;
026
027import org.ametys.core.observation.Event;
028import org.ametys.plugins.repository.AmetysObjectResolver;
029import org.ametys.web.ObservationConstants;
030import org.ametys.web.cache.AbstractCacheObserver;
031import org.ametys.web.cache.CacheHelper;
032import org.ametys.web.repository.page.Page;
033import org.ametys.web.usermanagement.UserSignupManager;
034
035/**
036 * Invalidate the site cache on front office instances when a signup or a change password page has been added, removed or updated
037 */
038public class InvalidateSiteCacheOnSignupPageUpdatedObserver extends AbstractCacheObserver implements Serviceable
039{
040    private AmetysObjectResolver _resolver;
041
042    public void service(ServiceManager manager) throws ServiceException
043    {
044        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
045    }
046    
047    public boolean supports(Event event)
048    {
049        String eventType = event.getId();
050        Map<String, Object> eventArgs = event.getArguments();
051        
052        if (eventType.equals(ObservationConstants.EVENT_PAGE_UPDATED) && eventArgs.containsKey(ObservationConstants.ARGS_PAGE_TAGS))
053        {
054            @SuppressWarnings("unchecked")
055            Set<String> pageTags = (Set<String>) eventArgs.get(ObservationConstants.ARGS_PAGE_TAGS);
056            @SuppressWarnings("unchecked")
057            Set<String> pageOldTags = (Set<String>) eventArgs.get(ObservationConstants.ARGS_PAGE_OLD_TAGS);
058            
059            // Invalidate cache if a signup or change password tag has been added or removed on a page
060            Collection differences = CollectionUtils.disjunction(pageTags, pageOldTags);
061            return differences.contains(UserSignupManager.SIGNUP_PAGE_TAG_NAME) || differences.contains(UserSignupManager.CHANGE_PASSWORD_PAGE_TAG_NAME);
062        }
063        else if ((eventType.equals(ObservationConstants.EVENT_ZONEITEM_DELETED) 
064                    || eventType.equals(ObservationConstants.EVENT_ZONEITEM_ADDED) 
065                    || eventType.equals(ObservationConstants.EVENT_SERVICE_MODIFIED))
066                && eventArgs.containsKey(ObservationConstants.ARGS_ZONE_ITEM_SERVICE))
067        {
068            // Invalidate cache if the signup or change password service has been added, update or removed
069            String serviceId = (String) eventArgs.get(ObservationConstants.ARGS_ZONE_ITEM_SERVICE);
070            return UserSignupManager.SIGNUP_PAGE_SERVICE_ID.equals(serviceId) || UserSignupManager.CHANGE_PASSWORD_PAGE_SERVICE_ID.equals(serviceId);
071        }
072        else if (eventType.equals(ObservationConstants.EVENT_PAGE_DELETING))
073        {
074            String pageId = (String) event.getArguments().get(ObservationConstants.ARGS_PAGE_ID);
075            if (_resolver.hasAmetysObjectForId(pageId))
076            {
077                Page page = _resolver.resolveById(pageId);
078                
079                // Invalidate cache if a signup or change password page has been removed
080                Set<String> tags = page.getTags();
081                return tags.contains(UserSignupManager.SIGNUP_PAGE_TAG_NAME) || tags.contains(UserSignupManager.CHANGE_PASSWORD_PAGE_TAG_NAME);
082            }
083        }
084        
085        return false;
086    }
087
088    public void observe(Event event, Map<String, Object> transientVars) throws Exception
089    {
090        try
091        {
092            // Signup page or change password page has been added, updated or removed
093            // Site cache should be reset
094            CacheHelper.testWS("/_resetCache", getLogger());
095        }
096        catch (Exception e)
097        {
098            getLogger().error("Unable to invalidate site cache", e);
099        }
100
101    }
102
103}