001/*
002 *  Copyright 2023 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 org.ametys.plugins.core.impl.right.WorkspaceAccessController;
019import org.ametys.runtime.i18n.I18nizableText;
020
021/**
022 * This implementation of {@link WorkspaceAccessController} will add the current site to the context depending on context format:
023 * - /site => /site/[CURRENT_SITE_NAME]
024 * - /site/${site}/foo => /site/[CURRENT_SITE_NAME]/foo
025 * - /site/foo => /site/foo
026 */
027public class SiteWorkspaceAccessController extends WebWorkspaceAccessController
028{
029    private static final I18nizableText SITE_GENERAL_CONTEXT = new I18nizableText("plugin.web", "PLUGINS_WEB_RIGHT_ASSIGNMENT_CONTEXT_SITE_LABEL");
030
031    @Override
032    protected Object _convertContext(Object initialContext)
033    {
034        String supportedContext = (String) initialContext;
035        
036        if (supportedContext.contains("${site}"))
037        {
038            supportedContext = supportedContext.replaceAll("\\$\\{site\\}", _getSiteName());
039        }
040        else if ("/site".equals(supportedContext))
041        {
042            supportedContext = supportedContext + "/" + _getSiteName();
043        }
044        
045        return supportedContext;
046    }
047    
048    @Override
049    public I18nizableText getObjectLabel(Object object)
050    {
051        return SITE_GENERAL_CONTEXT;
052    }
053}