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.Map;
021import java.util.Set;
022import java.util.regex.Pattern;
023
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.commons.lang3.StringUtils;
027
028import org.ametys.core.right.AccessController;
029import org.ametys.core.right.RightsException;
030import org.ametys.plugins.core.impl.right.AbstractHierarchicalAccessController;
031import org.ametys.plugins.explorer.ExplorerNode;
032import org.ametys.plugins.explorer.resources.Resource;
033import org.ametys.plugins.explorer.resources.actions.ExplorerResourcesDAO;
034import org.ametys.plugins.repository.AmetysObject;
035import org.ametys.runtime.i18n.I18nizableText;
036import org.ametys.runtime.i18n.I18nizableTextParameter;
037
038/**
039 * {@link AccessController} for a {@link Resource}
040 */
041public class ResourceAccessController extends AbstractHierarchicalAccessController<AmetysObject>
042{
043    /** the resource context category */
044    public static final I18nizableText RESOURCE_CONTEXT_CATEGORY = new I18nizableText("plugin.explorer", "PLUGINS_EXPLORER_RIGHT_ASSIGNMENT_CONTEXT_RESOURCES_LABEL");
045    /** The explorer resources dao */
046    protected ExplorerResourcesDAO _resourcesDAO;
047
048    @Override
049    public void service(ServiceManager manager) throws ServiceException
050    {
051        super.service(manager);
052        
053        _resourcesDAO = (ExplorerResourcesDAO) manager.lookup(ExplorerResourcesDAO.ROLE);
054    }
055    
056    public boolean supports(Object object)
057    {
058        if (object instanceof ExplorerNode node)
059        {
060            return _isPartOfResourceExplorer(node.getPath());
061        }
062        else if (object instanceof Resource resource)
063        {
064            return _isPartOfResourceExplorer(resource.getPath());
065        }
066        return false;
067    }
068
069    private boolean _isPartOfResourceExplorer(String path)
070    {
071        for (Pattern pattern : _resourcesDAO.getExplorerNodePathPatterns())
072        {
073            if (pattern.matcher(path).matches())
074            {
075                return true;
076            }
077        }
078        return false;
079    }
080    
081    @Override
082    protected boolean _isSupportedStoredContext(Object storedObject)
083    {
084        // Do not rely on _resourcesDAO.getExplorerNodePathPatterns() but
085        // use the _resourcesDAO.getResourcesRootNodes() to restrict to
086        // resources available in the current context
087        if (storedObject instanceof ExplorerNode || storedObject instanceof Resource)
088        {
089            String path = storedObject instanceof ExplorerNode e ? e.getPath() : ((Resource) storedObject).getPath();
090            
091            for (ExplorerNode roots : _resourcesDAO.getResourcesRootNodes())
092            {
093                if (StringUtils.startsWith(path, roots.getPath()))
094                {
095                    return true;
096                }
097            }
098        }
099        
100        return false;
101    }
102
103    @Override
104    protected Set<AmetysObject> _getParents(AmetysObject object)
105    {
106        AmetysObject parent = object.getParent();
107        if (parent instanceof ExplorerNode)
108        {
109            return Collections.singleton(parent);
110        }
111        else
112        {
113            return null;
114        }
115    }
116    
117    @Override
118    protected Set<? extends Object> _convertWorkspaceToRootRightContexts(Set<Object> workspacesContexts)
119    {
120        if (workspacesContexts.contains("/cms"))
121        {
122            Set<Object> resourcesRootContexts = new HashSet<>();
123            resourcesRootContexts.addAll(_resourcesDAO.getResourcesRootNodes());
124            return resourcesRootContexts;
125        }
126        return null;
127    }
128    
129    @Override
130    protected I18nizableText getObjectLabelForExplanation(Object object) throws RightsException
131    {
132        Map<String, I18nizableTextParameter> params = Map.of("name", getObjectLabel(object));
133        if (object instanceof Resource)
134        {
135            return new I18nizableText("plugin.explorer", "PLUGINS_EXPLORER_RESOURCE_ACCESS_CONTROLLER_RESOURCE_CONTEXT_LABEL", params);
136            
137        }
138        else if (object instanceof ExplorerNode)
139        {
140            return new I18nizableText("plugin.explorer", "PLUGINS_EXPLORER_RESOURCE_ACCESS_CONTROLLER_EXPLORER_NODE_CONTEXT_LABEL", params);
141        }
142        throw new RightsException("Unsupported object " + object.toString());
143    }
144    
145    public I18nizableText getObjectLabel(Object object) throws RightsException
146    {
147        if (object instanceof Resource resource)
148        {
149            return new I18nizableText("plugin.explorer", "PLUGINS_EXPLORER_RESOURCE_ACCESS_CONTROLLER_PATH",
150                    Map.of("parent", getObjectLabel(resource.getParent()),
151                           "child", new I18nizableText(resource.getName()))
152                    );
153        }
154        else if (object instanceof ExplorerNode node)
155        {
156            if (_resourcesDAO.getResourcesRootNodes().contains(node))
157            {
158                return _resourcesDAO.getRootNodeLabel(node);
159            }
160            else
161            {
162                return new I18nizableText("plugin.explorer", "PLUGINS_EXPLORER_RESOURCE_ACCESS_CONTROLLER_PATH",
163                        Map.of("parent", getObjectLabel(node.getParent()),
164                               "child", new I18nizableText(node.getName()))
165                        );
166            }
167        }
168        throw new RightsException("Unsupported object " + object.toString());
169    }
170    
171    public I18nizableText getObjectCategory(Object object)
172    {
173        return RESOURCE_CONTEXT_CATEGORY;
174    }
175}