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 /** The prefix for rights on the root of extractions results */ 037 public static final String ROOT_RESULTS_CONTEXT = "/extraction-results-dir"; 038 039 @Override 040 protected I18nizableText getObjectLabelForExplanation(Object object) 041 { 042 if (object instanceof String str && StringUtils.startsWith(str, ExtractionAccessController.ROOT_CONTEXT)) 043 { 044 if (StringUtils.equals(str, ExtractionAccessController.ROOT_CONTEXT)) 045 { 046 return new I18nizableText("plugin.extraction", "PLUGINS_EXTRACTION_ACCESS_CONTROLLER_ROOT_EXPLANATION_LABEL"); 047 } 048 else 049 { 050 Map<String, I18nizableTextParameter> params = Map.of("contextLabel", getObjectLabel(object)); 051 String target = StringUtils.substringAfterLast(str, SEPARATOR); 052 if (StringUtils.endsWith(target, ".xml")) 053 { 054 return new I18nizableText("plugin.extraction", "PLUGINS_EXTRACTION_ACCESS_CONTROLLER_FILE_EXPLANATION_LABEL", params); 055 } 056 else 057 { 058 return new I18nizableText("plugin.extraction", "PLUGINS_EXTRACTION_ACCESS_CONTROLLER_FOLDER_EXPLANATION_LABEL", params); 059 } 060 } 061 } 062 else if (object instanceof String str && StringUtils.startsWith(str, ExtractionAccessController.ROOT_RESULTS_CONTEXT)) 063 { 064 if (StringUtils.equals(str, ExtractionAccessController.ROOT_RESULTS_CONTEXT)) 065 { 066 return new I18nizableText("plugin.extraction", "PLUGINS_EXTRACTION_ACCESS_CONTROLLER_RESULT_ROOT_EXPLANATION_LABEL"); 067 } 068 else 069 { 070 Map<String, I18nizableTextParameter> params = Map.of("contextLabel", getObjectLabel(object)); 071 String target = StringUtils.substringAfterLast(str, SEPARATOR); 072 if (StringUtils.endsWith(target, ".xml") || StringUtils.endsWith(target, ".pdf")) 073 { 074 return new I18nizableText("plugin.extraction", "PLUGINS_EXTRACTION_ACCESS_CONTROLLER_RESULT_FILE_EXPLANATION_LABEL", params); 075 } 076 else 077 { 078 return new I18nizableText("plugin.extraction", "PLUGINS_EXTRACTION_ACCESS_CONTROLLER_RESULT_FOLDER_EXPLANATION_LABEL", params); 079 } 080 } 081 } 082 return super.getObjectLabel(object); 083 } 084 085 @Override 086 public I18nizableText getObjectLabel(Object object) 087 { 088 if (object instanceof String str && StringUtils.startsWith(str, ExtractionAccessController.ROOT_CONTEXT)) 089 { 090 if (StringUtils.equals(str, ExtractionAccessController.ROOT_CONTEXT)) 091 { 092 return new I18nizableText("plugin.extraction", "PLUGINS_EXTRACTION_ACCESS_CONTROLLER_ROOT_LABEL"); 093 } 094 else 095 { 096 String target = StringUtils.substringAfter(str, ROOT_CONTEXT); 097 target = StringUtils.replace(target.substring(1), "/", " > "); 098 return new I18nizableText(target); 099 } 100 } 101 else if (object instanceof String str && StringUtils.startsWith(str, ExtractionAccessController.ROOT_RESULTS_CONTEXT)) 102 { 103 if (StringUtils.equals(str, ExtractionAccessController.ROOT_RESULTS_CONTEXT)) 104 { 105 return new I18nizableText("plugin.extraction", "PLUGINS_EXTRACTION_ACCESS_CONTROLLER_RESULT_ROOT_LABEL"); 106 } 107 else 108 { 109 String target = StringUtils.substringAfter(str, ROOT_CONTEXT); 110 target = StringUtils.replace(target.substring(1), "/", " > "); 111 return new I18nizableText(target); 112 } 113 } 114 115 throw new RightsException("Unsupported object " + object.toString()); 116 } 117 118 @Override 119 public I18nizableText getObjectCategory(Object object) 120 { 121 return EXTRACTION_CONTEXT_CATEGORY; 122 } 123 124 public int getObjectPriority(Object object) 125 { 126 if (object instanceof String str && StringUtils.equals(str, ExtractionAccessController.ROOT_CONTEXT)) 127 { 128 return 10; 129 } 130 return super.getObjectPriority(object); 131 } 132}