From 59a5b1f2b2107179832c73e5b40dc15992d664ef Mon Sep 17 00:00:00 2001 From: pabois <pierreandre.boissinot@noesya.coop> Date: Thu, 5 May 2022 13:07:04 +0200 Subject: [PATCH] user avatar sso --- app/models/user.rb | 3 +- app/models/user/with_avatar.rb | 37 +++++++++++++++++++ app/models/user/with_omniauth.rb | 2 +- ...20220505105709_add_picture_url_to_users.rb | 5 +++ db/schema.rb | 3 +- 5 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 app/models/user/with_avatar.rb create mode 100644 db/migrate/20220505105709_add_picture_url_to_users.rb diff --git a/app/models/user.rb b/app/models/user.rb index 97c8b7767..0b7266b7e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -22,6 +22,7 @@ # last_sign_in_ip :string # locked_at :datetime # mobile_phone :string +# picture_url :string # remember_created_at :datetime # reset_password_sent_at :datetime # reset_password_token :string indexed @@ -53,7 +54,7 @@ # fk_rails_bd6f7212a9 (university_id => universities.id) # class User < ApplicationRecord - has_one_attached_deletable :picture # In this order, "resize avatar" callback will be fired after the others. + include WithAvatar include WithUniversity include WithAuthentication include WithOmniauth diff --git a/app/models/user/with_avatar.rb b/app/models/user/with_avatar.rb new file mode 100644 index 000000000..863b3d0a2 --- /dev/null +++ b/app/models/user/with_avatar.rb @@ -0,0 +1,37 @@ +module User::WithAvatar + extend ActiveSupport::Concern + + included do + has_one_attached_deletable :picture # Nota: user has a picture_url property for SSO mapping. If picture_url is set it will use the url to change the picture + + before_save :update_picture, if: :will_save_change_to_picture_url? + after_save :update_picture_url + + private + + def update_picture + if picture_url.blank? + do_purge_picture + else + do_update_picture + end + end + + def update_picture_url + if picture_url.present? && !picture.attached? + self.update_column(:picture_url, nil) + end + end + + def do_purge_picture + self.picture.purge if self.picture.attached? + end + + def do_update_picture + downloaded_image = open(picture_url) + content_type = downloaded_image.content_type + extension = content_type.split('/').last + self.picture.attach(io: downloaded_image, filename: "avatar.#{extension}") + end + end +end diff --git a/app/models/user/with_omniauth.rb b/app/models/user/with_omniauth.rb index 64a2e5c72..64ba7ddc5 100644 --- a/app/models/user/with_omniauth.rb +++ b/app/models/user/with_omniauth.rb @@ -34,7 +34,7 @@ module User::WithOmniauth def self.update_data_for_mapping_element(user, mapping_element, attributes) sso_key = mapping_element['sso_key'] - return user if attributes[sso_key].blank? # if not provided by sso, just return + return user if attributes[sso_key].nil? # if not provided by sso, just return internal_key = mapping_element['internal_key'] user = self.update_data_for_mapping_element_standard(user, mapping_element, self.get_provided_answer(attributes[sso_key])) user diff --git a/db/migrate/20220505105709_add_picture_url_to_users.rb b/db/migrate/20220505105709_add_picture_url_to_users.rb new file mode 100644 index 000000000..9570a3173 --- /dev/null +++ b/db/migrate/20220505105709_add_picture_url_to_users.rb @@ -0,0 +1,5 @@ +class AddPictureUrlToUsers < ActiveRecord::Migration[6.1] + def change + add_column :users, :picture_url, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index f5a8ea0a6..c6b36805a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_04_28_171541) do +ActiveRecord::Schema.define(version: 2022_05_05_105709) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" @@ -782,6 +782,7 @@ ActiveRecord::Schema.define(version: 2022_04_28_171541) do t.datetime "direct_otp_sent_at" t.datetime "totp_timestamp" t.string "session_token" + t.string "picture_url" t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true t.index ["email", "university_id"], name: "index_users_on_email_and_university_id", unique: true t.index ["encrypted_otp_secret_key"], name: "index_users_on_encrypted_otp_secret_key", unique: true -- GitLab