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.plugins.workspaces.project.rights;
017
018import java.util.Collections;
019import java.util.List;
020import java.util.Set;
021
022import org.apache.avalon.framework.context.Context;
023import org.apache.avalon.framework.context.ContextException;
024import org.apache.avalon.framework.context.Contextualizable;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.cocoon.components.ContextHelper;
028import org.apache.cocoon.environment.Request;
029
030import org.ametys.core.right.AccessController;
031import org.ametys.plugins.core.impl.right.AbstractProfileStorageBasedAccessController;
032import org.ametys.plugins.workspaces.project.ProjectManager;
033import org.ametys.plugins.workspaces.project.objects.Project;
034import org.ametys.web.repository.site.SiteManager;
035
036/**
037 * {@link AccessController} for a {@link Project}
038 */
039public class ProjectAccessController extends AbstractProfileStorageBasedAccessController implements Contextualizable
040{
041    /** The avalon context */
042    protected Context _context;
043    /** The web site manager */
044    protected SiteManager _siteManager;
045    /** The project manager */
046    protected ProjectManager _projectManager;
047
048    public void contextualize(Context context) throws ContextException
049    {
050        _context = context;
051    }
052    
053    @Override
054    public void service(ServiceManager manager) throws ServiceException
055    {
056        super.service(manager);
057        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
058        _projectManager = (ProjectManager) manager.lookup(ProjectManager.ROLE);
059    }
060    
061    @Override
062    public boolean isSupported(Object object)
063    {
064        return object instanceof Project;
065    }
066
067    @Override
068    protected Set< ? extends Object> _convertWorkspaceToRootRightContexts(Set<Object> workspacesContexts)
069    {
070        if (workspacesContexts.contains("/web"))
071        {
072            Request request = ContextHelper.getRequest(_context);
073            
074            String siteName = request.getParameter("siteName");
075            if (siteName == null)
076            {
077                siteName = (String) request.getAttribute("siteName");
078            }
079            if (siteName == null)
080            {
081                siteName = (String) request.getAttribute("site");
082            }
083            
084            if (siteName != null)
085            {
086                List<String> projects = _projectManager.getProjectsForSite(siteName);
087                if (!projects.isEmpty())
088                {
089                    return Collections.singleton(projects.get(0));
090                }
091            }
092            
093            getLogger().warn("Could not determine current project to obtain all permissions");
094        }
095        
096        return null;
097    }
098
099}