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