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.core.right;
017
018import java.util.ArrayList;
019import java.util.List;
020import java.util.Map;
021import java.util.regex.Pattern;
022
023import org.apache.avalon.framework.configuration.Configuration;
024import org.apache.avalon.framework.configuration.ConfigurationException;
025import org.apache.avalon.framework.context.Context;
026import org.apache.avalon.framework.context.ContextException;
027import org.apache.avalon.framework.context.Contextualizable;
028import org.apache.commons.lang3.StringUtils;
029
030import org.ametys.core.ui.StaticClientSideElement;
031import org.ametys.runtime.workspace.WorkspaceMatcher;
032
033/**
034 * This static impl is a static client side element that take care that the current workspace matches a configured regexp 
035 */
036public abstract class AbstractStaticRightAssignmentContext extends StaticClientSideElement implements RightAssignmentContext, Contextualizable
037{
038    /** The private property */
039    protected boolean _isPrivate;
040    /** The avalon context */
041    protected Context _context;
042    /** The regexp that should match current workspace */
043    protected Pattern _workspaceMatcher;
044    /** Consider _workspaceMatcher negatively */
045    protected boolean _reverseWorkspaceMather;
046
047    public void contextualize(Context context) throws ContextException
048    {
049        _context = context;
050    }
051    
052    @Override
053    public void configure(Configuration configuration) throws ConfigurationException
054    {
055        super.configure(configuration);
056     
057        String workspace = configuration.getChild("workspace").getValue();
058        if (StringUtils.isNotBlank(workspace))
059        {
060            if (workspace.startsWith("!"))
061            {
062                _reverseWorkspaceMather = true;
063                workspace = workspace.substring(1);
064            }
065            _workspaceMatcher = Pattern.compile(workspace);
066        }
067        
068        _isPrivate = configuration.getChild("private").getValueAsBoolean(false);
069    }
070    
071    @Override
072    public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters)
073    {
074        if (!matchWorkspace(contextParameters))
075        {
076            return new ArrayList<>();
077        }
078        
079        return super.getScripts(ignoreRights, contextParameters);
080    }
081    
082    @Override
083    public boolean isPrivate()
084    {
085        return _isPrivate;
086    }
087    
088    /**
089     * Determines if the current workspace matches the configured workspace regexp
090     * @param contextParameters The contextual parameters 
091     * @return true if the current workspace matches
092     */
093    protected boolean matchWorkspace (Map<String, Object> contextParameters)
094    {
095        String currentWorkspace = (String) contextParameters.get(WorkspaceMatcher.WORKSPACE_NAME);
096        if (_workspaceMatcher != null)
097        {
098            boolean match = _workspaceMatcher.matcher(currentWorkspace).matches();
099            if (match && _reverseWorkspaceMather || !match && !_reverseWorkspaceMather)
100            {
101                return false;
102            }
103        }
104        
105        return true;
106    }
107}