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 052 public boolean supports(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 (supports(parent)) 062 { 063 return Collections.singleton(parent); 064 } 065 else 066 { 067 return null; 068 } 069 } 070 071 @Override 072 protected boolean ignoreOnHasAnyPermission() 073 { 074 return true; 075 } 076 077 @Override 078 protected Set< ? extends Object> _convertWorkspaceToRootRightContexts(Set<Object> workspacesContexts) 079 { 080 if (workspacesContexts.contains("/cms")) 081 { 082 return Set.of(_queryDAO.getQueriesRootNode()); 083 } 084 return null; 085 } 086 087 @Override 088 protected I18nizableText getObjectLabelForExplanation(Object object) throws RightsException 089 { 090 if (object instanceof Query) 091 { 092 Map<String, I18nizableTextParameter> params = Map.of("title", getObjectLabel(object)); 093 return new I18nizableText("plugin.queries-directory", "PLUGINS_QUERIESDIRECTORY_QUERY_ACCESS_CONTROLLER_QUERY_CONTEXT_LABEL", params); 094 } 095 else if (object instanceof QueryContainer) 096 { 097 if (_queryDAO.getQueriesRootNode().equals(object)) 098 { 099 return new I18nizableText("plugin.queries-directory", "PLUGINS_QUERIESDIRECTORY_QUERY_ACCESS_CONTROLLER_ROOT_CONTAINER_CONTEXT_EXPLANATION_LABEL"); 100 } 101 else 102 { 103 Map<String, I18nizableTextParameter> params = Map.of("name", getObjectLabel(object)); 104 return new I18nizableText("plugin.queries-directory", "PLUGINS_QUERIESDIRECTORY_QUERY_ACCESS_CONTROLLER_QUERY_CONTAINER_CONTEXT_LABEL", params); 105 } 106 } 107 throw new RightsException("Unsupported object " + object.toString()); 108 } 109 110 public I18nizableText getObjectLabel(Object object) 111 { 112 if (object instanceof Query query) 113 { 114 return new I18nizableText(getQueryContainerParentPathLabel(query.getParent()) + query.getTitle()); 115 } 116 else if (object instanceof QueryContainer container) 117 { 118 if (_queryDAO.getQueriesRootNode().equals(object)) 119 { 120 return new I18nizableText("plugin.queries-directory", "PLUGINS_QUERIESDIRECTORY_QUERY_ACCESS_CONTROLLER_ROOT_CONTAINER_CONTEXT_LABEL"); 121 } 122 else 123 { 124 return new I18nizableText(getQueryContainerParentPathLabel(container.getParent()) + container.getName()); 125 } 126 } 127 throw new RightsException("Unsupported object " + object.toString()); 128 } 129 130 public I18nizableText getObjectCategory(Object object) 131 { 132 return QUERIES_DIRECTORY_CONTEXT_CATEGORY; 133 } 134 135 public int getObjectPriority(Object object) 136 { 137 if (_queryDAO.getQueriesRootNode().equals(object)) 138 { 139 return 10; 140 } 141 return super.getObjectPriority(object); 142 } 143 144 /** 145 * Get a label representing the path of the container. 146 * 147 * The root is not included in the path 148 * @param container the container 149 * @return the label 150 */ 151 public static String getQueryContainerParentPathLabel(QueryContainer container) 152 { 153 if (container.getParent() instanceof QueryContainer parent) 154 { 155 return getQueryContainerParentPathLabel(parent) + container.getName() + " > "; 156 } 157 else 158 { 159 return ""; 160 } 161 } 162}