2014-09-09 09:03:42 +10: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!
user . change_trust_level! ( 4 )
user . email_tokens . update_all confirmed : true
2014-09-11 12:47:17 -04:00
puts " Sending email! "
2014-09-09 09:03:42 +10:00
email_token = user . email_tokens . create ( email : user . email )
2014-09-11 12:47:17 -04:00
Jobs . enqueue ( :user_email , type : :account_created , user_id : user . id , email_token : email_token . token )
2014-09-09 09:03:42 +10:00
end
2013-06-10 05:56:51 +04:00
desc " Creates a forum administrator "
task " admin:create " = > :environment do
require 'highline/import'
2014-07-02 02:03:02 +05:30
2013-06-10 05:56:51 +04:00
begin
2014-07-02 13:30:38 +05:30
email = ask ( " Email: " )
2014-07-02 02:03:02 +05:30
existing_user = User . find_by_email ( email )
# check if user account already exixts
2014-07-02 13:30:38 +05:30
if existing_user
2014-07-02 02:03:02 +05:30
# user already exists, ask for password reset
admin = existing_user
2014-07-07 16:58:23 +05:30
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 02:03:02 +05:30
begin
2014-07-02 13:30:38 +05:30
password = ask ( " Password: " ) { | q | q . echo = false }
password_confirmation = ask ( " Repeat password: " ) { | q | q . echo = false }
2014-07-02 02:03:02 +05:30
end while password != password_confirmation
admin . password = password
end
else
# create new user
admin = User . new
admin . email = email
2014-07-16 17:54:40 +10:00
admin . username = UserNameSuggester . suggest ( admin . email )
2014-07-02 02:03:02 +05:30
begin
2014-07-02 13:30:38 +05:30
password = ask ( " Password: " ) { | q | q . echo = false }
password_confirmation = ask ( " Repeat password: " ) { | q | q . echo = false }
2014-07-02 02:03:02 +05:30
end while password != password_confirmation
admin . password = password
end
# save/update user account
2013-06-10 05:56:51 +04:00
saved = admin . save
if ! saved
puts admin . errors . full_messages . join ( " \n " )
next
end
end while ! saved
2014-07-02 02:03:02 +05:30
2014-07-16 17:54:40 +10:00
say " \n Ensuring account is active! "
admin . active = true
admin . save
2014-07-02 13:30:38 +05:30
if existing_user
2014-07-02 02:03:02 +05:30
say ( " \n Account updated successfully! " )
else
say ( " \n Account created successfully with username #{ admin . username } " )
end
# grant admin privileges
2014-07-07 16:58:23 +05:30
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 02:03:02 +05:30
admin . grant_admin!
2014-09-05 17:49:51 +10:00
admin . change_trust_level! ( 4 )
2014-07-02 02:03:02 +05:30
admin . email_tokens . update_all confirmed : true
admin . activate
say ( " \n Your account now has Admin privileges! " )
end
2013-06-10 05:56:51 +04:00
end