001/*
002 *  Copyright 2017 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.web.rights;
017
018import java.util.ArrayList;
019import java.util.Arrays;
020import java.util.List;
021import java.util.Map;
022import java.util.Set;
023import java.util.stream.Collectors;
024
025import org.apache.avalon.framework.context.Context;
026import org.apache.avalon.framework.context.ContextException;
027import org.apache.avalon.framework.context.Contextualizable;
028import org.apache.cocoon.components.ContextHelper;
029import org.apache.cocoon.environment.Request;
030import org.apache.commons.lang3.StringUtils;
031
032import org.ametys.core.right.RightsException;
033import org.ametys.plugins.core.impl.right.StringHierarchicalAccessController;
034import org.ametys.plugins.core.impl.right.WorkspaceAccessController;
035import org.ametys.runtime.i18n.I18nizableText;
036import org.ametys.web.WebHelper;
037
038/**
039 * This impl of {@link WorkspaceAccessController} will add the current sitemap to the cms workspace
040 */
041public class WebWorkspaceAccessController extends StringHierarchicalAccessController implements Contextualizable
042{
043    private Context _context;
044
045    public void contextualize(Context context) throws ContextException
046    {
047        _context = context;
048    }
049    
050    @Override
051    protected Set<String> getSupportedPrefixes()
052    {
053        String sitename = _getSiteName();
054        if (StringUtils.isBlank(sitename))
055        {
056            return null;
057        }
058
059        return _prefixes;
060    }
061    
062    @Override
063    protected Object _convertContext(Object initialContext)
064    {
065        String supportedContext = (String) initialContext;
066        List<String> contexts = new ArrayList<>(Arrays.asList(StringUtils.split(supportedContext, '/')));
067        contexts.add(1, _getSiteName());
068        return "/" + StringUtils.join(contexts, "/");
069    }
070    
071    @Override
072    protected boolean _isSupportedStoredContext(Object storedObject)
073    {
074        if (isSupported(storedObject))
075        {
076            String context = (String) storedObject;
077            List<String> contexts = new ArrayList<>(Arrays.asList(StringUtils.split(context, '/')));
078            if (contexts.size() >= 2 && StringUtils.equals(_getSiteName(), contexts.remove(1)))
079            {
080                return true;
081            }
082        }
083        return false;
084    }
085    
086    @Override
087    protected Object _unconvertContext(Object storedObject)
088    {
089        List<String> contexts = new ArrayList<>(Arrays.asList(StringUtils.split((String) storedObject, '/')));
090        contexts.remove(1);
091        return "/" + StringUtils.join(contexts, "/");
092    }
093    
094    @Override
095    protected Set<String> getRootPrefixes()
096    {
097        String sitename = "/" + _getSiteName();
098        return _prefixes.stream().map(p -> p + sitename).collect(Collectors.toSet());
099    }
100    
101    /**
102     * Get the current site name
103     * @return the current site name
104     */
105    protected String _getSiteName()
106    {
107        Request request = ContextHelper.getRequest(_context);
108        return WebHelper.getSiteName(request);
109    }
110    
111    @Override
112    protected I18nizableText getObjectLabelForExplanation(Object object) throws RightsException
113    {
114        return new I18nizableText("plugin.web", "PLUGINS_WEB_WORKSPACE_ACCESS_CONTROLLER_CONTEXT_LABEL", Map.of("object", getObjectLabel(object)));
115    }
116    
117    @Override
118    public I18nizableText getObjectLabel(Object object)
119    {
120        return WorkspaceAccessController.GENERAL_CONTEXT_CATEGORY;
121    }
122    
123    @Override
124    public I18nizableText getObjectCategory(Object object)
125    {
126        return WorkspaceAccessController.GENERAL_CONTEXT_CATEGORY;
127    }
128}