From 25af95e0e6f2ea13de1b694c5e289c987d84a12d Mon Sep 17 00:00:00 2001 From: Spencer Alan Date: Wed, 5 Dec 2018 15:08:22 -0500 Subject: [PATCH] add json scim response structure --- app/controllers/concerns/scim_rails/response.rb | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/app/controllers/concerns/scim_rails/response.rb b/app/controllers/concerns/scim_rails/response.rb index a9683eb..856ef95 100644 --- a/app/controllers/concerns/scim_rails/response.rb +++ b/app/controllers/concerns/scim_rails/response.rb @@ -1,7 +1,72 @@ module ScimRails module Response + CONTENT_TYPE = "application/scim+json, application/json".freeze + def json_response(object, status = :ok) - render(json: object, status: status) + render \ + json: object, + status: status + end + + def json_scim_response(object:, status: :ok, counts: nil) + case params[:action] + when "index" + render \ + json: list_response(object, counts), + status: status, + content_type: CONTENT_TYPE + when "show", "create", "update" + render \ + json: user_response(object), + status: status, + content_type: CONTENT_TYPE + end + end + + private + + def list_response(object, counts) + object = object + .order(:id) + .offset(counts.offset) + .limit(counts.limit) + { + "schemas": [ + "urn:ietf:params:scim:api:messages:2.0:ListResponse" + ], + "totalResults": counts.total, + "startIndex": counts.start_index, + "itemsPerPage": counts.limit, + "Resources": list_users(object) + } + end + + def list_users(users) + users.map do |user| + user_response(user) + end + end + + def user_response(user) + schema = ScimRails.config.user_schema + find_value(user, schema) + end + + def find_value(user, object) + case object + when Hash + object.each.with_object({}) do |(key, value), hash| + hash[key] = find_value(user, value) + end + when Array + object.map do |value| + find_value(user, value) + end + when Symbol + user.public_send(object) + else + object + end end end end -- libgit2 0.21.0