1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dyrynda\Artisan\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
use Illuminate\Support\Facades\Password; |
8
|
|
|
use Dyrynda\Artisan\Exceptions\MakeUserException; |
9
|
|
|
|
10
|
|
|
class MakeUser extends Command |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The name and signature of the console command. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $signature = 'make:user'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The console command description. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $description = 'Create a new application user'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Array of custom fields to attach to the user. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $customFields = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Execute the console command. |
35
|
|
|
* |
36
|
|
|
* Handle creation of the new application user. |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
2 |
|
public function handle() |
41
|
|
|
{ |
42
|
2 |
|
$email = $this->ask("What is the new user's email address?"); |
43
|
2 |
|
$name = $this->ask("What is the new user's name?") ?: ''; |
44
|
2 |
|
$password = bcrypt($this->secret("What is the new user's password? (blank generates a random one)", str_random(32))); |
|
|
|
|
45
|
2 |
|
$sendReset = $this->confirm('Do you want to send a password reset email?'); |
46
|
|
|
|
47
|
2 |
|
while ($custom = $this->ask('Do you have any custom user fields to add? Field=Value (blank continues)', false)) { |
|
|
|
|
48
|
1 |
|
list($key, $value) = explode('=', $custom); |
49
|
1 |
|
$this->customFields[$key] = value($value); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
try { |
53
|
2 |
|
app('db')->beginTransaction(); |
54
|
|
|
|
55
|
|
|
$this->validateEmail($email); |
56
|
|
|
|
57
|
|
|
app(config('auth.providers.users.model'))->create(array_merge( |
|
|
|
|
58
|
|
|
compact('email', 'name', 'password'), |
59
|
|
|
$this->customFields |
60
|
|
|
)); |
61
|
|
|
|
62
|
|
|
if ($sendReset) { |
63
|
|
|
Password::sendResetLink(compact('email')); |
64
|
|
|
|
65
|
|
|
$this->info("Sent password reset email to {$email}"); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->info("Created new user for email {$email}"); |
69
|
|
|
|
70
|
|
|
app('db')->commit(); |
71
|
2 |
|
} catch (Exception $e) { |
72
|
2 |
|
$this->error($e->getMessage()); |
73
|
|
|
|
74
|
2 |
|
$this->error('The user was not created'); |
75
|
|
|
|
76
|
2 |
|
app('db')->rollBack(); |
77
|
|
|
} |
78
|
2 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Determine if the given email address already exists. |
82
|
|
|
* |
83
|
|
|
* @param string $email |
84
|
|
|
* @return void |
85
|
|
|
* |
86
|
|
|
* @throws \Dyrynda\Artisan\Exceptions\MakeUserException |
87
|
|
|
*/ |
88
|
|
|
private function validateEmail($email) |
89
|
|
|
{ |
90
|
|
|
if (! filter_var($email, FILTER_VALIDATE_EMAIL)) { |
91
|
|
|
throw MakeUserException::invalidEmail($email); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (app(config('auth.providers.users.model'))->where('email', $email)->exists()) { |
|
|
|
|
95
|
|
|
throw MakeUserException::emailExists($email); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|