pundit_policy.rb 822 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module PunditPolicy
  3. attr_reader :user, :custom_exception
  4. def initialize(user, context)
  5. @user = user
  6. user_required! if user_required?
  7. initialize_context(context)
  8. end
  9. def user_required?
  10. true
  11. end
  12. def user_required!
  13. return if user
  14. raise Exceptions::Forbidden, __('Authentication required')
  15. end
  16. private
  17. def not_authorized(details_or_exception)
  18. @custom_exception = case details_or_exception
  19. when Exception
  20. details_or_exception
  21. else
  22. message = "Not authorized (#{details_or_exception})!"
  23. Exceptions::Forbidden.new(message)
  24. end
  25. false
  26. end
  27. end