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.Set;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023
024import org.ametys.cms.content.ContentHelper;
025import org.ametys.cms.repository.Content;
026import org.ametys.plugins.core.impl.right.AbstractHierarchicalAccessController;
027
028/**
029 * Content access controller for reference table
030 */
031public class ReferenceTableAccessController extends AbstractHierarchicalAccessController<Object>
032{
033    /** The context for simple contents */
034    public static final String CONTEXT = "/reference-tables";
035    /** The helper for contents */
036    protected ContentHelper _contentHelper;
037
038    @Override
039    public void service(ServiceManager manager) throws ServiceException
040    {
041        super.service(manager);
042        _contentHelper = (ContentHelper) manager.lookup(ContentHelper.ROLE);
043    }
044    
045    @Override
046    public boolean isSupported(Object object)
047    {
048        return object instanceof Content && _contentHelper.isReferenceTable((Content) object)
049                || CONTEXT.equals(object);
050    }
051    
052    @Override
053    protected Set<Object> _getParents(Object object)
054    {
055        if (object instanceof Content)
056        {
057            return Collections.singleton(CONTEXT);
058        }
059        else
060        {
061            return null;
062        }
063    }
064    
065    @Override
066    protected Set< ? extends Object> _convertWorkspaceToRootRightContexts(Set<Object> workspacesContexts)
067    {
068        if (workspacesContexts.contains("/cms"))
069        {
070            // no need to list the root of simple contents as it is the same as the ContentAccessController
071            return Collections.singleton(CONTEXT);
072        }
073        return null;
074    }
075}