001/*
002 *  Copyright 2025 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.List;
019import java.util.Map;
020
021import org.apache.http.NameValuePair;
022import org.apache.http.message.BasicNameValuePair;
023
024import org.ametys.core.ObservationConstants;
025import org.ametys.core.observation.AsyncObserver;
026import org.ametys.core.observation.Event;
027import org.ametys.core.user.UserIdentity;
028import org.ametys.runtime.plugin.component.AbstractLogEnabled;
029import org.ametys.web.cache.CacheHelper;
030
031/**
032 * Observer to propagate user event to the site applications
033 */
034public class InvalidateUserCacheObserver extends AbstractLogEnabled implements AsyncObserver
035{
036    public int getPriority()
037    {
038        return Integer.MAX_VALUE;
039    }
040
041    public boolean supports(Event event)
042    {
043        String id = event.getId();
044        return ObservationConstants.EVENT_USER_DELETED.equals(id)
045            || ObservationConstants.EVENT_USER_UPDATED.equals(id);
046    }
047
048    public void observe(Event event, Map<String, Object> transientVars) throws Exception
049    {
050        List<NameValuePair> params = List.of(
051                new BasicNameValuePair("id", event.getId()),
052                new BasicNameValuePair("issuer", UserIdentity.userIdentityToString(event.getIssuer())),
053                new BasicNameValuePair("args." + ObservationConstants.ARGS_USER, UserIdentity.userIdentityToString((UserIdentity) event.getArguments().get(ObservationConstants.ARGS_USER)))
054        );
055        
056        CacheHelper.callWS("/_invalidate-user-directory-cache", params, getLogger());
057    }
058}