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;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.commons.lang3.StringUtils;
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    @Override
056    public boolean isSupported(Object object)
057    {
058        // FIXME this controller should only support resource in under the resource root node
059        return object instanceof ExplorerNode || object instanceof Resource;
060    }
061
062    @Override
063    protected Set<AmetysObject> _getParents(AmetysObject object)
064    {
065        AmetysObject parent = object.getParent();
066        if (parent instanceof ExplorerNode)
067        {
068            return Collections.singleton(parent);
069        }
070        else
071        {
072            return null;
073        }
074    }
075    
076    @Override
077    protected boolean _isSupportedStoredContext(Object storedObject)
078    {
079        if (isSupported(storedObject))
080        {
081            String path = null;
082            if (storedObject instanceof ExplorerNode node)
083            {
084                path = node.getPath();
085            }
086            else if (storedObject instanceof Resource resource)
087            {
088                path = resource.getPath();
089            }
090            
091            if (path != null)
092            {
093                for (ExplorerNode root : _resourcesDAO.getResourcesRootNodes())
094                {
095                    if (StringUtils.contains(path, root.getPath()))
096                    {
097                        return true;
098                    }
099                }
100            }
101        }
102        return false;
103    }
104    
105    @Override
106    protected Set<? extends Object> _convertWorkspaceToRootRightContexts(Set<Object> workspacesContexts)
107    {
108        if (workspacesContexts.contains("/cms"))
109        {
110            Set<Object> resourcesRootContexts = new HashSet<>();
111            resourcesRootContexts.addAll(_resourcesDAO.getResourcesRootNodes());
112            return resourcesRootContexts;
113        }
114        return null;
115    }
116    
117    @Override
118    protected I18nizableText getObjectLabelForExplanation(Object object) throws RightsException
119    {
120        Map<String, I18nizableTextParameter> params = Map.of("name", getObjectLabel(object));
121        if (object instanceof Resource)
122        {
123            return new I18nizableText("plugin.explorer", "PLUGINS_EXPLORER_RESOURCE_ACCESS_CONTROLLER_RESOURCE_CONTEXT_LABEL", params);
124            
125        }
126        else if (object instanceof ExplorerNode)
127        {
128            return new I18nizableText("plugin.explorer", "PLUGINS_EXPLORER_RESOURCE_ACCESS_CONTROLLER_EXPLORER_NODE_CONTEXT_LABEL", params);
129        }
130        throw new RightsException("Unsupported object " + object.toString());
131    }
132    
133    public I18nizableText getObjectLabel(Object object) throws RightsException
134    {
135        if (object instanceof Resource resource)
136        {
137            return new I18nizableText("plugin.explorer", "PLUGINS_EXPLORER_RESOURCE_ACCESS_CONTROLLER_PATH",
138                    Map.of("parent", getObjectLabel(resource.getParent()),
139                           "child", new I18nizableText(resource.getName()))
140                    );
141        }
142        else if (object instanceof ExplorerNode node)
143        {
144            if (_resourcesDAO.getResourcesRootNodes().contains(node))
145            {
146                return _resourcesDAO.getRootNodeLabel(node);
147            }
148            else
149            {
150                return new I18nizableText("plugin.explorer", "PLUGINS_EXPLORER_RESOURCE_ACCESS_CONTROLLER_PATH",
151                        Map.of("parent", getObjectLabel(node.getParent()),
152                               "child", new I18nizableText(node.getName()))
153                        );
154            }
155        }
156        throw new RightsException("Unsupported object " + object.toString());
157    }
158    
159    public I18nizableText getObjectCategory(Object object)
160    {
161        return RESOURCE_CONTEXT_CATEGORY;
162    }
163}