001/*
002 *  Copyright 2026 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 org.apache.avalon.framework.context.Context;
019import org.apache.avalon.framework.context.ContextException;
020import org.apache.avalon.framework.context.Contextualizable;
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.components.ContextHelper;
024import org.apache.cocoon.environment.Request;
025
026import org.ametys.cms.repository.Content;
027import org.ametys.cms.rights.ContentInteractionAccessController;
028import org.ametys.web.WebHelper;
029import org.ametys.web.repository.site.Site;
030import org.ametys.web.repository.site.SiteManager;
031
032/**
033 * Access controller for reactions and report for web contents, taking into account the site configuration.
034 * See {@link ContentInteractionAccessController}.
035 */
036public class WebContentInteractionAccessController extends ContentInteractionAccessController implements Contextualizable
037{
038    private SiteManager _siteManager;
039    private Context _context;
040
041    @Override
042    public void service(ServiceManager manager) throws ServiceException
043    {
044        super.service(manager);
045        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
046    }
047    
048    public void contextualize(Context context) throws ContextException
049    {
050        _context = context;
051    }
052    
053    @Override
054    protected boolean isReadAccessBased(Object object)
055    {
056        Request request = ContextHelper.getRequest(_context);
057        String siteName = WebHelper.getSiteName(request, (Content) object);
058        
059        if (siteName != null)
060        {
061            Site site = _siteManager.getSite(siteName);
062            
063            String accessType = site.getValueOrDefault("site-contents-interaction-access");
064            if ("global".equals(accessType))
065            {
066                return super.isReadAccessBased(object);
067            }
068            else 
069            {
070                return "read-access".equals(accessType);
071            }
072        }
073        
074        return super.isReadAccessBased(object);
075    }
076}