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