001/* 002 * Copyright 2023 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; 025import org.apache.commons.lang3.ArrayUtils; 026import org.apache.commons.lang3.StringUtils; 027 028import org.ametys.cms.data.type.ModelItemTypeConstants; 029import org.ametys.cms.workflow.AmetysObjectCheckRightsCondition; 030import org.ametys.core.user.CurrentUserProvider; 031import org.ametys.core.user.UserIdentity; 032import org.ametys.core.user.UserManager; 033import org.ametys.plugins.forms.repository.FormEntry; 034import org.ametys.plugins.workflow.EnhancedCondition; 035import org.ametys.plugins.workflow.component.WorkflowArgument; 036import org.ametys.plugins.workflow.support.WorkflowElementDefinitionHelper; 037import org.ametys.plugins.workflow.support.WorkflowHelper.WorkflowVisibility; 038import org.ametys.runtime.i18n.I18nizableText; 039 040import com.opensymphony.module.propertyset.PropertySet; 041import com.opensymphony.workflow.WorkflowException; 042 043/** 044 * Worklow condition for checking if current user is the same user as an user defined in a question 045 */ 046public class IsUserCondition extends AbstractLogEnabled implements EnhancedCondition, Serviceable 047{ 048 /** The question name argument */ 049 public static final String QUESTION_NAME = "question-name"; 050 051 /** The user manager */ 052 protected UserManager _userManager; 053 054 /** The current user provider */ 055 protected CurrentUserProvider _currentUserProvider; 056 057 @Override 058 public void service(ServiceManager manager) throws ServiceException 059 { 060 _userManager = (UserManager) manager.lookup(UserManager.ROLE); 061 _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE); 062 } 063 064 @Override 065 public boolean passesCondition(Map transientVars, Map args, PropertySet ps) throws WorkflowException 066 { 067 FormEntry formEntry = (FormEntry) transientVars.get(AmetysObjectCheckRightsCondition.AMETYS_OBJECT_KEY); 068 String questionName = (String) args.get(QUESTION_NAME); 069 if (StringUtils.isBlank(questionName) || !formEntry.hasDefinition(questionName) || !formEntry.getType(questionName).getId().equals(ModelItemTypeConstants.USER_ELEMENT_TYPE_ID)) 070 { 071 getLogger().warn("Given argument '" + questionName + "' for condition is not an user form question"); 072 return false; 073 } 074 075 if (formEntry.isMultiple(questionName)) 076 { 077 UserIdentity[] userIds = formEntry.getValue(questionName); 078 return userIds != null && ArrayUtils.contains(userIds, _currentUserProvider.getUser()); 079 } 080 else 081 { 082 UserIdentity userId = formEntry.getValue(questionName); 083 return userId != null && userId.equals(_currentUserProvider.getUser()); 084 } 085 } 086 087 @Override 088 public List<WorkflowArgument> getArguments() 089 { 090 WorkflowArgument questionName = WorkflowElementDefinitionHelper.getElementDefinition( 091 QUESTION_NAME, 092 new I18nizableText("plugin.forms", "PLUGINS_FORMS_IS_USER_ARGUMENT_QUESTION_NAME_LABEL"), 093 new I18nizableText("plugin.forms", "PLUGINS_FORMS_IS_USER_ARGUMENT_QUESTION_NAME_DESCRIPTION"), 094 true, 095 false 096 ); 097 098 return List.of(questionName); 099 } 100 101 public I18nizableText getLabel() 102 { 103 return new I18nizableText("plugin.forms", "PLUGINS_FORMS_IS_USER_CONDITION_LABEL"); 104 } 105 106 @Override 107 public I18nizableText getFullLabel(Map<String, String> argumentsValues) 108 { 109 String questionName = argumentsValues.get(QUESTION_NAME); 110 return new I18nizableText("plugin.forms", "PLUGINS_FORMS_IS_USER_CONDITION_DESCRIPTION", List.of(questionName)); 111 } 112 113 @Override 114 public List<WorkflowVisibility> getVisibilities() 115 { 116 List<WorkflowVisibility> visibilities = EnhancedCondition.super.getVisibilities(); 117 visibilities.add(WorkflowVisibility.USER); 118 return visibilities; 119 } 120}