From e8c5c97cb0b5092800d8ca0b4d222c9bd30f1a82 Mon Sep 17 00:00:00 2001 From: Peter Bouda Date: Thu, 31 Oct 2024 09:08:33 +0000 Subject: [PATCH] DEV: Change mapping of emails --- lib/discourse_scim/user_mixin.rb | 49 ++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/lib/discourse_scim/user_mixin.rb b/lib/discourse_scim/user_mixin.rb index df2df4d..ddcba0f 100644 --- a/lib/discourse_scim/user_mixin.rb +++ b/lib/discourse_scim/user_mixin.rb @@ -3,6 +3,34 @@ require "scimitar" module DiscourseScim::UserMixin + def scim_email_mapper=(scim_emails) + user_emails = scim_emails.map do |scim_email| + UserEmail.build({ + email: scim_emails[0][:value], + primary: scim_emails[0].key?(:type) || scim_emails[0][:type] == "work" + }) + end + + primary_email = user_emails[0] + primary_emails = user_emails.select { |value| value.primary } + if primary_emails.length() > 0 + primary_email = primary_emails[0] + end + + self.user_emails = user_emails + self.primary_email = primary_email + end + + def scim_email_mapper + self.user_emails.map do |user_email| + scim_email = { value: user_email[:email] } + if user_email[:primary] + scim_email[:type] = "primary" + end + scim_email + end + end + module UserClassMethods def scim_resource_type Scimitar::Resources::User @@ -10,29 +38,20 @@ module DiscourseScim::UserMixin def scim_attributes_map { - id: :id, - userName: :username, - displayName: :name, - emails: [ - { - match: "type", - with: "work", - using: { - value: :email, - primary: true - } - } - ], + id: :id, + userName: :username, + displayName: :name, + emails: :scim_email_mapper, groups: [ { - list: :groups, + list: :groups, using: { value: :id, display: :name } } ], - active: :active + active: :active } end