From 98b5f85f10a3af50a54fcd79e09fc9fd88f774fa Mon Sep 17 00:00:00 2001 From: Claire Date: Mon, 15 Jan 2024 19:04:58 +0100 Subject: [PATCH] Rename and refactor `User#confirm!` to `User#mark_email_as_confirmed!` (#28735) --- .../admin/confirmations_controller.rb | 2 +- app/models/concerns/user/omniauthable.rb | 2 +- app/models/user.rb | 54 +++++++++---------- lib/mastodon/cli/accounts.rb | 2 +- spec/features/oauth_spec.rb | 2 +- spec/models/user_spec.rb | 4 +- spec/policies/user_policy_spec.rb | 2 +- 7 files changed, 33 insertions(+), 35 deletions(-) diff --git a/app/controllers/admin/confirmations_controller.rb b/app/controllers/admin/confirmations_controller.rb index 6f4e4267972..7ccf5c9012d 100644 --- a/app/controllers/admin/confirmations_controller.rb +++ b/app/controllers/admin/confirmations_controller.rb @@ -7,7 +7,7 @@ module Admin def create authorize @user, :confirm? - @user.confirm! + @user.mark_email_as_confirmed! log_action :confirm, @user redirect_to admin_accounts_path end diff --git a/app/models/concerns/user/omniauthable.rb b/app/models/concerns/user/omniauthable.rb index 6d1d1b8cc3f..113bfda2304 100644 --- a/app/models/concerns/user/omniauthable.rb +++ b/app/models/concerns/user/omniauthable.rb @@ -61,7 +61,7 @@ module User::Omniauthable user.account.avatar_remote_url = nil end - user.confirm! if email_is_verified + user.mark_email_as_confirmed! if email_is_verified user.save! user end diff --git a/app/models/user.rb b/app/models/user.rb index 4bc19b16a6a..5c90af56d28 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -190,37 +190,16 @@ class User < ApplicationRecord end def confirm - new_user = !confirmed? - self.approved = true if grant_approval_on_confirmation? - - super - - if new_user - # Avoid extremely unlikely race condition when approving and confirming - # the user at the same time - reload unless approved? - - if approved? - prepare_new_user! - else - notify_staff_about_pending_account! - end + wrap_email_confirmation do + super end end - def confirm! - new_user = !confirmed? - self.approved = true if grant_approval_on_confirmation? - - skip_confirmation! - save! - - if new_user - # Avoid extremely unlikely race condition when approving and confirming - # the user at the same time - reload unless approved? - - prepare_new_user! if approved? + # Mark current email as confirmed, bypassing Devise + def mark_email_as_confirmed! + wrap_email_confirmation do + skip_confirmation! + save! end end @@ -435,6 +414,25 @@ class User < ApplicationRecord open_registrations? && !sign_up_from_ip_requires_approval? && !sign_up_email_requires_approval? end + def wrap_email_confirmation + new_user = !confirmed? + self.approved = true if grant_approval_on_confirmation? + + yield + + if new_user + # Avoid extremely unlikely race condition when approving and confirming + # the user at the same time + reload unless approved? + + if approved? + prepare_new_user! + else + notify_staff_about_pending_account! + end + end + end + def sign_up_from_ip_requires_approval? !sign_up_ip.nil? && IpBlock.where(severity: :sign_up_requires_approval).where('ip >>= ?', sign_up_ip.to_s).exists? end diff --git a/lib/mastodon/cli/accounts.rb b/lib/mastodon/cli/accounts.rb index 4146753035e..9dc65c1477d 100644 --- a/lib/mastodon/cli/accounts.rb +++ b/lib/mastodon/cli/accounts.rb @@ -105,7 +105,7 @@ module Mastodon::CLI if user.save if options[:confirmed] user.confirmed_at = nil - user.confirm! + user.mark_email_as_confirmed! end user.approve! if options[:approve] diff --git a/spec/features/oauth_spec.rb b/spec/features/oauth_spec.rb index 967956cc8ea..70356784d2c 100644 --- a/spec/features/oauth_spec.rb +++ b/spec/features/oauth_spec.rb @@ -49,7 +49,7 @@ describe 'Using OAuth from an external app' do let(:user) { Fabricate(:user, email: email, password: password) } before do - user.confirm! + user.mark_email_as_confirmed! user.approve! end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 7f68671df45..213022e8301 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -461,12 +461,12 @@ RSpec.describe User do end end - describe '#confirm!' do + describe '#mark_email_as_confirmed!' do subject(:user) { Fabricate(:user, confirmed_at: confirmed_at) } before do ActionMailer::Base.deliveries.clear - user.confirm! + user.mark_email_as_confirmed! end after { ActionMailer::Base.deliveries.clear } diff --git a/spec/policies/user_policy_spec.rb b/spec/policies/user_policy_spec.rb index fa476a9fc3d..7854547d26d 100644 --- a/spec/policies/user_policy_spec.rb +++ b/spec/policies/user_policy_spec.rb @@ -64,7 +64,7 @@ RSpec.describe UserPolicy do context 'when record.confirmed?' do it 'denies' do - john.user.confirm! + john.user.mark_email_as_confirmed! expect(subject).to_not permit(admin, john.user) end end