Sprankelprachtig aan/afmeldsysteem

groups_controller.rb 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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
  11. before_action :require_membership!, except: has_no_group
  12. before_action :api_require_admin!, only: has_no_group
  13. skip_before_action :set_group, :require_membership!, :api_require_authentication!, if: 'request.authorization'
  14. # API key based filter (both authenticates and authorizes)
  15. before_action :api_auth_token, if: 'request.authorization'
  16. # GET /api/groups
  17. def index
  18. @groups = Group.all
  19. end
  20. # GET /api/groups/1
  21. def show; end
  22. # GET /api/groups/1/current_activities
  23. def current_activities
  24. @activities = @group.current_activities
  25. render 'api/activities/index'
  26. end
  27. # GET /api/groups/1/upcoming_activities
  28. def upcoming_activities
  29. @activities = @group.upcoming_activities
  30. render 'api/activities/index'
  31. end
  32. # GET /api/groups/1/previous_activities
  33. def previous_activities
  34. @activities = @group.previous_activities
  35. render 'api/activities/index'
  36. end
  37. private
  38. # Set group from the :id parameter.
  39. def set_group
  40. @group = Group.find(params[:id])
  41. end
  42. # Authenticate a request by a 'Authorization: Group xxx'-header.
  43. # Asserts that the client meant to pass a Group API key, and then sets the
  44. # @group variable from the key's associated group.
  45. def api_auth_token
  46. words = request.authorization.split(' ')
  47. head :unauthorized unless words[0].casecmp('group').zero?
  48. @group = Group.find_by api_token: words[1]
  49. head :unauthorized unless @group
  50. end
  51. end