authorizes.rb 839 B

12345678910111213141516171819202122232425262728293031323334
  1. module ApplicationController::Authorizes
  2. extend ActiveSupport::Concern
  3. include Pundit
  4. private
  5. def authorize!(record = policy_record, query = nil)
  6. authorize(record, query)
  7. end
  8. def authorized?(record = policy_record, query = nil)
  9. authorize!(record, query)
  10. true
  11. rescue Exceptions::Forbidden, Pundit::NotAuthorizedError
  12. false
  13. end
  14. def policy_record
  15. # check permissions in matching Pundit policy
  16. # Controllers namspace is used (See: https://github.com/varvet/pundit#policy-namespacing)
  17. # [:controllers, self] => Controllers::RolesControllerPolicy
  18. [:controllers, self]
  19. end
  20. def pundit_user
  21. @pundit_user ||= begin
  22. if current_user_on_behalf
  23. UserContext.new(current_user_on_behalf)
  24. else
  25. UserContext.new(current_user_real, @_token)
  26. end
  27. end
  28. end
  29. end