001/*
002 *  Copyright 2024 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.plugins.forms.workflow;
017
018import java.util.List;
019import java.util.Map;
020
021import org.apache.avalon.framework.logger.AbstractLogEnabled;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025
026import org.ametys.cms.workflow.AmetysObjectCheckRightsCondition;
027import org.ametys.core.user.CurrentUserProvider;
028import org.ametys.core.user.UserIdentity;
029import org.ametys.plugins.forms.repository.FormEntry;
030import org.ametys.plugins.workflow.EnhancedCondition;
031import org.ametys.plugins.workflow.support.WorkflowHelper.WorkflowVisibility;
032import org.ametys.runtime.i18n.I18nizableText;
033
034import com.opensymphony.module.propertyset.PropertySet;
035import com.opensymphony.workflow.WorkflowException;
036
037/**
038 * Worklow condition for checking if current user is the same user as the entry submmiter
039 */
040public class IsSubmitterCondition extends AbstractLogEnabled implements EnhancedCondition, Serviceable
041{
042    /** The current user provider */
043    protected CurrentUserProvider _currentUserProvider;
044    
045    @Override
046    public void service(ServiceManager manager) throws ServiceException
047    {
048        _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
049    }
050
051    @Override
052    public boolean passesCondition(Map transientVars, Map args, PropertySet ps) throws WorkflowException
053    {
054        FormEntry formEntry = (FormEntry) transientVars.get(AmetysObjectCheckRightsCondition.AMETYS_OBJECT_KEY);
055        UserIdentity user = formEntry.getUser();
056        
057        return user != null && user.equals(_currentUserProvider.getUser());
058    }
059    
060    public I18nizableText getLabel()
061    {
062        return new I18nizableText("plugin.forms", "PLUGINS_FORMS_IS_SUBMITTER_CONDITION_LABEL");
063    }
064    
065    @Override
066    public List<WorkflowVisibility> getVisibilities()
067    {
068        List<WorkflowVisibility> visibilities = EnhancedCondition.super.getVisibilities();
069        visibilities.add(WorkflowVisibility.USER);
070        return visibilities;
071    }
072}