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