diff --git a/plugin.rb b/plugin.rb index e06af41..e3732f6 100644 --- a/plugin.rb +++ b/plugin.rb @@ -4,7 +4,7 @@ # name: discourse-md5_authentication # about: A plugin to authenticate users with MD5 passwords from legacy systems -# version: 0.21 +# version: 0.22 # authors: saint@federated.computer # 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" # Set the user's password to the provided one and update other attributes - user.password = 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! - Rails.logger.warn "MD5 -- DD -- user.present" + begin + 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.password = password + 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 token = SecureRandom.hex(20)