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.HashSet;
019import java.util.Map;
020import java.util.Set;
021
022import org.apache.commons.lang.StringUtils;
023
024import org.ametys.cms.content.RootContentHelper;
025import org.ametys.cms.contenttype.ContentType;
026import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
027import org.ametys.core.right.RightManager.RightResult;
028import org.ametys.core.user.UserIdentity;
029import org.ametys.plugins.workflow.component.CheckRightsCondition;
030
031import com.opensymphony.module.propertyset.PropertySet;
032import com.opensymphony.workflow.WorkflowException;
033
034/**
035 * Condition for checking if an user has the right to create a content of the requested type.
036 */
037public class CreateContentRightCondition extends CheckRightsCondition
038{
039    /** Content type extension point. */
040    protected ContentTypeExtensionPoint _contentTypeExtensionPoint;
041    /** Helper for root content */
042    protected RootContentHelper _rootContentHelper;
043
044    @Override
045    public void initialize() throws Exception
046    {
047        super.initialize();
048        _contentTypeExtensionPoint = (ContentTypeExtensionPoint) _manager.lookup(ContentTypeExtensionPoint.ROLE);
049        _rootContentHelper = (RootContentHelper) _manager.lookup(RootContentHelper.ROLE);
050    }
051    
052    @Override
053    public boolean passesCondition(Map transientVars, Map args, PropertySet ps) throws WorkflowException
054    {
055        Set<String> rights = getRights(transientVars);
056        args.put(__RIGHT_KEY, StringUtils.join(rights, '&'));
057        
058        return !rights.isEmpty() ? super.passesCondition(transientVars, args, ps) : true;
059    }
060    
061    @Override
062    protected boolean hasRight(UserIdentity user, String right, Object context)
063    {
064        return _rightManager.hasRight(user, right, "/cms") == RightResult.RIGHT_ALLOW || _rightManager.hasRight(user, right, _rootContentHelper.getRootContent()) == RightResult.RIGHT_ALLOW;
065    }
066    
067    /**
068     * Get the needed right for creation
069     * @param transientVars variables that will not be persisted.
070     * @return The required right
071     * @throws WorkflowException If an error occured
072     */
073    protected Set<String> getRights(Map transientVars) throws WorkflowException
074    {
075        Set<String> rights = new HashSet<>();
076        
077        String[] contentTypes = new String[0];
078        if (transientVars.get(CreateContentFunction.CONTENT_TYPES_KEY) != null)
079        {
080            contentTypes = (String[]) transientVars.get(CreateContentFunction.CONTENT_TYPES_KEY);
081        }
082        
083        for (String contentTypeId : contentTypes)
084        {
085            ContentType contentType = _contentTypeExtensionPoint.getExtension(contentTypeId);
086            
087            if (contentType == null)
088            {
089                throw new WorkflowException("Unknown content type: " + contentType);
090            }
091            
092            String right = contentType.getRight();
093            if (StringUtils.isNotEmpty(right))
094            {
095                rights.add(right);
096            }
097        }
098        
099        return rights;
100    }
101    
102    @Override
103    public void dispose()
104    {
105        _manager.release(_contentTypeExtensionPoint);
106        _contentTypeExtensionPoint = null;
107        super.dispose();
108    }
109}