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.HashSet;
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.AbstractHierarchicalAccessController;
032import org.ametys.plugins.repository.AmetysObject;
033import org.ametys.web.repository.page.Page;
034import org.ametys.web.repository.site.Site;
035import org.ametys.web.repository.site.SiteManager;
036import org.ametys.web.repository.sitemap.Sitemap;
037
038/**
039 * {@link AccessController} for a {@link Page}
040 */
041public class PageAccessController extends AbstractHierarchicalAccessController<AmetysObject> implements Contextualizable
042{
043    /** The avalon context */
044    protected Context _context;
045    /** The web site manager */
046    protected SiteManager _siteManager;
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    }
059    
060    @Override
061    public boolean isSupported(Object object)
062    {
063        return object instanceof Sitemap || object instanceof Page;
064    }
065
066    @Override
067    protected Set<AmetysObject> _getParents(AmetysObject object)
068    {
069        AmetysObject parent = object.getParent();
070        if (isSupported(parent))
071        {
072            return Collections.singleton(parent);
073        }
074        else
075        {
076            return null;
077        }
078    }
079    
080    @Override
081    protected Set< ? extends Object> _convertWorkspaceToRootRightContexts(Set<Object> workspacesContexts)
082    {
083        if (workspacesContexts.contains("/web"))
084        {
085            Request request = ContextHelper.getRequest(_context);
086            
087            String siteName = request.getParameter("siteName");
088            if (siteName == null)
089            {
090                siteName = (String) request.getAttribute("siteName");
091            }
092            if (siteName == null)
093            {
094                siteName = (String) request.getAttribute("site");
095            }
096            
097            if (siteName != null)
098            {
099                Site site = _siteManager.getSite(siteName);
100                if (site != null)
101                {
102                    Set<Sitemap> sitemaps = new HashSet<>();
103                    for (Sitemap sitemap : site.getSitemaps())
104                    {
105                        sitemaps.add(sitemap);
106                    }
107                    return sitemaps;
108                }
109            }
110            
111            getLogger().warn("Could not determine current site to obtain all permissions");
112        }
113        
114        return null;
115    }
116}