Просмотр исходного кода

Fixes #4868 - Error when selecting Group in core workflow.

Co-authored-by: Dominik Klein <dk@zammad.com>
Rolf Schmidt 1 год назад
Родитель
Сommit
635438210c

+ 7 - 21
app/assets/javascripts/app/controllers/ticket_zoom/form_handler_core_workflow.coffee

@@ -14,33 +14,17 @@ class App.FormHandlerCoreWorkflow
   # we cache the article params per dispatch because they are expensive to get
   articleParamsCache = {}
 
-  # defines the objects and screen for which Core Workflow is active
-  coreWorkflowScreens = {
-    Ticket: ['create_middle', 'edit', 'overview_bulk']
-    User: ['create', 'edit', 'invite_agent']
-    Organization: ['create', 'edit']
-    Sla: ['create', 'edit']
-    CoreWorkflow: ['create', 'edit']
-    Group: ['create', 'edit']
-    Role: ['create', 'edit']
-  }
-
   # returns the objects for which Core Workflow is active
   @getObjects: ->
-    return Object.keys(coreWorkflowScreens)
+    return Object.keys(App.Config.get('core_workflow_config').execution)
 
   # returns the screens for which Core Workflow is active
   @getScreens: ->
-    result = []
-    for object, screens of coreWorkflowScreens
-      for screen in screens
-        continue if screen in result
-        result.push(screen)
-    return result
+    return _.uniq(_.flatten(Object.values(App.Config.get('core_workflow_config').execution)))
 
   # returns if the object and screen is controlled by core workflow
   @checkScreen: (checkObject, checkScreen) ->
-    for object, screens of coreWorkflowScreens
+    for object, screens of App.Config.get('core_workflow_config').execution
       return true if checkObject is object && _.contains(screens, checkScreen)
     return false
 
@@ -76,8 +60,10 @@ class App.FormHandlerCoreWorkflow
     return false if !ui.model
     return false if !ui.model.className
     return false if !ui.screen
-    return false if coreWorkflowScreens[ui.model.className] is undefined
-    return false if !_.contains(coreWorkflowScreens[ui.model.className], ui.screen)
+
+    config_class = App.Config.get('core_workflow_config').execution[ui.model.className]
+    return false if config_class is undefined
+    return false if !_.contains(config_class, ui.screen)
     return true
 
   # checks if the ajax or websocket endpoint should be used

+ 2 - 0
app/controllers/sessions_controller.rb

@@ -316,6 +316,8 @@ class SessionsController < ApplicationController
       config['session_id'] = session.id.public_id
     end
 
+    config['core_workflow_config'] = CoreWorkflow.config
+
     config
   end
 

+ 7 - 0
app/models/concerns/checks_core_workflow.rb

@@ -13,9 +13,16 @@ module ChecksCoreWorkflow
   private
 
   class_methods do
+
+    # defines the screens which core workflow executes
     def core_workflow_screens(*screens)
       @core_workflow_screens ||= screens
     end
+
+    # defines the screens which are configurable via admin interface
+    def core_workflow_admin_screens(*screens)
+      @core_workflow_admin_screens ||= screens
+    end
   end
 
   def validate_workflows

+ 18 - 0
app/models/core_workflow.rb

@@ -3,6 +3,9 @@
 class CoreWorkflow < ApplicationModel
   include ChecksClientNotification
   include CoreWorkflow::Assets
+  include ChecksCoreWorkflow
+
+  core_workflow_screens 'create', 'edit'
 
   default_scope { order(:priority, :id) }
   scope :active, -> { where(active: true) }
@@ -16,6 +19,21 @@ class CoreWorkflow < ApplicationModel
 
   validates :name, presence: true
 
+  def self.classes
+    Models.all.keys.select { |m| m.included_modules.include?(ChecksCoreWorkflow) }
+  end
+
+  def self.config
+    classes.each_with_object({ configuration: {}, execution: {} }) do |config_class, result|
+      if config_class.try(:core_workflow_screens).present?
+        result[:execution][config_class.to_s] = config_class.try(:core_workflow_screens)
+      end
+      if config_class.try(:core_workflow_admin_screens).present?
+        result[:configuration][config_class.to_s] = config_class.try(:core_workflow_admin_screens)
+      end
+    end
+  end
+
 =begin
 
 Runs the core workflow engine based on the current state of the object.

+ 12 - 2
app/models/core_workflow/custom/admin_core_workflow.rb

@@ -14,8 +14,16 @@ class CoreWorkflow::Custom::AdminCoreWorkflow < CoreWorkflow::Custom::Backend
     perform_screen_by_object
   end
 
+  def allowed_objects
+    @allowed_objects ||= CoreWorkflow.config[:configuration].keys.unshift('')
+  end
+
+  def allowed_screens
+    @allowed_screens ||= CoreWorkflow.config[:configuration].values.flatten.uniq
+  end
+
   def perform_object_defaults
-    result('set_fixed_to', 'object', ['', 'Ticket', 'Organization', 'User', 'Group'])
+    result('set_fixed_to', 'object', allowed_objects)
   end
 
   def perform_screen_by_object
@@ -30,7 +38,9 @@ class CoreWorkflow::Custom::AdminCoreWorkflow < CoreWorkflow::Custom::Backend
   def screens_by_object
     result = []
     ObjectManager::Object.new(selected.object).attributes(@condition_object.user).each do |field|
-      result += field[:screen].keys
+      next if field[:screen].blank?
+
+      result += field[:screen].keys.select { |s| allowed_screens.include?(s) }
     end
     result
   end

+ 1 - 0
app/models/group.rb

@@ -24,6 +24,7 @@ class Group < ApplicationModel
   include ChecksCoreWorkflow
 
   core_workflow_screens 'create', 'edit'
+  core_workflow_admin_screens 'create', 'edit'
 
   before_validation :ensure_name_last_and_parent, :check_max_depth
 

+ 1 - 0
app/models/organization.rb

@@ -32,6 +32,7 @@ class Organization < ApplicationModel
   include HasTransactionDispatcher
 
   core_workflow_screens 'create', 'edit'
+  core_workflow_admin_screens 'create', 'edit'
 
   validates :name,   presence: true
   validates :domain, presence: { message: 'required when Domain Based Assignment is enabled' }, if: :domain_assignment

+ 5 - 0
app/models/role.rb

@@ -26,6 +26,11 @@ class Role < ApplicationModel
   before_create  :check_default_at_signup_permissions
   before_update  :last_admin_check_by_attribute, :validate_agent_limit_by_attributes, :check_default_at_signup_permissions
 
+  # workflow checks should run after before_create and before_update callbacks
+  include ChecksCoreWorkflow
+
+  core_workflow_screens 'create', 'edit'
+
   # ignore Users because this will lead to huge
   # results for e.g. the Customer role
   association_attributes_ignored :users

+ 2 - 0
app/models/sla.rb

@@ -11,6 +11,8 @@ class Sla < ApplicationModel
   # workflow checks should run after before_create and before_update callbacks
   include ChecksCoreWorkflow
 
+  core_workflow_screens 'create', 'edit'
+
   validates  :name, presence: true
 
   validate   :cannot_have_response_and_update

+ 1 - 0
app/models/ticket.rb

@@ -48,6 +48,7 @@ class Ticket < ApplicationModel
   activity_stream_permission 'ticket.agent'
 
   core_workflow_screens 'create_middle', 'edit', 'overview_bulk'
+  core_workflow_admin_screens 'create_middle', 'edit'
 
   activity_stream_attributes_ignored :organization_id, # organization_id will change automatically on user update
                                      :create_article_type_id,

Некоторые файлы не были показаны из-за большого количества измененных файлов