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