discourse-scim/lib/discourse_scim/user_mixin.rb

84 lines
1.8 KiB
Ruby
Raw Permalink Normal View History

# frozen_string_literal: true
require "scimitar"
module DiscourseScim::UserMixin
2024-10-31 09:08:33 +00:00
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
end
def scim_attributes_map
{
2024-10-31 09:08:33 +00:00
id: :id,
userName: :username,
displayName: :name,
emails: :scim_email_mapper,
groups: [
{
2024-10-31 09:08:33 +00:00
list: :groups,
using: {
value: :id,
display: :name
}
}
],
2024-10-31 09:08:33 +00:00
active: :active
}
end
def scim_timestamps_map
{
created: :created_at,
lastModified: :updated_at
}
end
def scim_mutable_attributes
nil
end
def scim_queryable_attributes
{
displayName: { column: :name },
userName: { column: :username },
emails: { column: :emails },
groups: { column: Group.arel_table[:id] },
"groups.value" => { column: Group.arel_table[:id] }
}
end
end
def self.included(base)
base.extend UserClassMethods
end
end