checks_access.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class Ticket
  3. class Article
  4. module ChecksAccess
  5. extend ActiveSupport::Concern
  6. # Checks the given access of a given user for a ticket article.
  7. #
  8. # @param [User] The user that will be checked for given access.
  9. # @param [String] The access that should get checked.
  10. #
  11. # @example
  12. # article.access?(user, 'read')
  13. # #=> true
  14. #
  15. # @return [Boolean]
  16. def access?(user, access)
  17. if user.permissions?('ticket.customer')
  18. return false if internal == true
  19. end
  20. ticket = Ticket.lookup(id: ticket_id)
  21. ticket.access?(user, access)
  22. end
  23. # Checks the given access of a given user for a ticket article and fails with an exception.
  24. #
  25. # @param (see Ticket::Article#access?)
  26. #
  27. # @example
  28. # article.access!(user, 'read')
  29. #
  30. # @raise [NotAuthorized] Gets raised if given user doesn't have the given access.
  31. #
  32. # @return [nil]
  33. def access!(user, access)
  34. return if access?(user, access)
  35. raise Exceptions::NotAuthorized
  36. end
  37. end
  38. end
  39. end