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