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