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 Set< ? extends Object> _convertWorkspaceToRootRightContexts(Set<Object> workspacesContexts)
072    {
073        return null;
074    }
075    
076    @Override
077    protected I18nizableText getObjectLabelForExplanation(Object object) throws RightsException
078    {
079        if (object instanceof Cart)
080        {
081            Map<String, I18nizableTextParameter> params = Map.of("title", getObjectLabel(object));
082            return new I18nizableText("plugin.cart", "PLUGINS_CART_ACCESS_CONTROLLER_CONTEXT_EXPLANATION_LABEL", params);
083        }
084        else if (object instanceof CartContainer)
085        {
086            return new I18nizableText("plugin.cart", "PLUGINS_CART_ACCESS_CONTROLLER_ROOT_CONTEXT_EXPLANATION_LABEL");
087        }
088        throw new RightsException("Unsupported object " + object.toString());
089    }
090    
091    public I18nizableText getObjectLabel(Object object)
092    {
093        if (object instanceof Cart cart)
094        {
095            return new I18nizableText(cart.getTitle());
096        }
097        else if (object instanceof CartContainer)
098        {
099            return new I18nizableText("plugin.cart", "PLUGINS_CART_ACCESS_CONTROLLER_ROOT_CONTEXT_LABEL");
100        }
101        throw new RightsException("Unsupported object " + object.toString());
102    }
103
104    public I18nizableText getObjectCategory(Object object)
105    {
106        return CART_CONTEXT_CATEGORY;
107    }
108    
109    public int getObjectPriority(Object object)
110    {
111        if (CartHelper.getCartsNode(_resolver).equals(object))
112        {
113            return 10;
114        }
115        return super.getObjectPriority(object);
116    }
117}