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.rights;
017
018import java.util.Collections;
019import java.util.HashMap;
020import java.util.Map;
021
022import org.apache.avalon.framework.logger.AbstractLogEnabled;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026
027import org.ametys.core.observation.Event;
028import org.ametys.core.observation.ObservationManager;
029import org.ametys.core.observation.Observer;
030import org.ametys.core.right.ProfileAssignmentStorageExtensionPoint;
031import org.ametys.core.right.RightManager;
032import org.ametys.core.user.CurrentUserProvider;
033import org.ametys.plugins.repository.ModifiableTraversableAmetysObject;
034import org.ametys.web.ObservationConstants;
035import org.ametys.web.repository.site.Site;
036import org.ametys.web.repository.site.SiteType;
037import org.ametys.web.repository.site.SiteTypesExtensionPoint;
038
039/**
040 * {@link Observer} for observing site creation in order to assign the READER profile to anonymous or root contents and resources.
041 */
042public class SetReadAccessOnSiteAddedObserver extends AbstractLogEnabled implements Observer, Serviceable
043{
044    /** The right manager */
045    protected ProfileAssignmentStorageExtensionPoint _profileAssignmentStorageEP;
046    private ObservationManager _observationManager;
047    private CurrentUserProvider _currentUserProvider;
048    private SiteTypesExtensionPoint _siteTypesEP;
049
050    @Override
051    public void service(ServiceManager manager) throws ServiceException
052    {
053        _profileAssignmentStorageEP = (ProfileAssignmentStorageExtensionPoint) manager.lookup(ProfileAssignmentStorageExtensionPoint.ROLE);
054        _observationManager = (ObservationManager) manager.lookup(ObservationManager.ROLE);
055        _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
056        _siteTypesEP = (SiteTypesExtensionPoint) manager.lookup(SiteTypesExtensionPoint.ROLE);
057    }
058
059    @Override
060    public boolean supports(Event event)
061    {
062        return event.getId().equals(ObservationConstants.EVENT_SITE_ADDED);
063    }
064
065    @Override
066    public int getPriority(Event event)
067    {
068        return MIN_PRIORITY;
069    }
070
071    @Override
072    public void observe(Event event, Map<String, Object> transientVars) throws Exception
073    {
074        Map<String, Object> arguments = event.getArguments();
075        Site site = (Site)  arguments.get(ObservationConstants.ARGS_SITE);
076        
077        SiteType siteType = _siteTypesEP.getExtension(site.getType());
078        
079        // Set read access for non restricted sites.
080        if (!siteType.siteInitiallyRestricted())
081        {
082            // Set read access for Anonymous on root of contents
083            ModifiableTraversableAmetysObject rootOfContents = site.getRootContents();
084            
085            _profileAssignmentStorageEP.allowProfileToAnonymous(RightManager.READER_PROFILE_ID, rootOfContents);
086            
087            Map<String, Object> eventParams = new HashMap<>();
088            eventParams.put(org.ametys.core.ObservationConstants.ARGS_ACL_CONTEXT, rootOfContents);
089            eventParams.put(org.ametys.core.ObservationConstants.ARGS_ACL_PROFILES, Collections.singleton(RightManager.READER_PROFILE_ID));
090            
091            _observationManager.notify(new Event(org.ametys.core.ObservationConstants.EVENT_ACL_UPDATED, _currentUserProvider.getUser(), eventParams));
092            
093            // Set read access for Anonymous on root of resources
094            ModifiableTraversableAmetysObject rootResources = site.getRootResources();
095            
096            _profileAssignmentStorageEP.allowProfileToAnonymous(RightManager.READER_PROFILE_ID, rootResources);
097            
098            eventParams = new HashMap<>();
099            eventParams.put(org.ametys.core.ObservationConstants.ARGS_ACL_CONTEXT, rootResources);
100            eventParams.put(org.ametys.core.ObservationConstants.ARGS_ACL_PROFILES, Collections.singleton(RightManager.READER_PROFILE_ID));
101            
102            _observationManager.notify(new Event(org.ametys.core.ObservationConstants.EVENT_ACL_UPDATED, _currentUserProvider.getUser(), eventParams));
103        }
104    }
105}