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.Collections;
019import java.util.Set;
020
021import org.apache.avalon.framework.configuration.Configuration;
022import org.apache.avalon.framework.configuration.ConfigurationException;
023import org.apache.cocoon.components.ContextHelper;
024import org.apache.cocoon.environment.Request;
025import org.apache.commons.lang3.StringUtils;
026
027import org.ametys.runtime.workspace.WorkspaceMatcher;
028
029/**
030 * Act as the WorksapceAccessController but also have a configured switching value when converting the variable ${WorkspaceName}. Such as 'repository' => 'admin'. 
031 */
032public class WorkspaceSwitchedAccessController extends WorkspaceAccessController
033{
034    /** The workspace to react on */
035    protected String _workspaceToMatch;
036    /** The workspace to replace with */
037    protected String _workspaceToReplaceWith;
038
039    @Override
040    public void configure(Configuration configuration) throws ConfigurationException
041    {
042        super.configure(configuration);
043        _workspaceToMatch = configuration.getChild("workspace-to-match").getValue();
044        _workspaceToReplaceWith = configuration.getChild("workspace-to-replace-with").getValue();
045    }
046    
047    @Override
048    protected Set<String> getRootPrefixes()
049    {
050        Request request = ContextHelper.getRequest(_context);
051        String workspaceName = (String) request.getAttribute(WorkspaceMatcher.WORKSPACE_NAME);
052        if (StringUtils.equals(_workspaceToMatch, workspaceName))
053        {
054            return Collections.singleton(SEPARATOR + _workspaceToReplaceWith);
055        }
056        else
057        {
058            return null;
059        }
060    }
061    
062    @Override
063    protected Set<String> getSupportedPrefixes()
064    {
065        return getRootPrefixes();
066    }
067}