1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
4
|
|
|
|
5
|
|
|
use App\Models\User; |
6
|
|
|
use Illuminate\Console\Command; |
7
|
|
|
|
8
|
|
|
class ManageUsers extends Command |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The name and signature of the console command. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $signature = 'mmex:user {email?} {password?} {--delete} {--admin} {--noadmin}'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The console command description. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $description = 'Add, remove and change users from commandline'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Create a new command instance. |
26
|
|
|
*/ |
27
|
|
|
public function __construct() |
28
|
|
|
{ |
29
|
|
|
parent::__construct(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Execute the console command. |
34
|
|
|
* |
35
|
|
|
* @return mixed |
36
|
|
|
*/ |
37
|
|
|
public function handle() |
38
|
|
|
{ |
39
|
|
|
$email = $this->argument('email'); |
40
|
|
|
$password = $this->argument('password'); |
41
|
|
|
|
42
|
|
|
$delete = $this->option('delete'); |
43
|
|
|
$admin = $this->option('admin'); |
44
|
|
|
$noadmin = $this->option('noadmin'); |
45
|
|
|
|
46
|
|
|
$user = User::whereEmail($email)->first(); |
47
|
|
|
|
48
|
|
|
if ($delete) { |
49
|
|
|
if (!$user) { |
50
|
|
|
$this->error(sprintf('User %s does not exist!', $email)); |
51
|
|
|
} else { |
52
|
|
|
$user->delete(); |
53
|
|
|
|
54
|
|
|
$this->warn(sprintf('%s was deleted', $user->username)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (empty($email) && empty($password)) { |
61
|
|
|
$headers = ['Name', 'Email', 'Admin', 'MMEX Guid', 'Locale', 'API Key', 'Created at']; |
62
|
|
|
|
63
|
|
|
$users = User::all(['name', 'email', 'is_admin', 'mmex_guid', 'locale', 'api_token', 'created_at'])->toArray(); |
64
|
|
|
|
65
|
|
|
$this->info(sprintf('%d users found', count($users))); |
66
|
|
|
|
67
|
|
|
$this->table($headers, $users); |
68
|
|
|
|
69
|
|
|
return; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (!$user) { |
73
|
|
|
$name = $this->ask('Name of user?'); |
74
|
|
|
$user = User::create([ |
75
|
|
|
'name' => $name, |
76
|
|
|
'email' => $email, |
77
|
|
|
'password' => bcrypt($password), |
|
|
|
|
78
|
|
|
]); |
79
|
|
|
|
80
|
|
|
$this->info(sprintf('User %s was created!', $user->username)); |
81
|
|
|
} else { |
82
|
|
|
if ($password) { |
83
|
|
|
$user->password = bcrypt($password); |
|
|
|
|
84
|
|
|
$user->save(); |
85
|
|
|
|
86
|
|
|
$this->info(sprintf('%s\'s password was updated!', $user->name)); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if ($admin || $noadmin) { |
91
|
|
|
if (!$user) { |
92
|
|
|
$this->error(sprintf('User %s does not exist!', $email)); |
93
|
|
|
} else { |
94
|
|
|
$user->is_admin = $admin || $noadmin; |
95
|
|
|
$user->save(); |
96
|
|
|
|
97
|
|
|
if ($admin == true) { |
98
|
|
|
$this->info('Admin privileges set'); |
99
|
|
|
} else { |
100
|
|
|
$this->info('Admin privileges removed'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.