My beer compendium
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
2.7 KiB

3 years ago
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\User;
  5. class UserCreate extends Command
  6. {
  7. /**
  8. * The name and signature of the console command.
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'user:create {--name=} {--username=} {--email=} {--password=} {--is_admin=0} {--confirm_email=0}';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = 'Create a new user';
  19. /**
  20. * Create a new command instance.
  21. *
  22. * @return void
  23. */
  24. public function __construct()
  25. {
  26. parent::__construct();
  27. }
  28. /**
  29. * Execute the console command.
  30. *
  31. * @return mixed
  32. */
  33. public function handle()
  34. {
  35. $this->info('Creating a new user...');
  36. $o = $this->options();
  37. if( $o['name'] &&
  38. $o['username'] &&
  39. $o['email'] &&
  40. $o['password']
  41. ) {
  42. $user = new User;
  43. $user->username = $o['username'];
  44. $user->name = $o['name'];
  45. $user->email = $o['email'];
  46. $user->password = bcrypt($o['password']);
  47. $user->is_admin = (bool) $o['is_admin'];
  48. $user->email_verified_at = (bool) $o['confirm_email'] ? now() : null;
  49. $user->save();
  50. $this->info('Successfully created user!');
  51. return;
  52. }
  53. $name = $this->ask('Name');
  54. $username = $this->ask('Username');
  55. if(User::whereUsername($username)->exists()) {
  56. $this->error('Username already in use, please try again...');
  57. exit;
  58. }
  59. $email = $this->ask('Email');
  60. if(User::whereEmail($email)->exists()) {
  61. $this->error('Email already in use, please try again...');
  62. exit;
  63. }
  64. $password = $this->secret('Password');
  65. $confirm = $this->secret('Confirm Password');
  66. if($password !== $confirm) {
  67. $this->error('Password mismatch, please try again...');
  68. exit;
  69. }
  70. $is_admin = $this->confirm('Make this user an admin?');
  71. $confirm_email = $this->confirm('Manually verify email address?');
  72. if($this->confirm('Are you sure you want to create this user?') &&
  73. $username &&
  74. $name &&
  75. $email &&
  76. $password
  77. ) {
  78. $user = new User;
  79. $user->username = $username;
  80. $user->name = $name;
  81. $user->email = $email;
  82. $user->password = bcrypt($password);
  83. $user->is_admin = $is_admin;
  84. $user->email_verified_at = $confirm_email ? now() : null;
  85. $user->save();
  86. $this->info('Created new user!');
  87. }
  88. }
  89. }