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.web.ObservationConstants; 034import org.ametys.web.repository.site.SiteType; 035import org.ametys.web.repository.site.SiteTypesExtensionPoint; 036import org.ametys.web.repository.sitemap.Sitemap; 037 038/** 039 * {@link Observer} for observing sitemap addition in order to assign the READER profile as allowed to anonymous. 040 */ 041public class SetReadAccessOnSitemapAddedObserver extends AbstractLogEnabled implements Observer, Serviceable 042{ 043 /** The right manager */ 044 protected ProfileAssignmentStorageExtensionPoint _profileAssignmentStorageEP; 045 private ObservationManager _observationManager; 046 private CurrentUserProvider _currentUserProvider; 047 private SiteTypesExtensionPoint _siteTypesEP; 048 049 @Override 050 public void service(ServiceManager manager) throws ServiceException 051 { 052 _profileAssignmentStorageEP = (ProfileAssignmentStorageExtensionPoint) manager.lookup(ProfileAssignmentStorageExtensionPoint.ROLE); 053 _observationManager = (ObservationManager) manager.lookup(ObservationManager.ROLE); 054 _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE); 055 _siteTypesEP = (SiteTypesExtensionPoint) manager.lookup(SiteTypesExtensionPoint.ROLE); 056 } 057 058 @Override 059 public boolean supports(Event event) 060 { 061 return event.getId().equals(ObservationConstants.EVENT_SITEMAP_ADDED); 062 } 063 064 @Override 065 public int getPriority(Event event) 066 { 067 return MIN_PRIORITY; 068 } 069 070 @Override 071 public void observe(Event event, Map<String, Object> transientVars) throws Exception 072 { 073 Map<String, Object> arguments = event.getArguments(); 074 Sitemap sitemap = (Sitemap) arguments.get(ObservationConstants.ARGS_SITEMAP); 075 076 SiteType siteType = _siteTypesEP.getExtension(sitemap.getSite().getType()); 077 078 // Set read access for non restricted sites. 079 if (!siteType.siteInitiallyRestricted()) 080 { 081 _profileAssignmentStorageEP.allowProfileToAnonymous(RightManager.READER_PROFILE_ID, sitemap); 082 083 Map<String, Object> eventParams = new HashMap<>(); 084 eventParams.put(org.ametys.core.ObservationConstants.ARGS_ACL_CONTEXT, sitemap); 085 eventParams.put(org.ametys.core.ObservationConstants.ARGS_ACL_PROFILES, Collections.singleton(RightManager.READER_PROFILE_ID)); 086 087 _observationManager.notify(new Event(org.ametys.core.ObservationConstants.EVENT_ACL_UPDATED, _currentUserProvider.getUser(), eventParams)); 088 } 089 } 090}