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.core.impl.right;
017
018import java.util.HashSet;
019import java.util.Set;
020import java.util.stream.Collectors;
021
022import org.apache.avalon.framework.configuration.Configuration;
023import org.apache.avalon.framework.configuration.ConfigurationException;
024import org.apache.avalon.framework.context.Context;
025import org.apache.avalon.framework.context.ContextException;
026import org.apache.avalon.framework.context.Contextualizable;
027import org.apache.commons.collections.CollectionUtils;
028
029import org.ametys.core.right.AccessController;
030import org.ametys.runtime.workspace.WorkspaceManager;
031
032/**
033 * {@link AccessController} for a workspace context objects (strings starting with "/" + workspace name).
034 */
035public class WorkspaceAccessController extends StringHierarchicalAccessController implements Contextualizable
036{
037    /** The avalon context */
038    protected Context _context;
039    
040    public void contextualize(Context context) throws ContextException
041    {
042        _context = context;
043    }
044    
045    @Override
046    public void configure(Configuration configuration) throws ConfigurationException
047    {
048        // Do not "super.configure"
049    }
050    
051    @Override
052    protected Set<String> getRootPrefixes()
053    {
054        return WorkspaceManager.getInstance().getWorkspaceNames().stream().map(name -> "/" + name).collect(Collectors.toSet());
055    }
056    
057    @Override
058    protected Set<String> getSupportedPrefixes()
059    {
060        return getRootPrefixes();
061    }
062    
063    @SuppressWarnings("unchecked")
064    @Override
065    protected Set< ? extends Object> _convertWorkspaceToRootRightContexts(Set<Object> workspacesContexts)
066    {
067        Set<String> rootPrefixes = getRootPrefixes();
068        return workspacesContexts != null && rootPrefixes != null ? new HashSet<>(CollectionUtils.intersection(workspacesContexts, rootPrefixes))
069                                                                  : null;
070    }
071}