2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
2014-09-08 19:03:42 -04:00
|
|
|
|
|
|
|
desc "invite an admin to this discourse instance"
|
|
|
|
task "admin:invite", [:email] => [:environment] do |_, args|
|
|
|
|
email = args[:email]
|
|
|
|
if !email || email !~ /@/
|
|
|
|
puts "ERROR: Expecting rake admin:invite[some@email.com]"
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
unless user = User.find_by_email(email)
|
|
|
|
puts "Creating new account!"
|
|
|
|
user = User.new(email: email)
|
|
|
|
user.password = SecureRandom.hex
|
|
|
|
user.username = UserNameSuggester.suggest(user.email)
|
|
|
|
end
|
|
|
|
|
|
|
|
user.active = true
|
|
|
|
user.save!
|
|
|
|
|
|
|
|
puts "Granting admin!"
|
|
|
|
user.grant_admin!
|
2018-04-04 20:53:11 -04:00
|
|
|
if user.trust_level < 1
|
|
|
|
user.change_trust_level!(1)
|
|
|
|
end
|
|
|
|
|
2014-09-08 19:03:42 -04:00
|
|
|
user.email_tokens.update_all confirmed: true
|
|
|
|
|
2014-09-11 12:47:17 -04:00
|
|
|
puts "Sending email!"
|
2022-01-20 09:38:56 -05:00
|
|
|
email_token = user.email_tokens.create!(email: user.email, scope: EmailToken.scopes[:password_reset])
|
2022-02-04 12:34:38 -05:00
|
|
|
Jobs.enqueue(:user_email, type: "account_created", user_id: user.id, email_token: email_token.token)
|
2014-09-08 19:03:42 -04:00
|
|
|
end
|
|
|
|
|
2013-06-09 21:56:51 -04:00
|
|
|
desc "Creates a forum administrator"
|
|
|
|
task "admin:create" => :environment do
|
|
|
|
require 'highline/import'
|
2014-07-01 16:33:02 -04:00
|
|
|
|
2013-06-09 21:56:51 -04:00
|
|
|
begin
|
2014-07-02 04:00:38 -04:00
|
|
|
email = ask("Email: ")
|
2014-07-01 16:33:02 -04:00
|
|
|
existing_user = User.find_by_email(email)
|
|
|
|
|
2021-05-20 21:43:47 -04:00
|
|
|
# check if user account already exists
|
2014-07-02 04:00:38 -04:00
|
|
|
if existing_user
|
2014-07-01 16:33:02 -04:00
|
|
|
# user already exists, ask for password reset
|
|
|
|
admin = existing_user
|
2014-07-07 07:28:23 -04:00
|
|
|
reset_password = ask("User with this email already exists! Do you want to reset the password for this email? (Y/n) ")
|
|
|
|
if (reset_password == "" || reset_password.downcase == 'y')
|
2014-07-01 16:33:02 -04:00
|
|
|
begin
|
2014-07-02 04:00:38 -04:00
|
|
|
password = ask("Password: ") { |q| q.echo = false }
|
|
|
|
password_confirmation = ask("Repeat password: ") { |q| q.echo = false }
|
2019-12-12 19:40:33 -05:00
|
|
|
passwords_match = password == password_confirmation
|
|
|
|
|
|
|
|
say("Passwords don't match, try again...") unless passwords_match
|
|
|
|
end while !passwords_match
|
2014-07-01 16:33:02 -04:00
|
|
|
admin.password = password
|
|
|
|
end
|
|
|
|
else
|
|
|
|
# create new user
|
|
|
|
admin = User.new
|
|
|
|
admin.email = email
|
2014-07-16 03:54:40 -04:00
|
|
|
admin.username = UserNameSuggester.suggest(admin.email)
|
2014-07-01 16:33:02 -04:00
|
|
|
begin
|
2018-12-17 22:42:19 -05:00
|
|
|
if ENV["RANDOM_PASSWORD"] == "1"
|
|
|
|
password = password_confirmation = SecureRandom.hex
|
|
|
|
else
|
|
|
|
password = ask("Password: ") { |q| q.echo = false }
|
|
|
|
password_confirmation = ask("Repeat password: ") { |q| q.echo = false }
|
|
|
|
end
|
2019-12-12 19:40:33 -05:00
|
|
|
|
2020-03-15 02:24:11 -04:00
|
|
|
passwords_match = password == password_confirmation
|
|
|
|
|
2019-12-12 19:40:33 -05:00
|
|
|
say("Passwords don't match, try again...") unless passwords_match
|
|
|
|
end while !passwords_match
|
2014-07-01 16:33:02 -04:00
|
|
|
admin.password = password
|
|
|
|
end
|
|
|
|
|
2020-11-02 05:12:42 -05:00
|
|
|
if SiteSetting.full_name_required && admin.name.blank?
|
|
|
|
admin.name = ask("Full name: ")
|
|
|
|
end
|
|
|
|
|
2014-07-01 16:33:02 -04:00
|
|
|
# save/update user account
|
2013-06-09 21:56:51 -04:00
|
|
|
saved = admin.save
|
2019-12-12 19:40:33 -05:00
|
|
|
say(admin.errors.full_messages.join("\n")) unless saved
|
2013-06-09 21:56:51 -04:00
|
|
|
end while !saved
|
2014-07-01 16:33:02 -04:00
|
|
|
|
2014-07-16 03:54:40 -04:00
|
|
|
say "\nEnsuring account is active!"
|
|
|
|
admin.active = true
|
|
|
|
admin.save
|
|
|
|
|
2014-07-02 04:00:38 -04:00
|
|
|
if existing_user
|
2014-07-01 16:33:02 -04:00
|
|
|
say("\nAccount updated successfully!")
|
|
|
|
else
|
|
|
|
say("\nAccount created successfully with username #{admin.username}")
|
|
|
|
end
|
|
|
|
|
|
|
|
# grant admin privileges
|
2014-07-07 07:28:23 -04:00
|
|
|
grant_admin = ask("Do you want to grant Admin privileges to this account? (Y/n) ")
|
|
|
|
if (grant_admin == "" || grant_admin.downcase == 'y')
|
2014-07-01 16:33:02 -04:00
|
|
|
admin.grant_admin!
|
2018-04-07 07:27:17 -04:00
|
|
|
if admin.trust_level < 1
|
|
|
|
admin.change_trust_level!(1)
|
2018-04-04 20:53:11 -04:00
|
|
|
end
|
2014-07-01 16:33:02 -04:00
|
|
|
admin.email_tokens.update_all confirmed: true
|
|
|
|
admin.activate
|
|
|
|
|
|
|
|
say("\nYour account now has Admin privileges!")
|
|
|
|
end
|
|
|
|
|
2013-06-09 21:56:51 -04:00
|
|
|
end
|