001/*
002 *  Copyright 2017 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.cms.rights;
017
018import java.util.Collections;
019import java.util.Map;
020import java.util.Set;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024
025import org.ametys.cms.content.ContentHelper;
026import org.ametys.cms.repository.Content;
027import org.ametys.core.right.RightsException;
028import org.ametys.plugins.core.impl.right.AbstractHierarchicalAccessController;
029import org.ametys.runtime.i18n.I18nizableText;
030
031/**
032 * Content access controller for reference table
033 */
034public class ReferenceTableAccessController extends AbstractHierarchicalAccessController<Object>
035{
036    /** The reference table context category */
037    public static final I18nizableText REFERENCE_TABLE_CONTEXT_CATEGORY = new I18nizableText("plugin.cms", "PLUGINS_CMS_RIGHT_ASSIGNMENT_CONTEXT_REFERENCETABLES_LABEL");
038    /** The context for simple contents */
039    public static final String ROOT_CONTEXT = "/reference-tables";
040    /** The helper for contents */
041    protected ContentHelper _contentHelper;
042
043    @Override
044    public void service(ServiceManager manager) throws ServiceException
045    {
046        super.service(manager);
047        _contentHelper = (ContentHelper) manager.lookup(ContentHelper.ROLE);
048    }
049    
050    @Override
051    public boolean isSupported(Object object)
052    {
053        return object instanceof Content && _contentHelper.isReferenceTable((Content) object)
054                || ROOT_CONTEXT.equals(object);
055    }
056    
057    @Override
058    protected Set<Object> _getParents(Object object)
059    {
060        if (object instanceof Content)
061        {
062            return Collections.singleton(ROOT_CONTEXT);
063        }
064        else
065        {
066            return null;
067        }
068    }
069    
070    @Override
071    protected Set< ? extends Object> _convertWorkspaceToRootRightContexts(Set<Object> workspacesContexts)
072    {
073        if (workspacesContexts.contains("/cms"))
074        {
075            // no need to list the root of simple contents as it is the same as the ContentAccessController
076            return Collections.singleton(ROOT_CONTEXT);
077        }
078        return null;
079    }
080    
081    @Override
082    protected I18nizableText getObjectLabelForExplanation(Object object) throws RightsException
083    {
084        if (object.equals(ROOT_CONTEXT))
085        {
086            return new I18nizableText("plugin.cms", "PLUGINS_CMS_REFERENCETABLE_ACCESS_CONTROLLER_ROOT_CONTEXT_EXPLANATION_LABEL");
087        }
088        else if (object instanceof Content)
089        {
090            return new I18nizableText("plugin.cms", "PLUGINS_CMS_REFERENCETABLE_ACCESS_CONTROLLER_CONTENT_CONTEXT_EXPLANATION_LABEL",
091                    Map.of("title", getObjectLabel(object)));
092        }
093        throw new RightsException("Unsupported context " + object.toString());
094    }
095    
096    public I18nizableText getObjectLabel(Object object)
097    {
098        if (object.equals(ROOT_CONTEXT))
099        {
100            return new I18nizableText("plugin.cms", "PLUGINS_CMS_REFERENCETABLE_ACCESS_CONTROLLER_ROOT_CONTEXT_LABEL");
101        }
102        else if (object instanceof Content content)
103        {
104            return new I18nizableText(content.getTitle());
105        }
106        throw new RightsException("Unsupported context " + object.toString());
107    }
108    
109    public I18nizableText getObjectCategory(Object object)
110    {
111        return REFERENCE_TABLE_CONTEXT_CATEGORY;
112    }
113    
114    public int getObjectPriority(Object object)
115    {
116        if (object.equals(ROOT_CONTEXT))
117        {
118            return 10;
119        }
120        return super.getObjectPriority(object);
121    }
122}