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.cms.rights;
017
018import java.util.Collections;
019import java.util.HashSet;
020import java.util.Set;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.commons.lang3.StringUtils;
025
026import org.ametys.cms.contenttype.ContentType;
027import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
028import org.ametys.core.right.AccessController;
029import org.ametys.plugins.core.impl.right.AbstractHierarchicalAccessController;
030
031/**
032 * {@link AccessController} for a context objects (strings starting with "/content-types").
033 */
034public class ContentTypeAccessController extends AbstractHierarchicalAccessController<String>
035{
036    /** The content type extension point */
037    protected ContentTypeExtensionPoint _contentTypeExtensionPoint;
038
039    @Override
040    public void service(ServiceManager manager) throws ServiceException
041    {
042        super.service(manager);
043        
044        _contentTypeExtensionPoint = (ContentTypeExtensionPoint) manager.lookup(ContentTypeExtensionPoint.ROLE);
045    }
046    
047    @Override
048    public boolean isSupported(Object object)
049    {
050        return object instanceof String && (Content2ContentTypeRightContextConvertor.CONTENT_TYPE_RIGHT_CONTEXT_PREFIX.equals(object) || ((String) object).startsWith(Content2ContentTypeRightContextConvertor.CONTENT_TYPE_RIGHT_CONTEXT_PREFIX + '/'));
051    }
052
053    @Override
054    protected Set<String> _getParents(String context)
055    {
056        if (context == null || Content2ContentTypeRightContextConvertor.CONTENT_TYPE_RIGHT_CONTEXT_PREFIX.equals(context))
057        {
058            return null;
059        }
060        
061        String ctypeId = StringUtils.substringAfter(context, Content2ContentTypeRightContextConvertor.CONTENT_TYPE_RIGHT_CONTEXT_PREFIX + "/");
062        ContentType contentType = _contentTypeExtensionPoint.getExtension(ctypeId);
063        if (contentType == null)
064        {
065            getLogger().warn("Cannot get hierarchical rights of a content type that do not exists anymore '{}'. Silently goes to '{}'", ctypeId, Content2ContentTypeRightContextConvertor.CONTENT_TYPE_RIGHT_CONTEXT_PREFIX);
066            return null;
067        }
068        else
069        {
070            Set<String> parents = new HashSet<>();
071            String[] supertypeIds = contentType.getSupertypeIds();
072            for (int i = 0; i < supertypeIds.length; i++)
073            {
074                parents.add(Content2ContentTypeRightContextConvertor.CONTENT_TYPE_RIGHT_CONTEXT_PREFIX + "/" + supertypeIds[i]);
075            }
076            
077            if (parents.size() == 0 && !contentType.isReferenceTable()) // We do hierarchy for simple, but not until to the root...
078            {
079                parents.add(Content2ContentTypeRightContextConvertor.CONTENT_TYPE_RIGHT_CONTEXT_PREFIX);
080            }
081            
082            return parents;
083        }
084    }
085    
086    @Override
087    protected Set< ? extends Object> _convertWorkspaceToRootRightContexts(Set<Object> workspacesContexts)
088    {
089        if (workspacesContexts.contains("/cms"))
090        {
091            return Collections.singleton(Content2ContentTypeRightContextConvertor.CONTENT_TYPE_RIGHT_CONTEXT_PREFIX);
092        }
093        return null;
094    }
095}