68 lines
2.3 KiB
Ruby
68 lines
2.3 KiB
Ruby
|
# Federated Computer, Inc.
|
||
|
# David Sainty <saint@federated.computer> 2024 A.D.
|
||
|
# Gossamer Threads to Discourse -- Reset Discourse Email
|
||
|
# v0.1 First addition for resetting email address for user
|
||
|
|
||
|
require File.expand_path("../../../../config/environment", __FILE__)
|
||
|
|
||
|
class GossamerResetEmail
|
||
|
def reset_email(discourse_username, new_email)
|
||
|
puts "Resetting email for Discourse username: '#{discourse_username}'"
|
||
|
|
||
|
user = User.find_by(username: discourse_username)
|
||
|
if user
|
||
|
puts "New email: #{new_email} ..."
|
||
|
|
||
|
# Delete Gossamer MD5 Password
|
||
|
# user.custom_fields.delete('md5_password') # This removes the field completely
|
||
|
# user.save!
|
||
|
# puts " STEP 1 COMPLETED: Gossamer MD5 Password custom field removed for username: #{discourse_username}"
|
||
|
|
||
|
# Set the Discourse password with the `password=` method to properly hash the password
|
||
|
# user.password = new_password
|
||
|
# user.save!
|
||
|
# puts " STEP 2 COMPLETED: Discourse Password updated for username: #{discourse_username}"
|
||
|
|
||
|
user.email = new_email
|
||
|
user.save!
|
||
|
puts " STEP 1 COMPLETED: Email address updated for username: #{discourse_username}"
|
||
|
|
||
|
# Update other attributes (to be sure to remove issues with login)
|
||
|
user.active = true
|
||
|
user.approved = true
|
||
|
user.approved_at = Time.now
|
||
|
user.approved_by_id = 1
|
||
|
user.save!
|
||
|
puts " STEP 2 COMPLETED: Other attributes (active, approved, approved_at, approved_by_id) updated for username: #{discourse_username}"
|
||
|
|
||
|
# Generate a new token, hash it, and create a confirmed email token for the user
|
||
|
token = SecureRandom.hex(20)
|
||
|
token_hash = EmailToken.hash_token(token)
|
||
|
|
||
|
EmailToken.create!(
|
||
|
user_id: user.id,
|
||
|
email: user.email,
|
||
|
token_hash: token_hash,
|
||
|
confirmed: true
|
||
|
)
|
||
|
puts " STEP 3 COMPLETED: New token generated, hashed and set as confirmed email token for username: #{discourse_username}"
|
||
|
|
||
|
else
|
||
|
puts "User not found: #{discourse_username}"
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
# Main execution
|
||
|
if ARGV.length != 2
|
||
|
puts "Usage: #{$0} discourse_username email_address@domain.com"
|
||
|
exit 1
|
||
|
end
|
||
|
|
||
|
discourse_username = ARGV[0]
|
||
|
new_email = ARGV[1]
|
||
|
# search_topic_by_title(search_string)
|
||
|
|
||
|
GossamerResetEmail.new.reset_email(discourse_username, new_email)
|
||
|
|