ticket_policy.rb 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class TicketPolicy < ApplicationPolicy
  3. def show?
  4. access?('read')
  5. end
  6. def create?
  7. return false if !ensure_group?
  8. access?('create')
  9. end
  10. def update?
  11. access?('change')
  12. end
  13. def destroy?
  14. return true if user.permissions?('admin')
  15. # This might look like a bug is actually just defining
  16. # what exception is being raised and shown to the user.
  17. return false if !access?('delete')
  18. not_authorized('admin permission required')
  19. end
  20. def full?
  21. access?('full')
  22. end
  23. def ensure_group?
  24. return true if record.group_id
  25. not_authorized Exceptions::UnprocessableEntity.new __("The required value 'group_id' is missing.")
  26. end
  27. def follow_up?
  28. # This method is used to check if a follow-up is possible (mostly based on the configuration).
  29. # Agents are always allowed to reopen tickets, configuration does not matter.
  30. return update? if Ticket::StateType.lookup(id: record.state.state_type_id).name != 'closed' # check if the ticket state is already closed
  31. return true if agent_update_access?
  32. # Check follow_up_possible configuration, based on the group.
  33. return true if follow_up_possible? && update?
  34. not_authorized Exceptions::UnprocessableEntity.new __('Cannot follow-up on a closed ticket. Please create a new ticket.')
  35. end
  36. def agent_read_access?
  37. agent_access?('read')
  38. end
  39. def agent_update_access?
  40. agent_access?('change')
  41. end
  42. def create_mentions?
  43. agent_read_access?
  44. end
  45. private
  46. def follow_up_possible?
  47. case record.group.follow_up_possible
  48. when 'yes'
  49. true
  50. when 'new_ticket_after_certain_time'
  51. record.reopen_after_certain_time?
  52. when 'new_ticket'
  53. false
  54. end
  55. end
  56. def access?(access)
  57. return true if agent_access?(access)
  58. customer_access?
  59. end
  60. def agent_access?(access)
  61. return false if !user.permissions?('ticket.agent')
  62. return true if owner?
  63. user.group_access?(record.group.id, access)
  64. end
  65. def owner?
  66. record.owner_id == user.id
  67. end
  68. def customer_access?
  69. return false if !user.permissions?('ticket.customer')
  70. return true if customer?
  71. shared_organization?
  72. end
  73. def customer?
  74. record.customer_id == user.id
  75. end
  76. def shared_organization?
  77. return false if record.organization_id.blank?
  78. return false if user.organization_id.blank?
  79. return false if !user.organization_id?(record.organization_id)
  80. record.organization.shared?
  81. end
  82. end