ticket_policy.rb 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. class TicketPolicy < ApplicationPolicy
  3. def show?
  4. access?('read')
  5. end
  6. def create?
  7. 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 if record.group_id
  25. raise Exceptions::UnprocessableEntity, __("Group can't be blank")
  26. end
  27. def follow_up?
  28. return true if user.permissions?('ticket.agent') # agents can always reopen tickets, regardless of group configuration
  29. return true if record.group.follow_up_possible != 'new_ticket' # check if the setting for follow_up_possible is disabled
  30. return true if record.state.name != 'closed' # check if the ticket state is already closed
  31. raise Exceptions::UnprocessableEntity, __('Cannot follow-up on a closed ticket. Please create a new ticket.')
  32. end
  33. def agent_read_access?
  34. agent_access?('read')
  35. end
  36. private
  37. def access?(access)
  38. return true if agent_access?(access)
  39. customer_access?
  40. end
  41. def agent_access?(access)
  42. return false if !user.permissions?('ticket.agent')
  43. return true if owner?
  44. user.group_access?(record.group.id, access)
  45. end
  46. def owner?
  47. record.owner_id == user.id
  48. end
  49. def customer_access?
  50. return false if !user.permissions?('ticket.customer')
  51. return true if customer?
  52. shared_organization?
  53. end
  54. def customer?
  55. record.customer_id == user.id
  56. end
  57. def shared_organization?
  58. return false if record.organization_id.blank?
  59. return false if user.organization_id.blank?
  60. return false if record.organization_id != user.organization_id
  61. record.organization.shared?
  62. end
  63. end