DEV: Avoid unique validation in UserPasswordExpirer.expire_user_password (#27343)

This commit updates the `UserPasswordExpirer.expire_user_password`
method to update `UserPassword#password_expired_at` when an existing
`UserPassword` record exists with the same `password_salt`,
`password_hash` and `password_algorithm`. This is to prevent the unique
validation error on `UserPassword#user_id` and
`UserPassword#password_hash` from being raised when the method is called
twice for a user that has not changed its password.
This commit is contained in:
Alan Guo Xiang Tan
2024-06-05 15:22:40 +08:00
committed by GitHub
parent 748240ce3b
commit 82383ea776
2 changed files with 32 additions and 7 deletions

View File

@@ -2,12 +2,14 @@
class UserPasswordExpirer
def self.expire_user_password(user)
UserPassword.create!(
user:,
password_hash: user.password_hash,
password_salt: user.salt,
password_algorithm: user.password_algorithm,
password_expired_at: Time.zone.now,
)
UserPassword
.where(
user:,
password_hash: user.password_hash,
password_salt: user.salt,
password_algorithm: user.password_algorithm,
)
.first_or_initialize
.update!(password_expired_at: Time.zone.now)
end
end