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