Merge pull request #2489 from techAPJ/update_rake_task
Update admin rake task
This commit is contained in:
commit
2a998fd787
lib/tasks
|
@ -1,24 +1,59 @@
|
||||||
desc "Creates a forum administrator"
|
desc "Creates a forum administrator"
|
||||||
task "admin:create" => :environment do
|
task "admin:create" => :environment do
|
||||||
require 'highline/import'
|
require 'highline/import'
|
||||||
|
|
||||||
begin
|
begin
|
||||||
admin = User.new
|
email = ask("Email:")
|
||||||
admin.email = ask("Email:")
|
existing_user = User.find_by_email(email)
|
||||||
admin.username = "admin"
|
|
||||||
|
# check if user account already exixts
|
||||||
|
if !existing_user.nil?
|
||||||
|
# user already exists, ask for password reset
|
||||||
|
admin = existing_user
|
||||||
|
reset_password = ask("User with this email already exists! Do you want to reset the password for this email? Type 'Y' for Yes, 'N' for No.")
|
||||||
|
if (reset_password.downcase == 'y')
|
||||||
begin
|
begin
|
||||||
password = ask("Password:") {|q| q.echo = false}
|
password = ask("Password:") {|q| q.echo = false}
|
||||||
password_confirmation = ask("Repeat password:") {|q| q.echo = false}
|
password_confirmation = ask("Repeat password:") {|q| q.echo = false}
|
||||||
end while password != password_confirmation
|
end while password != password_confirmation
|
||||||
admin.password = password
|
admin.password = password
|
||||||
# admin.email_confirmed = true
|
end
|
||||||
|
else
|
||||||
|
# create new user
|
||||||
|
admin = User.new
|
||||||
|
admin.email = email
|
||||||
|
username_random = Random.new()
|
||||||
|
admin.username = "admin_#{username_random.rand(9999)}"
|
||||||
|
begin
|
||||||
|
password = ask("Password:") {|q| q.echo = false}
|
||||||
|
password_confirmation = ask("Repeat password:") {|q| q.echo = false}
|
||||||
|
end while password != password_confirmation
|
||||||
|
admin.password = password
|
||||||
|
end
|
||||||
|
|
||||||
|
# save/update user account
|
||||||
saved = admin.save
|
saved = admin.save
|
||||||
if !saved
|
if !saved
|
||||||
puts admin.errors.full_messages.join("\n")
|
puts admin.errors.full_messages.join("\n")
|
||||||
next
|
next
|
||||||
end
|
end
|
||||||
end while !saved
|
end while !saved
|
||||||
|
|
||||||
|
if !existing_user.nil?
|
||||||
|
say("\nAccount updated successfully!")
|
||||||
|
else
|
||||||
|
say("\nAccount created successfully with username #{admin.username}")
|
||||||
|
end
|
||||||
|
|
||||||
|
# grant admin privileges
|
||||||
|
reset_password = ask("Do you want to grant Admin privileges to this account? Type 'Y' for Yes, 'N' for No.")
|
||||||
|
if (reset_password.downcase == 'y')
|
||||||
admin.grant_admin!
|
admin.grant_admin!
|
||||||
admin.change_trust_level!(TrustLevel.levels.max_by{|k, v| v}[0])
|
admin.change_trust_level!(TrustLevel.levels.max_by{|k, v| v}[0])
|
||||||
admin.email_tokens.update_all confirmed: true
|
admin.email_tokens.update_all confirmed: true
|
||||||
admin.activate
|
admin.activate
|
||||||
|
|
||||||
|
say("\nYour account now has Admin privileges!")
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue