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