pundit_policy.rb 534 B

1234567891011121314151617181920212223242526272829303132
  1. module PunditPolicy
  2. attr_reader :user, :custom_exception
  3. def initialize(user, context)
  4. @user = user
  5. user_required! if user_required?
  6. initialize_context(context)
  7. end
  8. def user_required?
  9. true
  10. end
  11. def user_required!
  12. return if user
  13. raise Exceptions::NotAuthorized, 'authentication failed'
  14. end
  15. private
  16. def not_authorized(details = nil)
  17. if details
  18. details = "Not authorized (#{details})!"
  19. end
  20. @custom_exception = Exceptions::NotAuthorized.new(details)
  21. false
  22. end
  23. end