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.explorer.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;
024
025import org.ametys.core.right.AccessController;
026import org.ametys.plugins.core.impl.right.AbstractHierarchicalAccessController;
027import org.ametys.plugins.explorer.ExplorerNode;
028import org.ametys.plugins.explorer.resources.Resource;
029import org.ametys.plugins.explorer.resources.actions.ExplorerResourcesDAO;
030import org.ametys.plugins.repository.AmetysObject;
031
032/**
033 * {@link AccessController} for a {@link Resource}
034 */
035public class ResourceAccessController extends AbstractHierarchicalAccessController<AmetysObject>
036{
037    /** The explorer resources dao */
038    protected ExplorerResourcesDAO _resourcesDAO;
039
040    @Override
041    public void service(ServiceManager manager) throws ServiceException
042    {
043        super.service(manager);
044        
045        _resourcesDAO = (ExplorerResourcesDAO) manager.lookup(ExplorerResourcesDAO.ROLE);
046    }
047    
048    @Override
049    public boolean isSupported(Object object)
050    {
051        return object instanceof ExplorerNode || object instanceof Resource;
052    }
053
054    @Override
055    protected Set<AmetysObject> _getParents(AmetysObject object)
056    {
057        AmetysObject parent = object.getParent();
058        if (parent instanceof ExplorerNode)
059        {
060            return Collections.singleton(parent);
061        }
062        else
063        {
064            return null;
065        }
066    }
067    
068    @Override
069    protected Set<? extends Object> _convertWorkspaceToRootRightContexts(Set<Object> workspacesContexts)
070    {
071        if (workspacesContexts.contains("/cms"))
072        {
073            Set<Object> resourcesRootContexts = new HashSet<>();
074            resourcesRootContexts.addAll(_resourcesDAO.getResourcesRootNodes());
075            return resourcesRootContexts;
076        }
077        return null;
078    }
079}