Create SSO records, import avatars from URL

This commit is contained in:
Gerhard Schlager 2021-11-11 00:21:56 +01:00
parent 29b431cff2
commit dc821493f8
3 changed files with 37 additions and 17 deletions

View File

@ -304,8 +304,12 @@ class ImportScripts::Base
opts.delete(:id)
merge = opts.delete(:merge)
post_create_action = opts.delete(:post_create_action)
sso_record = opts.delete(:sso_record)
existing = find_existing_user(opts[:email], opts[:username])
existing = find_existing_user(opts[:email], sso_record&.fetch("external_id"))
return existing if existing
existing = User.where(username: opts[:username]).first
return existing if existing && (merge || existing.custom_fields["import_id"].to_s == import_id.to_s)
bio_raw = opts.delete(:bio_raw)
@ -391,14 +395,18 @@ class ImportScripts::Base
end
u.create_single_sign_on_record!(sso_record) if sso_record
post_create_action.try(:call, u) if u.persisted?
u # If there was an error creating the user, u.errors has the messages
end
def find_existing_user(email, username)
def find_existing_user(email, external_id)
# Force the use of the index on the 'user_emails' table
UserEmail.where("lower(email) = ?", email.downcase).first&.user || User.where(username: username).first
user = UserEmail.where("lower(email) = ?", email.downcase).first&.user
user ||= SingleSignOnRecord.where(external_id: external_id).first&.user if external_id.present?
user
end
def create_group_members(results, opts = {})

View File

@ -17,15 +17,15 @@ module ImportScripts
tmp.unlink rescue nil
end
def create_avatar(user, avatar_path)
def create_avatar(user, avatar_path, origin_url = nil)
tempfile = copy_to_tempfile(avatar_path)
filename = "avatar#{File.extname(avatar_path)}"
upload = UploadCreator.new(tempfile, filename, type: "avatar").create_for(user.id)
upload = UploadCreator.new(tempfile, filename, type: "avatar", origin: origin_url).create_for(user.id)
if upload.present? && upload.persisted?
user.create_user_avatar
user.user_avatar.update(custom_upload_id: upload.id)
user.update(uploaded_avatar_id: upload.id)
user.user_avatar.update!(custom_upload_id: upload.id)
user.update!(uploaded_avatar_id: upload.id)
else
STDERR.puts "Failed to upload avatar for user #{user.username}: #{avatar_path}"
STDERR.puts upload.errors.inspect if upload

View File

@ -35,19 +35,25 @@ class ImportScripts::Generic < ImportScripts::Base
next if all_records_exist?(:users, rows.map { |row| row["id"] })
create_users(rows, total: total_count, offset: offset) do |row|
if row["sso_record"].present?
sso_record = JSON.parse(row["sso_record"])
sso_record[:last_payload] = ""
end
{
id: row["id"],
username: row["username"],
created_at: to_datetime(row["created_at"]),
name: row["name"],
email: row["email"],
email: row["email"] || fake_email,
last_seen_at: to_datetime(row[:"last_seen_at"]) || to_datetime(row["created_at"]),
bio_raw: row["bio"],
location: row["location"],
admin: to_boolean(row["admin"]),
moderator: to_boolean(row["moderator"]),
sso_record: sso_record,
post_create_action: proc do |user|
create_avatar(user, row["avatar_path"])
create_avatar(user, row["avatar_path"], row["avatar_url"])
suspend_user(user, row["suspension"])
end
}
@ -55,14 +61,17 @@ class ImportScripts::Generic < ImportScripts::Base
end
end
def create_avatar(user, avatar_path)
return if avatar_path.blank?
avatar_path = File.join(AVATAR_DIRECTORY, avatar_path)
def create_avatar(user, avatar_path, avatar_url)
if avatar_path.present?
avatar_path = File.join(AVATAR_DIRECTORY, avatar_path)
if File.exist?(avatar_path)
@uploader.create_avatar(user, avatar_path)
else
STDERR.puts "Could not find avatar: #{avatar_path}"
if File.exist?(avatar_path)
@uploader.create_avatar(user, avatar_path)
else
STDERR.puts "Could not find avatar: #{avatar_path}"
end
elsif avatar_url.present?
UserAvatar.import_url_for_user(avatar_url, user, override_gravatar: true)
end
end
@ -162,10 +171,13 @@ class ImportScripts::Generic < ImportScripts::Base
SQL
create_categories(rows) do |row|
# TODO Add more columns
{
id: row["id"],
name: row["name"],
description: row["description"],
color: row["color"],
text_color: row["text_color"],
read_restricted: row["read_restricted"],
parent_category_id: category_id_from_imported_category_id(row["parent_category_id"]),
post_create_action: proc do |category|
create_permalink(row["old_relative_url"], category_id: category.id)