001/* 002 * Copyright 2021 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.queriesdirectory.accesscontroller; 017 018import java.util.Collections; 019import java.util.Map; 020import java.util.Set; 021 022import org.apache.avalon.framework.service.ServiceException; 023import org.apache.avalon.framework.service.ServiceManager; 024 025import org.ametys.core.right.AccessController; 026import org.ametys.core.right.RightsException; 027import org.ametys.plugins.core.impl.right.AbstractHierarchicalAccessController; 028import org.ametys.plugins.queriesdirectory.Query; 029import org.ametys.plugins.queriesdirectory.QueryContainer; 030import org.ametys.plugins.queriesdirectory.QueryDAO; 031import org.ametys.plugins.repository.AmetysObject; 032import org.ametys.runtime.i18n.I18nizableText; 033import org.ametys.runtime.i18n.I18nizableTextParameter; 034 035/** 036 * {@link AccessController} for a {@link Query} 037 */ 038public class QueryAccessController extends AbstractHierarchicalAccessController<AmetysObject> 039{ 040 /** the queries directory context category */ 041 public static final I18nizableText QUERIES_DIRECTORY_CONTEXT_CATEGORY = new I18nizableText("plugin.queries-directory", "PLUGINS_QUERIESDIRECTORY_RIGHTS_CATEGORY"); 042 /** The query DAO */ 043 protected QueryDAO _queryDAO; 044 045 @Override 046 public void service(ServiceManager manager) throws ServiceException 047 { 048 super.service(manager); 049 _queryDAO = (QueryDAO) manager.lookup(QueryDAO.ROLE); 050 } 051 @Override 052 public boolean isSupported(Object object) 053 { 054 return object instanceof Query || object instanceof QueryContainer; 055 } 056 057 @Override 058 protected Set<AmetysObject> _getParents(AmetysObject object) 059 { 060 AmetysObject parent = object.getParent(); 061 if (isSupported(parent)) 062 { 063 return Collections.singleton(parent); 064 } 065 else 066 { 067 return null; 068 } 069 } 070 071 @Override 072 protected Set< ? extends Object> _convertWorkspaceToRootRightContexts(Set<Object> workspacesContexts) 073 { 074 return null; 075 } 076 077 @Override 078 protected I18nizableText getObjectLabelForExplanation(Object object) throws RightsException 079 { 080 if (object instanceof Query) 081 { 082 Map<String, I18nizableTextParameter> params = Map.of("title", getObjectLabel(object)); 083 return new I18nizableText("plugin.queries-directory", "PLUGINS_QUERIESDIRECTORY_QUERY_ACCESS_CONTROLLER_QUERY_CONTEXT_LABEL", params); 084 } 085 else if (object instanceof QueryContainer) 086 { 087 if (_queryDAO.getQueriesRootNode().equals(object)) 088 { 089 return new I18nizableText("plugin.queries-directory", "PLUGINS_QUERIESDIRECTORY_QUERY_ACCESS_CONTROLLER_ROOT_CONTAINER_CONTEXT_EXPLANATION_LABEL"); 090 } 091 else 092 { 093 Map<String, I18nizableTextParameter> params = Map.of("name", getObjectLabel(object)); 094 return new I18nizableText("plugin.queries-directory", "PLUGINS_QUERIESDIRECTORY_QUERY_ACCESS_CONTROLLER_QUERY_CONTAINER_CONTEXT_LABEL", params); 095 } 096 } 097 throw new RightsException("Unsupported object " + object.toString()); 098 } 099 100 public I18nizableText getObjectLabel(Object object) 101 { 102 if (object instanceof Query query) 103 { 104 return new I18nizableText(getQueryContainerParentPathLabel(query.getParent()) + query.getTitle()); 105 } 106 else if (object instanceof QueryContainer container) 107 { 108 if (_queryDAO.getQueriesRootNode().equals(object)) 109 { 110 return new I18nizableText("plugin.queries-directory", "PLUGINS_QUERIESDIRECTORY_QUERY_ACCESS_CONTROLLER_ROOT_CONTAINER_CONTEXT_LABEL"); 111 } 112 else 113 { 114 return new I18nizableText(getQueryContainerParentPathLabel(container.getParent()) + container.getName()); 115 } 116 } 117 throw new RightsException("Unsupported object " + object.toString()); 118 } 119 120 public I18nizableText getObjectCategory(Object object) 121 { 122 return QUERIES_DIRECTORY_CONTEXT_CATEGORY; 123 } 124 125 public int getObjectPriority(Object object) 126 { 127 if (_queryDAO.getQueriesRootNode().equals(object)) 128 { 129 return 10; 130 } 131 return super.getObjectPriority(object); 132 } 133 134 /** 135 * Get a label representing the path of the container. 136 * 137 * The root is not included in the path 138 * @param container the container 139 * @return the label 140 */ 141 public static String getQueryContainerParentPathLabel(QueryContainer container) 142 { 143 if (container.getParent() instanceof QueryContainer parent) 144 { 145 return getQueryContainerParentPathLabel(parent) + container.getName() + " > "; 146 } 147 else 148 { 149 return ""; 150 } 151 } 152}