ticket_policy.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. # agent - access if requester is owner
  30. return true if record.owner_id == user.id
  31. # agent - access if requester is in group
  32. return true if user.group_access?(record.group.id, access)
  33. # check customer
  34. return false if !user.permissions?('ticket.customer')
  35. # access ok if its own ticket
  36. return true if record.customer_id == user.id
  37. organization_access?
  38. end
  39. def organization_access?
  40. return false if record.organization_id.blank?
  41. return false if user.organization_id.blank?
  42. return false if record.organization_id != user.organization_id
  43. record.organization.shared?
  44. end
  45. end