001/*
002 *  Copyright 2022 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.cms.workflow;
017
018import java.util.List;
019import java.util.Map;
020
021import org.ametys.core.user.UserIdentity;
022import org.ametys.plugins.repository.AmetysObject;
023import org.ametys.plugins.repository.AmetysRepositoryException;
024import org.ametys.plugins.workflow.component.CheckRightsCondition;
025import org.ametys.plugins.workflow.repository.WorkflowAwareAmetysObject;
026import org.ametys.runtime.i18n.I18nizableText;
027
028import com.opensymphony.workflow.WorkflowException;
029
030/**
031 * Condition for checking rights of an user for the current action
032 * using the right ametys object context.<p>
033 * The following configuration can be used for checking rights:<br>
034 * <pre>
035 * &lt;condition type="avalon"&gt;
036 * &nbsp;&nbsp;&lt;arg name="role"&gt;org.ametys.cms.workflow.AmetysObjectCheckRightsCondition&lt;/arg&gt;
037 * &nbsp;&nbsp;&lt;arg name="right"&gt;Right_Edition&lt;/arg&gt;
038 * &lt;/condition&gt;
039 * </pre>
040 */
041public class AmetysObjectCheckRightsCondition extends CheckRightsCondition
042{
043    /** Constant for storing the ametys object into the transient variables map. */
044    public static final String AMETYS_OBJECT_KEY = AmetysObject.class.getName();
045    
046    @Override
047    protected Object _computeContext(Map transientVars, Map args, UserIdentity user, String right) throws WorkflowException
048    {
049        try
050        {
051            return getAmetysObject(transientVars);
052        }
053        catch (AmetysRepositoryException e)
054        {
055            throw new WorkflowException("Unable to retrieve ametys object name", e);
056        }
057    }
058    
059    /**
060     * Retrieve the ametys object associated with the workflow.
061     * @param transientVars the parameters from the call.
062     * @return the ametys object.
063     * @throws WorkflowException if the content is not found.
064     */
065    protected WorkflowAwareAmetysObject getAmetysObject(Map transientVars) throws WorkflowException
066    {
067        WorkflowAwareAmetysObject content = (WorkflowAwareAmetysObject) transientVars.get(AMETYS_OBJECT_KEY);
068        
069        if (content == null)
070        {
071            throw new WorkflowException("Unable to retrieve ametys object");
072        }
073        
074        // Found in transient variables map
075        return content;
076    }
077    
078    @Override
079    public I18nizableText getLabel()
080    {
081        return new I18nizableText("plugin.cms", "PLUGINS_CMS_AMETYS_OBJECT_CHECK_RIGHTS_CONDITION_LABEL");
082    }
083    
084    @Override
085    protected I18nizableText _getSingleConditionDescriptionKey(List<String> parameters)
086    {
087        return new I18nizableText("plugin.cms", "PLUGINS_CMS_AMETYS_OBJECT_CHECK_RIGHTS_CONDITION_DESCRIPTION", parameters);
088    }
089}