From dafba8c0e17e25b39cd0886cffaf203dfb34c375 Mon Sep 17 00:00:00 2001 From: Peter Bouda Date: Mon, 7 Oct 2024 11:22:23 +0100 Subject: [PATCH] DEV: Modify User model for SCIM --- .../discourse_scim_plugin/scim_controller.rb | 11 ---- app/controllers/scim_v2/groups_controller.rb | 21 ++++++++ app/controllers/scim_v2/users_controller.rb | 21 ++++++++ config/initializers/scimitar.rb | 9 ++++ config/routes.rb | 27 +++++++--- plugin.rb | 51 +++++++++++++++++++ 6 files changed, 121 insertions(+), 19 deletions(-) delete mode 100644 app/controllers/discourse_scim_plugin/scim_controller.rb create mode 100644 app/controllers/scim_v2/groups_controller.rb create mode 100644 app/controllers/scim_v2/users_controller.rb create mode 100644 config/initializers/scimitar.rb diff --git a/app/controllers/discourse_scim_plugin/scim_controller.rb b/app/controllers/discourse_scim_plugin/scim_controller.rb deleted file mode 100644 index 9b40f52..0000000 --- a/app/controllers/discourse_scim_plugin/scim_controller.rb +++ /dev/null @@ -1,11 +0,0 @@ -# frozen_string_literal: true - -module ::DiscourseScimPlugin - class ScimController < ::ApplicationController - requires_plugin PLUGIN_NAME - - def index - render json: { hello: "world" } - end - end -end \ No newline at end of file diff --git a/app/controllers/scim_v2/groups_controller.rb b/app/controllers/scim_v2/groups_controller.rb new file mode 100644 index 0000000..229a42f --- /dev/null +++ b/app/controllers/scim_v2/groups_controller.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +require "scimitar" + +module Scim + class ScimV2::GroupsController < Scimitar::ActiveRecordBackedResourcesController + # requires_plugin PLUGIN_NAME + protect_from_forgery with: :null_session + + protected + + def storage_class + Group + end + + def storage_scope + Group.all # Or e.g. "User.where(is_deleted: false)" - whatever base scope you require + end + + end +end \ No newline at end of file diff --git a/app/controllers/scim_v2/users_controller.rb b/app/controllers/scim_v2/users_controller.rb new file mode 100644 index 0000000..883f8a6 --- /dev/null +++ b/app/controllers/scim_v2/users_controller.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +require "scimitar" + +module Scim + class ScimV2::UsersController < Scimitar::ActiveRecordBackedResourcesController + # requires_plugin PLUGIN_NAME + protect_from_forgery with: :null_session + + protected + + def storage_class + User + end + + def storage_scope + User.all # Or e.g. "User.where(is_deleted: false)" - whatever base scope you require + end + + end +end \ No newline at end of file diff --git a/config/initializers/scimitar.rb b/config/initializers/scimitar.rb new file mode 100644 index 0000000..080f4f5 --- /dev/null +++ b/config/initializers/scimitar.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +Rails.application.config.to_prepare do + Scimitar.engine_configuration = Scimitar::EngineConfiguration.new({ + token_authenticator: Proc.new do | token, options | + true + end + }) +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index ea3d31d..c4209e1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,18 +7,29 @@ require "scimitar" # end # Discourse::Application.routes.draw { mount ::DiscourseScimPlugin::Engine, at: "/scim" } - -namespace :scim_v2 do - DiscourseScimPlugin::Engine.routes.draw do +Discourse::Application.routes.draw { + namespace :scim_v2 do + # DiscourseScimPlugin::Engine.routes.draw do + # get 'Users', to: 'users#index' + # get 'Users/:id', to: 'users#show' + # post 'Users', to: 'users#create' + # put 'Users/:id', to: 'users#replace' + # patch 'Users/:id', to: 'users#update' + # delete 'Users/:id', to: 'users#destroy' + # end + + mount Scimitar::Engine, at: '/' + get 'Users', to: 'users#index' get 'Users/:id', to: 'users#show' post 'Users', to: 'users#create' put 'Users/:id', to: 'users#replace' patch 'Users/:id', to: 'users#update' delete 'Users/:id', to: 'users#destroy' + + get 'Groups', to: 'groups#index' + get 'Groups/:id', to: 'groups#show' + post 'Groups', to: 'groups#create' + patch 'Groups/:id', to: 'groups#update' end - - Discourse::Application.routes.draw { - mount Scimitar::Engine, at: '/scim' - } -end \ No newline at end of file +} diff --git a/plugin.rb b/plugin.rb index 9a33b35..ad37db9 100644 --- a/plugin.rb +++ b/plugin.rb @@ -22,4 +22,55 @@ module ::DiscourseScimPlugin PLUGIN_NAME = "scim" require_relative "lib/discourse_scim_plugin/engine" +end + +after_initialize do + class ::User + def self.scim_resource_type + Scimitar::Resources::User + end + + def self.scim_attributes_map + { + id: :id, + userName: :username, + displayName: :name, + name: { + formatted: :name + }, + emails: [ + { + match: "type", + with: "work", + using: { + value: :email, + primary: true + } + } + ], + active: :active + } + end + + def self.scim_timestamps_map + { + created: :created_at, + lastModified: :updated_at + } + end + + def self.scim_mutable_attributes + nil + end + + def self.scim_queryable_attributes + { + displayName: { column: :name }, + userName: { column: :username }, + emails: { column: :emails } + } + end + + include Scimitar::Resources::Mixin + end end \ No newline at end of file