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.AbstractProfileStorageBasedAccessController;
031import org.ametys.plugins.repository.AmetysObject;
032import org.ametys.plugins.survey.repository.Survey;
033import org.ametys.web.WebHelper;
034import org.ametys.web.repository.site.Site;
035import org.ametys.web.repository.site.SiteManager;
036
037/**
038 * {@link AccessController} for a {@link Survey}
039 */
040public class SurveyAccessController extends AbstractProfileStorageBasedAccessController implements Contextualizable
041{
042    /** The avalon context */
043    protected Context _context;
044    /** The web site manager */
045    protected SiteManager _siteManager;
046
047    public void contextualize(Context context) throws ContextException
048    {
049        _context = context;
050    }
051    
052    @Override
053    public void service(ServiceManager manager) throws ServiceException
054    {
055        super.service(manager);
056        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
057    }
058    
059    @Override
060    public boolean isSupported(Object object)
061    {
062        return object instanceof Survey;
063    }
064    
065    @Override
066    protected Set< ? extends Object> _convertWorkspaceToRootRightContexts(Set<Object> workspacesContexts)
067    {
068        if (workspacesContexts.contains("/web"))
069        {
070            Request request = ContextHelper.getRequest(_context);
071            
072            String siteName = WebHelper.getSiteName(request);
073            
074            if (siteName != null)
075            {
076                Site site = _siteManager.getSite(siteName);
077                if (site != null && site.getRootPlugins().hasChild("survey/ametys:surveys"))
078                {
079                    AmetysObject surveyNode = site.getRootPlugins().getChild("survey/ametys:surveys");
080                    return Collections.singleton(surveyNode);
081                }
082            }
083            
084            getLogger().warn("Could not determine current site to obtain all permissions");
085        }
086        return null;
087    }
088}