Sprankelprachtig aan/afmeldsysteem

api_controller.rb 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. class ApiController < ActionController::Base
  2. include AuthenticationHelper
  3. before_action :api_require_authentication!, except: [:status]
  4. def status
  5. @message = "Ok"
  6. render 'api/ok'
  7. end
  8. protected
  9. def api_require_authentication!
  10. if !is_logged_in?
  11. head :unauthorized
  12. end
  13. end
  14. def api_require_admin!
  15. if !current_person.is_admin?
  16. @message = I18n.t('authentication.admin_required')
  17. render 'api/error', status: :forbidden
  18. end
  19. end
  20. # Authenticate a request by a 'Authorization: Group xxx'-header.
  21. # Asserts that the client meant to pass a Group API key, and then sets the
  22. # @group variable from the key's associated group.
  23. def api_auth_group_token
  24. words = request.authorization.split(' ')
  25. head :unauthorized unless words[0].casecmp('group').zero?
  26. @group = Group.find_by api_token: words[1]
  27. head :unauthorized unless @group
  28. end
  29. # Require user to be a member of group OR admin, requires @group set
  30. def require_membership!
  31. if !current_person.groups.include?(@group) && !current_person.is_admin?
  32. @message = I18n.t('authentication.membership_required')
  33. render 'api/error', status: :forbidden
  34. end
  35. end
  36. end