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.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.content.RootContentHelper;
026import org.ametys.cms.repository.Content;
027import org.ametys.core.right.AccessController;
028import org.ametys.plugins.core.impl.right.AbstractHierarchicalAccessController;
029import org.ametys.plugins.repository.AmetysObject;
030
031/**
032 * {@link AccessController} for a {@link Content}
033 */
034public class ContentAccessController extends AbstractHierarchicalAccessController<AmetysObject>
035{
036    /** The helper for root content */
037    protected RootContentHelper _rootContentHelper;
038    /** The helper for contents */
039    protected ContentHelper _contentHelper;
040
041    @Override
042    public void service(ServiceManager manager) throws ServiceException
043    {
044        super.service(manager);
045        _rootContentHelper = (RootContentHelper) manager.lookup(RootContentHelper.ROLE);
046        _contentHelper = (ContentHelper) manager.lookup(ContentHelper.ROLE);
047    }
048    
049    @Override
050    public boolean isSupported(Object object)
051    {
052        return object instanceof Content && _rootContentHelper.isChildOfRootContent((Content) object)
053               || object != null && object.equals(_rootContentHelper.getRootContent());
054    }
055    
056    @Override
057    protected Set<AmetysObject> _getParents(AmetysObject object)
058    {
059        if (object instanceof Content)
060        {
061            return Collections.singleton(object.getParent());
062        }
063        else
064        {
065            return null;
066        }
067    }
068    
069    @Override
070    protected Set< ? extends Object> _convertWorkspaceToRootRightContexts(Set<Object> workspacesContexts)
071    {
072        if (workspacesContexts.contains("/cms"))
073        {
074            return Collections.singleton(_rootContentHelper.getRootContent());
075        }
076        return null;
077    }
078}