|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use App\Models\User; |
|
6
|
|
|
use Illuminate\Console\Command; |
|
7
|
|
|
|
|
8
|
|
|
class AddUser extends Command |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* The name and signature of the console command. |
|
12
|
|
|
* |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $signature = 'user:add |
|
16
|
|
|
{username : The username to add} |
|
17
|
|
|
{password? : Optional} |
|
18
|
|
|
{--realname= : The real name of the user} |
|
19
|
|
|
{--email= : The email of the user} |
|
20
|
|
|
{--read : Global Read access} |
|
21
|
|
|
{--admin : Global Admin access}'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The console command description. |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $description = 'Add a new user (if supported by the authentication type)'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Create a new command instance. |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct() |
|
34
|
|
|
{ |
|
35
|
|
|
parent::__construct(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Execute the console command. |
|
40
|
|
|
* |
|
41
|
|
|
* @return mixed |
|
42
|
|
|
*/ |
|
43
|
|
|
public function handle() |
|
44
|
|
|
{ |
|
45
|
|
|
$user = new User(); |
|
46
|
|
|
|
|
47
|
|
|
// set username |
|
48
|
|
|
$user->username = $this->argument('username'); |
|
49
|
|
|
if (User::where('username', $user->username)->exists()) { |
|
50
|
|
|
$this->error('User '.$user->username.' exists.'); |
|
51
|
|
|
return; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// set realname |
|
55
|
|
|
if ($this->option('realname')) { |
|
56
|
|
|
$user->realname = $this->option('realname'); |
|
57
|
|
|
} |
|
58
|
|
|
else { |
|
59
|
|
|
$user->realname = $this->ask('Real Name'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// set email |
|
63
|
|
|
if ($this->option('email')) { |
|
64
|
|
|
$user->email = $this->option('email'); |
|
65
|
|
|
} |
|
66
|
|
|
else { |
|
67
|
|
|
$user->email = $this->ask('Email'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
// set user level, start with 1 and upgrade as specified |
|
71
|
|
|
$user->level = 1; |
|
72
|
|
|
if ($this->option('read')) { |
|
73
|
|
|
$user->level = 5; |
|
74
|
|
|
} |
|
75
|
|
|
if ($this->option('admin')) { |
|
76
|
|
|
$user->level = 10; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
// set password |
|
80
|
|
|
if ($this->argument('password')) { |
|
81
|
|
|
$user->password = $this->argument('password'); |
|
82
|
|
|
} |
|
83
|
|
|
else { |
|
84
|
|
|
$user->password = $this->secret('Password'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// save user |
|
88
|
|
|
if ($user->save()) { |
|
89
|
|
|
$this->info('User '.$user->username.' created.'); |
|
90
|
|
|
} |
|
91
|
|
|
else { |
|
92
|
|
|
$this->error('Failed to create user '.$user->username); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|