1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* NOTICE OF LICENSE |
5
|
|
|
* |
6
|
|
|
* Part of the Rinvex Fort Package. |
7
|
|
|
* |
8
|
|
|
* This source file is subject to The MIT License (MIT) |
9
|
|
|
* that is bundled with this package in the LICENSE file. |
10
|
|
|
* |
11
|
|
|
* Package: Rinvex Fort Package |
12
|
|
|
* License: The MIT License (MIT) |
13
|
|
|
* Link: https://rinvex.com |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Rinvex\Fort\Console\Commands; |
17
|
|
|
|
18
|
|
|
use Illuminate\Console\Command; |
19
|
|
|
use Illuminate\Support\Facades\Lang; |
20
|
|
|
use Illuminate\Contracts\Validation\Factory; |
21
|
|
|
|
22
|
|
|
class UserUpdateCommand extends Command |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* The name and signature of the console command. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $signature = 'fort:user:update |
30
|
|
|
{field? : The identifier of the user (id, email, username)} |
31
|
|
|
{--E|email= : The Email of the user} |
32
|
|
|
{--U|username= : The username of the user} |
33
|
|
|
{--P|password= : The password of the user} |
34
|
|
|
{--F|firstName= : The first name of the user} |
35
|
|
|
{--M|middleName= : The middle name of the user} |
36
|
|
|
{--L|lastName= : The last name of the user} |
37
|
|
|
{--A|active : The active statuse of the user} |
38
|
|
|
{--I|inactive : Set the user as inactive}'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The console command description. |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $description = 'Update an existing user.'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Execute the console command. |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
public function handle() |
53
|
|
|
{ |
54
|
|
|
$data = array_filter([ |
55
|
|
|
|
56
|
|
|
// Required user attributes |
57
|
|
|
'email' => $this->option('email'), |
58
|
|
|
'username' => $this->option('username'), |
59
|
|
|
'password' => $this->option('password') ? bcrypt($this->option('password')) : null, |
|
|
|
|
60
|
|
|
'first_name' => $this->option('firstName'), |
61
|
|
|
'middle_name' => $this->option('middleName'), |
62
|
|
|
'last_name' => $this->option('lastName'), |
63
|
|
|
'active' => $this->option('active') ?: ! $this->option('inactive'), |
64
|
|
|
|
65
|
|
|
], [ |
66
|
|
|
$this, |
67
|
|
|
'filter' |
68
|
|
|
]); |
69
|
|
|
|
70
|
|
|
// Get required argument |
71
|
|
|
$field = $this->argument('field') ?: $this->ask(Lang::get('rinvex.fort::artisan.user.invalid')); |
72
|
|
|
|
73
|
|
|
// Find single user |
74
|
|
|
if (intval($field)) { |
75
|
|
|
$user = $this->laravel['rinvex.fort.user']->find($field); |
76
|
|
|
} else if (filter_var($field, FILTER_VALIDATE_EMAIL)) { |
77
|
|
|
$user = $this->laravel['rinvex.fort.user']->findWhere(['email' => $field])->first(); |
78
|
|
|
} else { |
79
|
|
|
$user = $this->laravel['rinvex.fort.user']->findWhere(['username' => $field])->first(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (! $user) { |
83
|
|
|
return $this->error(Lang::get('rinvex.fort::artisan.user.invalid', ['field' => $field])); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$rules = [ |
87
|
|
|
'email' => 'sometimes|required|email|max:255|unique:'.config('rinvex.fort.tables.users').',email', |
88
|
|
|
'username' => 'sometimes|required|max:255|unique:'.config('rinvex.fort.tables.users').',username', |
89
|
|
|
]; |
90
|
|
|
|
91
|
|
|
if (! empty($data)) { |
92
|
|
|
$validator = app(Factory::class)->make($data, $rules); |
93
|
|
|
|
94
|
|
|
if ($validator->fails()) { |
95
|
|
|
$this->error('Errors:'); |
96
|
|
|
|
97
|
|
|
foreach ($validator->errors()->getMessages() as $key => $messages) { |
98
|
|
|
$this->error('- '.$key.': '.$messages[0]); |
99
|
|
|
} |
100
|
|
|
} else { |
101
|
|
|
$user->update($data); |
102
|
|
|
|
103
|
|
|
$this->info(Lang::get('rinvex.fort::artisan.user.updated').' ['.Lang::get('rinvex.fort::artisan.user.id').': '.$user->id.', '.Lang::get('rinvex.fort::artisan.user.email').': '.$user->email.', '.Lang::get('rinvex.fort::artisan.user.username').': '.$user->username.']'); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
} else { |
106
|
|
|
$this->info(Lang::get('rinvex.fort::artisan.user.nothing')); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Filter null and empty values. |
112
|
|
|
* |
113
|
|
|
* @param $value |
114
|
|
|
* |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
|
|
protected function filter($value) |
118
|
|
|
{ |
119
|
|
|
return ($value !== null && $value !== ''); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.