user.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class CoreWorkflow::Attributes::User < CoreWorkflow::Attributes::Base
  3. def values
  4. if @attribute[:name] == 'owner_id' && @attributes.payload['class_name'] == 'Ticket'
  5. return ticket_owner_id_bulk if @attributes.payload['screen'] == 'overview_bulk'
  6. return ticket_owner_id
  7. end
  8. []
  9. end
  10. def group_agent_user_ids(group_id)
  11. @group_agent_user_ids ||= {}
  12. @group_agent_user_ids[group_id] ||= User.joins(', groups_users').where("users.id = groups_users.user_id AND groups_users.access = 'full' AND groups_users.group_id = ? AND users.id IN (?)", group_id, agent_user_ids).pluck(:id)
  13. end
  14. def group_agent_roles_ids(group_id)
  15. @group_agent_roles_ids ||= {}
  16. @group_agent_roles_ids[group_id] ||= Role.joins(', roles_groups').where("roles.id = roles_groups.role_id AND roles_groups.access = 'full' AND roles_groups.group_id = ? AND roles.id IN (?)", group_id, agent_role_ids).pluck(:id)
  17. end
  18. def agent_user_ids
  19. @agent_user_ids ||= User.joins(:roles).where(users: { active: true }).where('roles_users.role_id' => agent_role_ids).pluck(:id)
  20. end
  21. def agent_role_ids
  22. @agent_role_ids ||= Role.with_permissions('ticket.agent').pluck(:id)
  23. end
  24. def group_agent_role_user_ids(group_id)
  25. @group_agent_role_user_ids ||= {}
  26. @group_agent_role_user_ids[group_id] ||= User.joins(:roles).where(roles: { id: group_agent_roles_ids(group_id) }).pluck(:id)
  27. end
  28. def ticket_owner_id
  29. return [''] if @attributes.selected_only.group_id.blank?
  30. owner_ids = group_owner_ids
  31. return [''] if owner_ids.blank?
  32. owner_ids
  33. end
  34. def group_owner_ids
  35. user_ids = []
  36. # dont show system user in frontend but allow to reset it to 1 on update/create of the ticket
  37. if @attributes.payload['request_id'] == 'ChecksCoreWorkflow.validate_workflows'
  38. user_ids = [1]
  39. end
  40. User.where(id: group_owner_ids_user_ids, active: true).each do |user|
  41. user_ids << user.id
  42. assets(user)
  43. end
  44. user_ids
  45. end
  46. def group_owner_ids_user_ids
  47. group_agent_user_ids(@attributes.selected.group_id).concat(group_agent_role_user_ids(@attributes.selected.group_id)).uniq
  48. end
  49. def group_ids_bulk
  50. @group_ids_bulk ||= begin
  51. ticket_ids = String(@attributes.payload['params']['ticket_ids']).split(',').map(&:to_i)
  52. Ticket.distinct.where(id: ticket_ids).pluck(:group_id)
  53. end
  54. end
  55. def group_users_bulk
  56. @group_users_bulk ||= begin
  57. group_users_bulk_user_count.keys.select { |user| group_users_bulk_user_count[user] == group_ids_bulk.count }
  58. end
  59. end
  60. def group_users_bulk_user_count
  61. @group_users_bulk_user_count ||= begin
  62. user_count = {}
  63. group_ids_bulk.each do |group_id|
  64. User.where(id: group_agent_user_ids(group_id).concat(group_agent_role_user_ids(group_id)).uniq, active: true).each do |user|
  65. user_count[user] ||= 0
  66. user_count[user] += 1
  67. end
  68. end
  69. user_count
  70. end
  71. end
  72. def ticket_owner_id_bulk
  73. return group_owner_ids if @attributes.selected.group_id.present?
  74. return [''] if group_users_bulk.blank?
  75. group_users_bulk.each { |user| assets(user) }
  76. group_users_bulk.map(&:id)
  77. end
  78. def assets(user)
  79. return if @attributes.assets == false
  80. return if @attributes.assets[User.to_app_model] && @attributes.assets[User.to_app_model][user.id]
  81. @attributes.assets = user.assets(@attributes.assets)
  82. end
  83. end