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.Map;
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.cocoon.components.ContextHelper;
026import org.apache.commons.lang3.StringUtils;
027
028import org.ametys.core.right.AccessController;
029import org.ametys.core.right.RightsException;
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.plugins.survey.repository.SurveyPage;
035import org.ametys.plugins.survey.repository.SurveyQuestion;
036import org.ametys.runtime.i18n.I18nizableText;
037import org.ametys.runtime.i18n.I18nizableTextParameter;
038import org.ametys.web.WebHelper;
039import org.ametys.web.repository.SiteAwareAmetysObject;
040
041/**
042 * {@link AccessController} for a {@link Survey}
043 */
044public class SurveyAccessController extends AbstractHierarchicalAccessController<AmetysObject> implements Contextualizable
045{
046    private Context _context;
047
048    public void contextualize(Context context) throws ContextException
049    {
050        _context = context;
051    }
052    
053    @Override
054    public boolean isSupported(Object object)
055    {
056        return object instanceof AbstractSurveyElement;
057    }
058    
059    @Override
060    protected boolean _isSupportedStoredContext(Object storedObject)
061    {
062        if (isSupported(storedObject))
063        {
064            String siteName = WebHelper.getSiteName(ContextHelper.getRequest(_context));
065            return siteName == null
066                    || storedObject instanceof SiteAwareAmetysObject ao && StringUtils.equals(ao.getSiteName(), siteName);
067        }
068        return super._isSupportedStoredContext(storedObject);
069    }
070    
071    @Override
072    protected Set<AmetysObject> _getParents(AmetysObject object)
073    {
074        AmetysObject parent = object.getParent();
075        if (isSupported(parent))
076        {
077            return Collections.singleton(parent);
078        }
079        else
080        {
081            return null;
082        }
083    }
084    
085    @Override
086    protected Set< ? extends Object> _convertWorkspaceToRootRightContexts(Set<Object> workspacesContexts)
087    {
088        return null;
089    }
090    
091    @Override
092    protected I18nizableText getObjectLabelForExplanation(Object object) throws RightsException
093    {
094        Map<String, I18nizableTextParameter> params = Map.of("title", getObjectLabel(object));
095        return new I18nizableText("plugin.survey", "PLUGINS_SURVEY_ACCESS_CONTROLLER_CONTEXT_LABEL", params);
096    }
097    
098    public I18nizableText getObjectLabel(Object object) throws RightsException
099    {
100        if (object instanceof AbstractSurveyElement element)
101        {
102            Survey survey = null;
103            if (element instanceof SurveyPage page)
104            {
105                survey = page.getSurvey();
106            }
107            else if (element instanceof Survey s)
108            {
109                survey = s;
110            }
111            else if (element instanceof SurveyQuestion q)
112            {
113                survey = q.getSurvey();
114            }
115            
116            if (survey != null)
117            {
118                return new I18nizableText(survey.getLabel());
119            }
120        }
121        throw new RightsException("Unsupported object " + object.toString());
122    }
123    
124    public I18nizableText getObjectCategory(Object object)
125    {
126        return new I18nizableText("plugin.survey", "PLUGINS_SURVEY_ACCESS_CONTROLLER_CONTEXT_CATEGORY");
127    }
128}