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    @Override
053    public boolean isSupported(Object object)
054    {
055        return object instanceof Cart || object instanceof CartContainer;
056    }
057    
058    @Override
059    protected Set<AmetysObject> _getParents(AmetysObject object)
060    {
061        if (object instanceof Cart)
062        {
063            return Set.of(object.getParent());
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 Cart)
081        {
082            Map<String, I18nizableTextParameter> params = Map.of("title", getObjectLabel(object));
083            return new I18nizableText("plugin.cart", "PLUGINS_CART_ACCESS_CONTROLLER_CONTEXT_EXPLANATION_LABEL", params);
084        }
085        else if (object instanceof CartContainer)
086        {
087            return new I18nizableText("plugin.cart", "PLUGINS_CART_ACCESS_CONTROLLER_ROOT_CONTEXT_EXPLANATION_LABEL");
088        }
089        throw new RightsException("Unsupported object " + object.toString());
090    }
091    
092    public I18nizableText getObjectLabel(Object object)
093    {
094        if (object instanceof Cart cart)
095        {
096            return new I18nizableText(cart.getTitle());
097        }
098        else if (object instanceof CartContainer)
099        {
100            return new I18nizableText("plugin.cart", "PLUGINS_CART_ACCESS_CONTROLLER_ROOT_CONTEXT_LABEL");
101        }
102        throw new RightsException("Unsupported object " + object.toString());
103    }
104
105    public I18nizableText getObjectCategory(Object object)
106    {
107        return CART_CONTEXT_CATEGORY;
108    }
109    
110    public int getObjectPriority(Object object)
111    {
112        if (CartHelper.getCartsNode(_resolver).equals(object))
113        {
114            return 10;
115        }
116        return super.getObjectPriority(object);
117    }
118}