forked from cachethq/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakeUserCommand.php
More file actions
139 lines (111 loc) 路 2.99 KB
/
Copy pathMakeUserCommand.php
File metadata and controls
139 lines (111 loc) 路 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
namespace Cachet\Commands;
use Cachet\Cachet;
use Illuminate\Console\Command;
use function Laravel\Prompts\confirm;
use function Laravel\Prompts\password;
use function Laravel\Prompts\text;
class MakeUserCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'cachet:make:user {email?} {--password= : The user\'s password} {--admin= : Whether the user is an admin} {--name= : The name of the user }';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new user';
/**
* The user's data.
*/
protected array $data = [];
/**
* The user's email.
*/
protected ?string $email = null;
/**
* The user's password.
*/
protected ?string $password = null;
/**
* Whether the user is an admin.
*/
protected ?bool $isAdmin = null;
/**
* Execute the console command.
*/
public function handle(): int
{
$this->email = $this->argument('email');
$this->isAdmin = $this->option('admin') !== null ? (bool) $this->option('admin') : null;
$this->password = $this->option('password');
$this->data['name'] = $this->option('name');
if (! $this->data['name']) {
$this->promptName();
}
if (! $this->email) {
$this->promptEmail();
}
if ($this->isAdmin === null) {
$this->promptIsAdmin();
}
if (! $this->password) {
$this->promptPassword();
}
$this->createUser();
return self::SUCCESS;
}
/**
* Prompt the user for their email.
*/
protected function promptEmail(): self
{
$this->email = text('What is the user\'s email?', required: true);
return $this;
}
/**
* Prompt the user for their name.
*/
protected function promptName(): self
{
$this->data['name'] = text('What is the user\'s name?', required: true);
return $this;
}
/**
* Prompt the user for their password.
*/
protected function promptPassword(): self
{
if ($this->password) {
return $this;
}
$this->password = password('What is the user\'s password?', required: true);
return $this;
}
/**
* Prompt the user for whether they are an admin.
*/
protected function promptIsAdmin(): self
{
$this->isAdmin = confirm('Is the user an admin?', default: false);
return $this;
}
/**
* Create the user.
*/
protected function createUser(): void
{
$userModel = Cachet::userModel();
$userModel::create([
'name' => $this->data['name'],
'email' => $this->email,
'password' => bcrypt($this->password),
'is_admin' => $this->isAdmin,
]);
$this->components->info('User created successfully.');
}
}