Sprankelprachtig aan/afmeldsysteem

groups_controller.rb 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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_group_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. reference = try_parse_datetime params[:reference]
  25. @activities = @group.current_activities reference
  26. render 'api/activities/index'
  27. end
  28. # GET /api/groups/1/upcoming_activities
  29. def upcoming_activities
  30. reference = try_parse_datetime params[:reference]
  31. @activities = @group.upcoming_activities reference
  32. render 'api/activities/index'
  33. end
  34. # GET /api/groups/1/previous_activities
  35. def previous_activities
  36. reference = try_parse_datetime params[:reference]
  37. @activities = @group.previous_activities reference
  38. render 'api/activities/index'
  39. end
  40. private
  41. # Set group from the :id parameter.
  42. def set_group
  43. @group = Group.find(params[:id])
  44. end
  45. # @return [DateTime] the parsed input.
  46. def try_parse_datetime(input = nil)
  47. return unless input
  48. begin
  49. DateTime.parse input
  50. rescue ArgumentError
  51. nil
  52. end
  53. end
  54. end