ticket_policy.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. class TicketPolicy < ApplicationPolicy
  2. def show?
  3. access?('read')
  4. end
  5. def create?
  6. ensure_group!
  7. access?('create')
  8. end
  9. def update?
  10. access?('change')
  11. end
  12. def destroy?
  13. return true if user.permissions?('admin')
  14. # This might look like a bug is actually just defining
  15. # what exception is being raised and shown to the user.
  16. return false if !access?('delete')
  17. not_authorized('admin permission required')
  18. end
  19. def full?
  20. access?('full')
  21. end
  22. def ensure_group!
  23. return if record.group_id
  24. raise Exceptions::UnprocessableEntity, "Group can't be blank"
  25. end
  26. def follow_up?
  27. return true if user.permissions?('ticket.agent') # agents can always reopen tickets, regardless of group configuration
  28. return true if record.group.follow_up_possible != 'new_ticket' # check if the setting for follow_up_possible is disabled
  29. return true if record.state.name != 'closed' # check if the ticket state is already closed
  30. raise Exceptions::UnprocessableEntity, 'Cannot follow-up on a closed ticket. Please create a new ticket.'
  31. end
  32. def agent_read_access?
  33. agent_access?('read')
  34. end
  35. private
  36. def access?(access)
  37. return true if agent_access?(access)
  38. customer_access?
  39. end
  40. def agent_access?(access)
  41. return false if !user.permissions?('ticket.agent')
  42. return true if owner?
  43. user.group_access?(record.group.id, access)
  44. end
  45. def owner?
  46. record.owner_id == user.id
  47. end
  48. def customer_access?
  49. return false if !user.permissions?('ticket.customer')
  50. return true if customer?
  51. shared_organization?
  52. end
  53. def customer?
  54. record.customer_id == user.id
  55. end
  56. def shared_organization?
  57. return false if record.organization_id.blank?
  58. return false if user.organization_id.blank?
  59. return false if record.organization_id != user.organization_id
  60. record.organization.shared?
  61. end
  62. end