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    public boolean supports(Object object)
054    {
055        return object instanceof AbstractSurveyElement;
056    }
057    
058    @Override
059    protected boolean _isSupportedStoredContext(Object storedObject)
060    {
061        if (supports(storedObject))
062        {
063            String siteName = WebHelper.getSiteName(ContextHelper.getRequest(_context));
064            return siteName == null
065                    || storedObject instanceof SiteAwareAmetysObject ao && StringUtils.equals(ao.getSiteName(), siteName);
066        }
067        return super._isSupportedStoredContext(storedObject);
068    }
069    
070    @Override
071    protected Set<AmetysObject> _getParents(AmetysObject object)
072    {
073        AmetysObject parent = object.getParent();
074        if (supports(parent))
075        {
076            return Collections.singleton(parent);
077        }
078        else
079        {
080            return null;
081        }
082    }
083    
084    @Override
085    protected Set< ? extends Object> _convertWorkspaceToRootRightContexts(Set<Object> workspacesContexts)
086    {
087        return null;
088    }
089    
090    @Override
091    protected I18nizableText getObjectLabelForExplanation(Object object) throws RightsException
092    {
093        Map<String, I18nizableTextParameter> params = Map.of("title", getObjectLabel(object));
094        return new I18nizableText("plugin.survey", "PLUGINS_SURVEY_ACCESS_CONTROLLER_CONTEXT_LABEL", params);
095    }
096    
097    public I18nizableText getObjectLabel(Object object) throws RightsException
098    {
099        if (object instanceof AbstractSurveyElement element)
100        {
101            Survey survey = null;
102            if (element instanceof SurveyPage page)
103            {
104                survey = page.getSurvey();
105            }
106            else if (element instanceof Survey s)
107            {
108                survey = s;
109            }
110            else if (element instanceof SurveyQuestion q)
111            {
112                survey = q.getSurvey();
113            }
114            
115            if (survey != null)
116            {
117                return new I18nizableText(survey.getLabel());
118            }
119        }
120        throw new RightsException("Unsupported object " + object.toString());
121    }
122    
123    public I18nizableText getObjectCategory(Object object)
124    {
125        return new I18nizableText("plugin.survey", "PLUGINS_SURVEY_ACCESS_CONTROLLER_CONTEXT_CATEGORY");
126    }
127}