001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *     Thomas Roger
018 */
019package org.nuxeo.ecm.multi.tenant;
020
021import org.nuxeo.ecm.core.api.DocumentModel;
022import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
023import org.nuxeo.ecm.core.api.model.Property;
024import org.nuxeo.ecm.core.event.Event;
025import org.nuxeo.ecm.core.event.EventListener;
026import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
027import org.nuxeo.ecm.platform.usermanager.UserManagerImpl;
028import org.nuxeo.runtime.api.Framework;
029import org.nuxeo.runtime.services.event.EventService;
030
031/**
032 * Listeners invalidating the principals cache when the tenant administrators are changed.
033 *
034 * @since 5.9.2
035 */
036public class TenantAdministratorsListener implements EventListener {
037
038    @Override
039    public void handleEvent(Event event) {
040        String eventName = event.getName();
041        if (!DocumentEventTypes.BEFORE_DOC_UPDATE.equals(eventName)
042                || !(event.getContext() instanceof DocumentEventContext)) {
043            return;
044        }
045
046        DocumentEventContext docCtx = (DocumentEventContext) event.getContext();
047        DocumentModel doc = docCtx.getSourceDocument();
048        if (doc.hasFacet(Constants.TENANT_CONFIG_FACET)) {
049            Property property = doc.getProperty(Constants.TENANT_ADMINISTRATORS_PROPERTY);
050            if (property.isDirty()) {
051                // flush the principals cache
052                EventService eventService = Framework.getService(EventService.class);
053                eventService.sendEvent(new org.nuxeo.runtime.services.event.Event(UserManagerImpl.USERMANAGER_TOPIC,
054                        "invalidateAllPrincipals", null, null));
055            }
056        }
057    }
058
059}