Sprankelprachtig aan/afmeldsysteem

groups_controller.rb 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. module Api
  8. class GroupsController < ApiController
  9. has_no_group = [:index]
  10. # Session-based authentication / authorization filters
  11. before_action :set_group, except: has_no_group
  12. before_action :require_membership!, except: has_no_group
  13. before_action :api_require_admin!, only: has_no_group
  14. skip_before_action :set_group, :require_membership!, :api_require_authentication!, if: 'request.authorization'
  15. # API key based filter (both authenticates and authorizes)
  16. before_action :api_auth_group_token, if: 'request.authorization'
  17. # GET /api/groups
  18. def index
  19. @groups = Group.all
  20. end
  21. # GET /api/groups/1
  22. def show; end
  23. # GET /api/groups/1/current_activities
  24. def current_activities
  25. reference = try_parse_datetime params[:reference]
  26. @activities = @group.current_activities reference
  27. render 'api/activities/index'
  28. end
  29. # GET /api/groups/1/upcoming_activities
  30. def upcoming_activities
  31. reference = try_parse_datetime params[:reference]
  32. @activities = @group.upcoming_activities reference
  33. render 'api/activities/index'
  34. end
  35. # GET /api/groups/1/previous_activities
  36. def previous_activities
  37. reference = try_parse_datetime params[:reference]
  38. @activities = @group.previous_activities reference
  39. render 'api/activities/index'
  40. end
  41. private
  42. # Set group from the :id parameter.
  43. def set_group
  44. @group = Group.find(params[:id])
  45. end
  46. # @return [DateTime] the parsed input.
  47. def try_parse_datetime(input = nil)
  48. return unless input
  49. begin
  50. DateTime.zone.parse input
  51. rescue ArgumentError
  52. nil
  53. end
  54. end
  55. end
  56. end