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.plugins.workspaces.project.actions;
017
018import java.util.Map;
019import java.util.Set;
020
021import org.apache.avalon.framework.parameters.Parameters;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025import org.apache.cocoon.acting.AbstractAction;
026import org.apache.cocoon.environment.Redirector;
027import org.apache.cocoon.environment.SourceResolver;
028import org.apache.commons.lang3.StringUtils;
029
030import org.ametys.core.user.CurrentUserProvider;
031import org.ametys.core.user.UserIdentity;
032import org.ametys.plugins.workspaces.members.ProjectMemberManager;
033import org.ametys.plugins.workspaces.project.ProjectManager;
034import org.ametys.plugins.workspaces.project.ProjectWorkspaceSiteType;
035import org.ametys.plugins.workspaces.project.objects.Project;
036import org.ametys.plugins.workspaces.project.objects.Project.InscriptionStatus;
037import org.ametys.web.repository.site.Site;
038import org.ametys.web.repository.site.SiteManager;
039
040/**
041 * Ensure that only a granted user can see the illustration of asite.
042 */
043public class ProtectSiteParameterAction extends AbstractAction implements Serviceable
044{
045    private CurrentUserProvider _currentUserProvider;
046    private ProjectManager _projectManager;
047    private SiteManager _siteManager;
048    private ProjectMemberManager _projectMemberManager;
049    
050    private static final Set<String> _ALLOWED_PROJECT_PARAMETERS = Set.of("illustration");
051
052    public void service(ServiceManager manager) throws ServiceException
053    {
054        _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
055        _projectManager = (ProjectManager) manager.lookup(ProjectManager.ROLE);
056        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
057        _projectMemberManager = (ProjectMemberManager) manager.lookup(ProjectMemberManager.ROLE);
058    }
059    
060    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
061    {
062        // Is an authorized parameter?
063        String parameter = parameters.getParameter("parameter", null);
064        if (!_ALLOWED_PROJECT_PARAMETERS.contains(parameter))
065        {
066            return null;
067        }
068        
069        // Is the user logged in?
070        UserIdentity currentUser = _currentUserProvider.getUser();
071        if (currentUser == null)
072        {
073            return null;
074        }
075        
076        // Is the target site a project site?
077        Site projectSite = _siteManager.getSite(source);
078        if (!StringUtils.equals(projectSite.getType(), ProjectWorkspaceSiteType.TYPE_ID))
079        {
080            return null;
081        }
082        Project project = _projectManager.getProjectsForSite(projectSite).getFirst();
083        
084        // Is the target site public with catalog read rights OR has the user read access to the target site?
085        if (!_projectMemberManager.isProjectMember(project, currentUser)
086            && (project.getInscriptionStatus() == InscriptionStatus.PRIVATE
087                || !_projectManager.isUserInProjectPopulations(project, currentUser)))
088        {
089            return null;
090        }
091        
092        return EMPTY_MAP;
093    }
094}