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.survey.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.AbstractHierarchicalAccessController;
031import org.ametys.plugins.repository.AmetysObject;
032import org.ametys.plugins.survey.repository.AbstractSurveyElement;
033import org.ametys.plugins.survey.repository.Survey;
034import org.ametys.web.WebHelper;
035import org.ametys.web.repository.site.Site;
036import org.ametys.web.repository.site.SiteManager;
037
038/**
039 * {@link AccessController} for a {@link Survey}
040 */
041public class SurveyAccessController 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 AbstractSurveyElement;
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 = WebHelper.getSiteName(request);
088            
089            if (siteName != null)
090            {
091                Site site = _siteManager.getSite(siteName);
092                if (site != null)
093                {
094                    if (site.getRootPlugins().hasChild("survey/ametys:surveys"))
095                    {
096                        AmetysObject surveyNode = site.getRootPlugins().getChild("survey/ametys:surveys");
097                        return Collections.singleton(surveyNode);
098                    }
099                    else
100                    {
101                        getLogger().debug("Site '" +  siteName + "' has no survey root.");
102                    }
103                }
104                else
105                {
106                    getLogger().warn("Site '" +  siteName + "' does not exist.");
107                }
108            }
109            else
110            {
111                getLogger().warn("Could not determine current site to obtain all permissions");
112            }
113        }
114        return null;
115    }
116}