checks_access.rb 1.2 KB

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