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