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.right.RightManager.RightResult; 028import org.ametys.core.user.CurrentUserProvider; 029import org.ametys.core.user.UserIdentity; 030import org.ametys.plugins.forms.repository.FormEntry; 031import org.ametys.plugins.workflow.EnhancedCondition; 032import org.ametys.plugins.workflow.support.WorkflowHelper.WorkflowVisibility; 033import org.ametys.runtime.i18n.I18nizableText; 034 035import com.opensymphony.module.propertyset.PropertySet; 036import com.opensymphony.workflow.WorkflowException; 037 038/** 039 * Worklow condition for checking if current user is the same user as the entry submmiter 040 */ 041public class IsSubmitterCondition extends AbstractLogEnabled implements EnhancedCondition, FormEntryCondition, Serviceable 042{ 043 /** The current user provider */ 044 protected CurrentUserProvider _currentUserProvider; 045 046 @Override 047 public void service(ServiceManager manager) throws ServiceException 048 { 049 _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE); 050 } 051 052 @Override 053 public boolean passesCondition(Map transientVars, Map args, PropertySet ps) throws WorkflowException 054 { 055 FormEntry formEntry = (FormEntry) transientVars.get(AmetysObjectCheckRightsCondition.AMETYS_OBJECT_KEY); 056 UserIdentity user = _currentUserProvider.getUser(); 057 return user != null && user.equals(formEntry.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 073 public Map<UserIdentity, RightResult> getWorkflowActorsPermissions(FormEntry entry, Map args) 074 { 075 return Map.of(entry.getUser(), RightResult.RIGHT_ALLOW); 076 } 077}