Sprankelprachtig aan/afmeldsysteem

groups_controller.rb 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Provides API views to read information related to Groups.
  2. # This controller provides two methods to authenticate and authorize a request:
  3. # - By the Session used to authenticate logged-in users, and
  4. # - By passing a custom Authorization:-header of the form 'Group :api_key'.
  5. #
  6. # If the API key method is used, the :id parameter is ignored, but still required in the URL.
  7. class Api::GroupsController < ApiController
  8. has_no_group = [:index]
  9. # Session-based authentication / authorization filters
  10. before_action :set_group, except: has_no_group, unless: 'request.authorization'
  11. before_action :require_membership!, except: has_no_group, unless: 'request.authorization'
  12. before_action :api_require_admin!, only: has_no_group, unless: 'request.authorization'
  13. # API key based filter (both authenticates and authorizes)
  14. before_action :api_auth_token, if: 'request.authorization'
  15. # GET /api/groups
  16. def index
  17. @groups = Group.all
  18. end
  19. # GET /api/groups/1
  20. def show; end
  21. # GET /api/groups/1/current_activities
  22. def current_activities
  23. @activities = @group.current_activities
  24. render 'api/activities/index'
  25. end
  26. # GET /api/groups/1/upcoming_activities
  27. def upcoming_activities
  28. @activities = @group.upcoming_activities
  29. render 'api/activities/index'
  30. end
  31. # GET /api/groups/1/previous_activities
  32. def previous_activities
  33. @activities = @group.previous_activities
  34. render 'api/activities/index'
  35. end
  36. private
  37. # Set group from the :id parameter.
  38. def set_group
  39. @group = Group.find(params[:id])
  40. end
  41. # Authenticate a request by a 'Authorization: Group xxx'-header.
  42. # Asserts that the client meant to pass a Group API key, and then sets the
  43. # @group variable from the key's associated group.
  44. def api_auth_token
  45. words = request.authorization.split(' ')
  46. head :unauthorized unless words[0].casecmp('Group').zero?
  47. @group = Group.find_by api_token: words[1]
  48. head :unauthorized unless @group
  49. end
  50. end