v0.22 - Add support for users who have (re)set real passwords in Discourse via email and password is same

This commit is contained in:
David Sainty 2024-09-19 15:46:25 +10:00
parent 872b706e0f
commit ed83e8b3a0

View File

@ -4,7 +4,7 @@
# name: discourse-md5_authentication # name: discourse-md5_authentication
# about: A plugin to authenticate users with MD5 passwords from legacy systems # about: A plugin to authenticate users with MD5 passwords from legacy systems
# version: 0.21 # version: 0.22
# authors: saint@federated.computer # authors: saint@federated.computer
# url: https://gitea.federated.computer/saint/discourse-md5_authentication.git # url: https://gitea.federated.computer/saint/discourse-md5_authentication.git
@ -60,14 +60,29 @@ after_initialize do
Rails.logger.warn "MD5 -- 1.1. MD5 matches" Rails.logger.warn "MD5 -- 1.1. MD5 matches"
# Set the user's password to the provided one and update other attributes # Set the user's password to the provided one and update other attributes
user.password = password begin
user.active = true user.active = true
user.approved = true user.approved = true
user.approved_at = Time.now user.approved_at = Time.now
user.approved_by_id = 1 user.approved_by_id = 1
user.custom_fields['md5_password'] = nil # Clear the custom MD5 field user.custom_fields['md5_password'] = nil # Clear the custom MD5 field
user.save! user.password = password
Rails.logger.warn "MD5 -- DD -- user.present" user.save!
rescue ActiveRecord::RecordInvalid => e
if e.message.include?("Password is the same as your current password")
Rails.logger.warn "MD5 -- Skipping password update because the new password is the same as the current password."
user.active = true
user.approved = true
user.approved_at = Time.now
user.approved_by_id = 1
user.custom_fields['md5_password'] = nil # Clear the custom MD5 field
user.save!(validate: false) # Skip validations as password is unchanged
else
raise e
end
end
Rails.logger.warn "MD5 -- DD -- User attributes updated"
# Generate a new token and hash it # Generate a new token and hash it
token = SecureRandom.hex(20) token = SecureRandom.hex(20)