discourse/lib/tasks/admin.rake

60 lines
1.9 KiB
Ruby
Raw Normal View History

2013-06-10 09:56:51 +08:00
desc "Creates a forum administrator"
task "admin:create" => :environment do
require 'highline/import'
2014-07-02 04:33:02 +08:00
2013-06-10 09:56:51 +08:00
begin
2014-07-02 16:00:38 +08:00
email = ask("Email: ")
2014-07-02 04:33:02 +08:00
existing_user = User.find_by_email(email)
# check if user account already exixts
2014-07-02 16:00:38 +08:00
if existing_user
2014-07-02 04:33:02 +08:00
# user already exists, ask for password reset
admin = existing_user
2014-07-07 19:28:23 +08: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-02 04:33:02 +08:00
begin
2014-07-02 16:00:38 +08:00
password = ask("Password: ") {|q| q.echo = false}
password_confirmation = ask("Repeat password: ") {|q| q.echo = false}
2014-07-02 04:33:02 +08:00
end while password != password_confirmation
admin.password = password
end
else
# create new user
admin = User.new
admin.email = email
username_random = Random.new()
admin.username = "admin_#{username_random.rand(9999)}"
begin
2014-07-02 16:00:38 +08:00
password = ask("Password: ") {|q| q.echo = false}
password_confirmation = ask("Repeat password: ") {|q| q.echo = false}
2014-07-02 04:33:02 +08:00
end while password != password_confirmation
admin.password = password
end
# save/update user account
2013-06-10 09:56:51 +08:00
saved = admin.save
if !saved
puts admin.errors.full_messages.join("\n")
next
end
end while !saved
2014-07-02 04:33:02 +08:00
2014-07-02 16:00:38 +08:00
if existing_user
2014-07-02 04:33:02 +08:00
say("\nAccount updated successfully!")
else
say("\nAccount created successfully with username #{admin.username}")
end
# grant admin privileges
2014-07-07 19:28:23 +08: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-02 04:33:02 +08:00
admin.grant_admin!
admin.change_trust_level!(TrustLevel.levels.max_by{|k, v| v}[0])
admin.email_tokens.update_all confirmed: true
admin.activate
say("\nYour account now has Admin privileges!")
end
2013-06-10 09:56:51 +08:00
end