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.HashSet;
020import java.util.Map;
021import java.util.Set;
022
023import javax.jcr.RepositoryException;
024
025import org.apache.avalon.framework.context.Context;
026import org.apache.avalon.framework.context.ContextException;
027import org.apache.avalon.framework.context.Contextualizable;
028import org.apache.avalon.framework.service.ServiceException;
029import org.apache.avalon.framework.service.ServiceManager;
030import org.apache.cocoon.components.ContextHelper;
031
032import org.ametys.core.right.AccessController;
033import org.ametys.core.right.RightsException;
034import org.ametys.plugins.core.impl.right.AbstractHierarchicalAccessController;
035import org.ametys.plugins.repository.AmetysObject;
036import org.ametys.plugins.repository.AmetysObjectIterable;
037import org.ametys.plugins.repository.AmetysRepositoryException;
038import org.ametys.plugins.survey.dao.SurveyDAO;
039import org.ametys.plugins.survey.repository.AbstractSurveyElement;
040import org.ametys.plugins.survey.repository.Survey;
041import org.ametys.plugins.survey.repository.SurveyPage;
042import org.ametys.plugins.survey.repository.SurveyQuestion;
043import org.ametys.runtime.i18n.I18nizableText;
044import org.ametys.runtime.i18n.I18nizableTextParameter;
045import org.ametys.web.WebHelper;
046import org.ametys.web.repository.site.Site;
047import org.ametys.web.repository.site.SiteManager;
048import org.ametys.web.repository.sitemap.Sitemap;
049
050/**
051 * {@link AccessController} for a {@link Survey}
052 */
053public class SurveyAccessController extends AbstractHierarchicalAccessController<AmetysObject> implements Contextualizable
054{
055    /** The site manager */
056    protected SiteManager _siteManager;
057    /** The survey DAO */
058    protected SurveyDAO _surveyDAO;
059    private Context _context;
060
061    public void contextualize(Context context) throws ContextException
062    {
063        _context = context;
064    }
065    
066    @Override
067    public void service(ServiceManager manager) throws ServiceException
068    {
069        super.service(manager);
070        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
071        _surveyDAO = (SurveyDAO) manager.lookup(SurveyDAO.ROLE);
072    }
073    
074    public boolean supports(Object object)
075    {
076        return object instanceof AbstractSurveyElement;
077    }
078    
079    @Override
080    protected Set<AmetysObject> _getParents(AmetysObject object)
081    {
082        AmetysObject parent = object.getParent();
083        if (supports(parent))
084        {
085            return Collections.singleton(parent);
086        }
087        else
088        {
089            return null;
090        }
091    }
092    
093    @Override
094    protected boolean ignoreOnHasAnyPermission()
095    {
096        return true;
097    }
098    
099    @Override
100    protected Set< ? extends Object> _convertWorkspaceToRootRightContexts(Set<Object> workspacesContexts)
101    {
102        String siteName = WebHelper.getSiteName(ContextHelper.getRequest(_context));
103        if (siteName != null)
104        {
105            Site site = _siteManager.getSite(siteName);
106            try (AmetysObjectIterable<Sitemap> sitemaps = site.getSitemaps())
107            {
108                Set<Object> roots = new HashSet<>();
109                for (Sitemap sitemap: sitemaps)
110                {
111                    roots.add(_surveyDAO.getSurveyRootNode(siteName, sitemap.getName()));
112                }
113                return roots;
114            }
115            catch (AmetysRepositoryException | RepositoryException e)
116            {
117                getLogger().error("Failed to convert workspace to root right contexts. The controller will be ignored", e);
118                return null;
119            }
120        }
121        return null;
122    }
123    
124    @Override
125    protected I18nizableText getObjectLabelForExplanation(Object object) throws RightsException
126    {
127        Map<String, I18nizableTextParameter> params = Map.of("title", getObjectLabel(object));
128        return new I18nizableText("plugin.survey", "PLUGINS_SURVEY_ACCESS_CONTROLLER_CONTEXT_LABEL", params);
129    }
130    
131    public I18nizableText getObjectLabel(Object object) throws RightsException
132    {
133        if (object instanceof AbstractSurveyElement element)
134        {
135            Survey survey = null;
136            if (element instanceof SurveyPage page)
137            {
138                survey = page.getSurvey();
139            }
140            else if (element instanceof Survey s)
141            {
142                survey = s;
143            }
144            else if (element instanceof SurveyQuestion q)
145            {
146                survey = q.getSurvey();
147            }
148            
149            if (survey != null)
150            {
151                return new I18nizableText(survey.getLabel());
152            }
153        }
154        throw new RightsException("Unsupported object " + object.toString());
155    }
156    
157    public I18nizableText getObjectCategory(Object object)
158    {
159        return new I18nizableText("plugin.survey", "PLUGINS_SURVEY_ACCESS_CONTROLLER_CONTEXT_CATEGORY");
160    }
161}