mirror of
https://github.com/discourse/discourse.git
synced 2025-06-04 20:04:42 +08:00
FIX: Add guard to prevent a primary UserEmail
from being reassigned.
This commit is contained in:
@ -12,6 +12,8 @@ class UserEmail < ActiveRecord::Base
|
||||
validates :email, email: true, format: { with: EmailValidator.email_regex },
|
||||
if: :validate_email?
|
||||
|
||||
validate :user_id_not_changed, if: :primary
|
||||
|
||||
validates :primary, uniqueness: { scope: [:user_id] }
|
||||
|
||||
private
|
||||
@ -27,6 +29,14 @@ class UserEmail < ActiveRecord::Base
|
||||
return false if self.skip_validate_email
|
||||
email_changed?
|
||||
end
|
||||
|
||||
def user_id_not_changed
|
||||
if self.will_save_change_to_user_id? && self.persisted?
|
||||
self.errors.add(:user_id, I18n.t(
|
||||
'active_record.errors.model.user_email.attributes.user_id.reassigning_primary_email')
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# == Schema Information
|
||||
|
Reference in New Issue
Block a user