001/* 002 * Copyright 2020 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.rights.convertor; 017 018import java.util.HashSet; 019import java.util.List; 020import java.util.Set; 021 022import org.apache.avalon.framework.service.ServiceException; 023import org.apache.avalon.framework.service.ServiceManager; 024import org.apache.avalon.framework.service.Serviceable; 025 026import org.ametys.core.right.RightContextConvertor; 027import org.ametys.plugins.workspaces.project.ProjectManager; 028import org.ametys.plugins.workspaces.project.objects.Project; 029import org.ametys.web.repository.page.Page; 030import org.ametys.web.repository.site.Site; 031 032/** 033 * Converts a page of a project to its module root 034 * The index page of a project will be additionally converted to the Project 035 */ 036public class PageToModuleRootRightContextConvertor implements RightContextConvertor, Serviceable 037{ 038 private ProjectManager _projectManager; 039 040 public void service(ServiceManager manager) throws ServiceException 041 { 042 _projectManager = (ProjectManager) manager.lookup(ProjectManager.ROLE); 043 } 044 045 private Project _getProject(Page page) 046 { 047 Site site = page.getSite(); 048 List<Project> projectsForSite = _projectManager.getProjectsForSite(site); 049 if (projectsForSite.isEmpty()) 050 { 051 return null; 052 } 053 else 054 { 055 return projectsForSite.get(0); 056 } 057 } 058 059 public Set<Object> convert(Object object) 060 { 061 if (object instanceof Page) 062 { 063 Page page = (Page) object; 064 065 Project project = _getProject(page); 066 if (project == null) 067 { 068 return Set.of(); // optimization to avoid to call _projectManager.pageToModuleRoot on a non-project site 069 } 070 071 Set<Object> results = new HashSet<>(); 072 if ("index".equals(page.getPathInSitemap())) 073 { 074 results.add(project); 075 } 076 077 results.addAll(_projectManager.pageToModuleRoot(page)); 078 079 return results; 080 } 081 else 082 { 083 return Set.of(); 084 } 085 } 086}