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.cart.accesscontroller; 017 018import java.util.Map; 019import java.util.Set; 020 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023 024import org.ametys.core.right.AccessController; 025import org.ametys.core.right.RightsException; 026import org.ametys.plugins.cart.Cart; 027import org.ametys.plugins.cart.CartContainer; 028import org.ametys.plugins.cart.CartHelper; 029import org.ametys.plugins.core.impl.right.AbstractHierarchicalAccessController; 030import org.ametys.plugins.repository.AmetysObject; 031import org.ametys.plugins.repository.AmetysObjectResolver; 032import org.ametys.runtime.i18n.I18nizableText; 033import org.ametys.runtime.i18n.I18nizableTextParameter; 034 035/** 036 * {@link AccessController} for a {@link Cart} 037 */ 038public class CartAccessController extends AbstractHierarchicalAccessController<AmetysObject> 039{ 040 /** cart context category */ 041 public static final I18nizableText CART_CONTEXT_CATEGORY = new I18nizableText("plugin.cart", "PLUGINS_CART_RIGHTS_CATEGORY"); 042 /** The ametys object resolver */ 043 protected AmetysObjectResolver _resolver; 044 045 @Override 046 public void service(ServiceManager manager) throws ServiceException 047 { 048 super.service(manager); 049 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 050 } 051 052 public boolean supports(Object object) 053 { 054 return object instanceof Cart || object instanceof CartContainer; 055 } 056 057 @Override 058 protected Set<AmetysObject> _getParents(AmetysObject object) 059 { 060 if (object instanceof Cart) 061 { 062 return Set.of(object.getParent()); 063 } 064 else 065 { 066 return null; 067 } 068 } 069 070 @Override 071 protected boolean ignoreOnHasAnyPermission() 072 { 073 return true; 074 } 075 076 @Override 077 protected Set< ? extends Object> _convertWorkspaceToRootRightContexts(Set<Object> workspacesContexts) 078 { 079 if (workspacesContexts.contains("/cms")) 080 { 081 return Set.of(CartHelper.getCartsNode(_resolver)); 082 } 083 return null; 084 } 085 086 @Override 087 protected I18nizableText getObjectLabelForExplanation(Object object) throws RightsException 088 { 089 if (object instanceof Cart) 090 { 091 Map<String, I18nizableTextParameter> params = Map.of("title", getObjectLabel(object)); 092 return new I18nizableText("plugin.cart", "PLUGINS_CART_ACCESS_CONTROLLER_CONTEXT_EXPLANATION_LABEL", params); 093 } 094 else if (object instanceof CartContainer) 095 { 096 return new I18nizableText("plugin.cart", "PLUGINS_CART_ACCESS_CONTROLLER_ROOT_CONTEXT_EXPLANATION_LABEL"); 097 } 098 throw new RightsException("Unsupported object " + object.toString()); 099 } 100 101 public I18nizableText getObjectLabel(Object object) 102 { 103 if (object instanceof Cart cart) 104 { 105 return new I18nizableText(cart.getTitle()); 106 } 107 else if (object instanceof CartContainer) 108 { 109 return new I18nizableText("plugin.cart", "PLUGINS_CART_ACCESS_CONTROLLER_ROOT_CONTEXT_LABEL"); 110 } 111 throw new RightsException("Unsupported object " + object.toString()); 112 } 113 114 public I18nizableText getObjectCategory(Object object) 115 { 116 return CART_CONTEXT_CATEGORY; 117 } 118 119 public int getObjectPriority(Object object) 120 { 121 if (CartHelper.getCartsNode(_resolver).equals(object)) 122 { 123 return 10; 124 } 125 return super.getObjectPriority(object); 126 } 127}