ticket_policy.rb 1.7 KB

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