├── src ├── MakeUserServiceProvider.php ├── Exceptions │ └── MakeUserException.php └── Console │ └── Commands │ └── MakeUser.php ├── LICENSE.txt ├── composer.json └── .github └── workflows └── run-tests.yml /src/MakeUserServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->singleton('dyrynda.artisan.make:user', function ($app) { 18 | return $app->make(MakeUser::class); 19 | }); 20 | 21 | $this->commands('dyrynda.artisan.make:user'); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/Exceptions/MakeUserException.php: -------------------------------------------------------------------------------- 1 | ask("What is the new user's email address?"); 44 | $name = $this->ask("What is the new user's name?") ?: ''; 45 | $password = $this->secret("What is the new user's password? (blank generates a random one)") ?: Str::random(32); 46 | $encrypt = $this->confirm('Should the password be encrypted?', true); 47 | $sendReset = $this->confirm('Do you want to send a password reset email?'); 48 | 49 | if ($encrypt) { 50 | $password = bcrypt($password); 51 | } 52 | 53 | while ($custom = $this->ask('Do you have any custom user fields to add? Field=Value (blank continues)', false)) { 54 | [$key, $value] = explode('=', $custom); 55 | $this->customFields[$key] = value($value); 56 | } 57 | 58 | try { 59 | app('db')->beginTransaction(); 60 | 61 | $this->validateEmail($email); 62 | 63 | app(config('auth.providers.users.model'))->create(array_merge( 64 | compact('email', 'name', 'password'), 65 | $this->customFields 66 | )); 67 | 68 | if ($sendReset) { 69 | Password::sendResetLink(compact('email')); 70 | 71 | $this->info("Sent password reset email to {$email}"); 72 | } 73 | 74 | $this->info("Created new user for email {$email}"); 75 | 76 | app('db')->commit(); 77 | } catch (Exception $e) { 78 | $this->error($e->getMessage()); 79 | 80 | $this->error('The user was not created'); 81 | 82 | app('db')->rollBack(); 83 | } 84 | } 85 | 86 | /** 87 | * Determine if the given email address already exists. 88 | * 89 | * @param string $email 90 | * @return void 91 | * 92 | * @throws \Dyrynda\Artisan\Exceptions\MakeUserException 93 | */ 94 | private function validateEmail($email) 95 | { 96 | if (! filter_var($email, FILTER_VALIDATE_EMAIL)) { 97 | throw MakeUserException::invalidEmail($email); 98 | } 99 | 100 | if (app(config('auth.providers.users.model'))->where('email', $email)->exists()) { 101 | throw MakeUserException::emailExists($email); 102 | } 103 | } 104 | } 105 | --------------------------------------------------------------------------------