1 | <?php |
||||
2 | |||||
3 | namespace Dyrynda\Artisan\Console\Commands; |
||||
4 | |||||
5 | use Dyrynda\Artisan\Exceptions\MakeUserException; |
||||
6 | use Exception; |
||||
7 | use Illuminate\Console\Command; |
||||
8 | use Illuminate\Support\Facades\Password; |
||||
9 | use Illuminate\Support\Str; |
||||
10 | |||||
11 | class MakeUser extends Command |
||||
12 | { |
||||
13 | /** |
||||
14 | * The name and signature of the console command. |
||||
15 | * |
||||
16 | * @var string |
||||
17 | */ |
||||
18 | protected $signature = 'make:user'; |
||||
19 | |||||
20 | /** |
||||
21 | * The console command description. |
||||
22 | * |
||||
23 | * @var string |
||||
24 | */ |
||||
25 | protected $description = 'Create a new application user'; |
||||
26 | |||||
27 | /** |
||||
28 | * Array of custom fields to attach to the user. |
||||
29 | * |
||||
30 | * @var array |
||||
31 | */ |
||||
32 | protected $customFields = []; |
||||
33 | |||||
34 | /** |
||||
35 | * Execute the console command. |
||||
36 | * |
||||
37 | * Handle creation of the new application user. |
||||
38 | * |
||||
39 | * @return void |
||||
40 | */ |
||||
41 | 2 | public function handle() |
|||
42 | { |
||||
43 | 2 | $email = $this->ask("What is the new user's email address?"); |
|||
44 | 2 | $name = $this->ask("What is the new user's name?") ?: ''; |
|||
45 | 2 | $password = $this->secret("What is the new user's password? (blank generates a random one)") ?: Str::random(32); |
|||
46 | 2 | $encrypt = $this->confirm('Should the password be encrypted?', true); |
|||
47 | 2 | $sendReset = $this->confirm('Do you want to send a password reset email?'); |
|||
48 | |||||
49 | 2 | if ($encrypt) { |
|||
50 | 2 | $password = bcrypt($password); |
|||
51 | } |
||||
52 | |||||
53 | 2 | while ($custom = $this->ask('Do you have any custom user fields to add? Field=Value (blank continues)', false)) { |
|||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
54 | 1 | [$key, $value] = explode('=', $custom); |
|||
55 | 1 | $this->customFields[$key] = value($value); |
|||
56 | } |
||||
57 | |||||
58 | try { |
||||
59 | 2 | app('db')->beginTransaction(); |
|||
60 | |||||
61 | $this->validateEmail($email); |
||||
62 | |||||
63 | app(config('auth.providers.users.model'))->create(array_merge( |
||||
0 ignored issues
–
show
The method
create() does not exist on Illuminate\Contracts\Foundation\Application .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||
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 | 2 | } catch (Exception $e) { |
|||
78 | 2 | $this->error($e->getMessage()); |
|||
79 | |||||
80 | 2 | $this->error('The user was not created'); |
|||
81 | |||||
82 | 2 | app('db')->rollBack(); |
|||
83 | } |
||||
84 | 2 | } |
|||
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()) { |
||||
0 ignored issues
–
show
The method
where() does not exist on Illuminate\Contracts\Foundation\Application .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||
101 | throw MakeUserException::emailExists($email); |
||||
102 | } |
||||
103 | } |
||||
104 | } |
||||
105 |