001/*
002 *  Copyright 2024 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.extraction.rights;
017
018import java.util.Map;
019
020import org.apache.commons.lang3.StringUtils;
021
022import org.ametys.core.right.RightsException;
023import org.ametys.plugins.core.impl.right.StringHierarchicalAccessController;
024import org.ametys.runtime.i18n.I18nizableText;
025import org.ametys.runtime.i18n.I18nizableTextParameter;
026
027/**
028 * AccessController handling access to extraction
029 */
030public class ExtractionAccessController extends StringHierarchicalAccessController
031{
032    /** the extraction context category */
033    public static final I18nizableText EXTRACTION_CONTEXT_CATEGORY = new I18nizableText("plugin.extraction", "RIBBON_TABS_TAB_EXTRACTION_LABEL");
034    /** The prefix for rights on the root of a collection */
035    public static final String ROOT_CONTEXT = "/extraction-dir";
036
037    @Override
038    protected I18nizableText getObjectLabelForExplanation(Object object)
039    {
040        if (object instanceof String str && StringUtils.startsWith(str, ExtractionAccessController.ROOT_CONTEXT))
041        {
042            if (StringUtils.equals(str, ExtractionAccessController.ROOT_CONTEXT))
043            {
044                return new I18nizableText("plugin.extraction", "PLUGINS_EXTRACTION_ACCESS_CONTROLLER_ROOT_EXPLANATION_LABEL");
045            }
046            else
047            {
048                Map<String, I18nizableTextParameter> params = Map.of("contextLabel", getObjectLabel(object));
049                String target = StringUtils.substringAfterLast(str, SEPARATOR);
050                if (StringUtils.endsWith(target, ".xml"))
051                {
052                    return new I18nizableText("plugin.extraction", "PLUGINS_EXTRACTION_ACCESS_CONTROLLER_FILE_EXPLANATION_LABEL", params);
053                }
054                else
055                {
056                    return new I18nizableText("plugin.extraction", "PLUGINS_EXTRACTION_ACCESS_CONTROLLER_FOLDER_EXPLANATION_LABEL", params);
057                }
058            }
059        }
060        return super.getObjectLabel(object);
061    }
062    
063    @Override
064    public I18nizableText getObjectLabel(Object object)
065    {
066        if (object instanceof String str && StringUtils.startsWith(str, ExtractionAccessController.ROOT_CONTEXT))
067        {
068            if (StringUtils.equals(str, ExtractionAccessController.ROOT_CONTEXT))
069            {
070                return new I18nizableText("plugin.extraction", "PLUGINS_EXTRACTION_ACCESS_CONTROLLER_ROOT_LABEL");
071            }
072            else
073            {
074                String target = StringUtils.substringAfter(str, ROOT_CONTEXT);
075                target = StringUtils.replace(target.substring(1), "/", " > ");
076                return new I18nizableText(target);
077            }
078        }
079        throw new RightsException("Unsupported object " + object.toString());
080    }
081    
082    @Override
083    public I18nizableText getObjectCategory(Object object)
084    {
085        return EXTRACTION_CONTEXT_CATEGORY;
086    }
087    
088    public int getObjectPriority(Object object)
089    {
090        if (object instanceof String str && StringUtils.equals(str, ExtractionAccessController.ROOT_CONTEXT))
091        {
092            return 10;
093        }
094        return super.getObjectPriority(object);
095    }
096}