Guest User

Untitled

a guest
Apr 8th, 2018
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. namespace :admin do
  2. desc 'Create admin user'
  3. task create: :environment do
  4. require 'highline/import'
  5.  
  6. class User
  7. def reset_password
  8. begin
  9. password = ask('Password: ') { |q| q.echo = 'x' }
  10. password_confirmation = ask('Repeat password: ') { |q| q.echo = 'x' }
  11. end while password != password_confirmation
  12. self.password = password
  13. self.password_confirmation = password
  14. end
  15. end
  16.  
  17. begin
  18. email = ask('Email: ')
  19. existing_user = User.find_by_email(email)
  20.  
  21. # check if user account already exists
  22. if existing_user
  23. user = existing_user
  24. # user already exists, ask for password reset
  25. reset_password = ask('User with this email already exists! Do you want to reset the password for this email? (Y/n) ')
  26. user.reset_password if yes?(reset_password)
  27. else
  28. # create new user otherwise
  29. user = User.new(email: email, confirmed_at: Time.current)
  30. user.reset_password
  31. end
  32.  
  33. saved = user.save
  34. if !saved
  35. puts admin.errors.full_messages.join("\n")
  36. next
  37. end
  38.  
  39. grant_admin = ask('Do you want to grant Admin privileges to this account? (Y/n) ')
  40. if yes?(grant_admin)
  41. user.role = :admin
  42. say("\nYour account now has Admin privileges!") if user.save
  43. end
  44. end while !saved
  45. end
  46. end
  47.  
  48. def yes?(string)
  49. string = string.strip.downcase
  50. string.empty? || string == 'y' || string == 'yes'
  51. end
Add Comment
Please, Sign In to add comment