CreateSuperManager.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace App\Console\Commands\Init;
  3. use App\Repositories\ManagerRepository;
  4. use Illuminate\Console\Command;
  5. use Otp\GoogleAuthenticator;
  6. class CreateSuperManager extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'init:create_super_manager';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = '创建一个超管';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return mixed
  33. */
  34. public function handle()
  35. {
  36. if (app()->environment() != 'local') {
  37. $this->error('仅允许开发环境');
  38. return false;
  39. }
  40. /**
  41. * @var ManagerRepository $managerRepo
  42. */
  43. $managerRepo = app(ManagerRepository::class);
  44. $username = strtolower('adminboss');
  45. $nick = '超管admin';
  46. $passwordTxt = 'qwe123456';
  47. $email = strtolower('adminboss@test.com');
  48. $salt = 'cTYcEH';
  49. $password = $managerRepo->generatePassword($passwordTxt, $salt);
  50. $roleId = 1;
  51. $roleName = '超管';
  52. $otpSecret = GoogleAuthenticator::generateRandom();
  53. $userInfo = [
  54. 'username' => $username,
  55. 'nick' => $nick,
  56. 'role_id' => $roleId,
  57. 'role_name' => $roleName,
  58. 'password' => $password,
  59. 'salt' => $salt,
  60. 'email' => $email,
  61. 'otp_secret' => $otpSecret,
  62. ];
  63. if ($managerRepo->isExistsUsername($username)) {
  64. $this->error('用户名已经存在');
  65. return false;
  66. }
  67. $result = $managerRepo->create($userInfo);
  68. if (!$result) {
  69. $this->error('创建失败');
  70. return false;
  71. } else {
  72. $this->info('创建超管成功,用户名:'. $username);
  73. return true;
  74. }
  75. }
  76. }