|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Luilliarcec\UserCommands\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Hash; |
|
6
|
|
|
use Illuminate\Support\Facades\Validator; |
|
7
|
|
|
|
|
8
|
|
|
class ResetUserPasswordCommand extends UserCommand |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* The name and signature of the console command. |
|
12
|
|
|
* |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $signature = 'user:reset-password |
|
16
|
|
|
{value : Get user for a value (by email, id)} |
|
17
|
|
|
{field? : Field to search by}'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The console command description. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $description = 'Restore a user`s password'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Execute the console command. |
|
28
|
|
|
*/ |
|
29
|
|
|
public function handle() |
|
30
|
|
|
{ |
|
31
|
|
|
$password = $this->secret('password:'); |
|
32
|
|
|
$password_confirmation = $this->secret('password confirmation:'); |
|
33
|
|
|
|
|
34
|
|
|
$passes = $this->validate([ |
|
35
|
|
|
'password' => $password, |
|
36
|
|
|
'password_confirmation' => $password_confirmation |
|
37
|
|
|
]); |
|
38
|
|
|
|
|
39
|
|
|
if (!$passes) return 1; |
|
40
|
|
|
|
|
41
|
|
|
$user = $this->getUserModel(); |
|
42
|
|
|
|
|
43
|
|
|
if (is_null($user)) { |
|
44
|
|
|
$this->error('Oops, the user was not found!'); |
|
45
|
|
|
return 1; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$user->forceFill([ |
|
49
|
|
|
'password' => Hash::make($password) |
|
50
|
|
|
])->save(); |
|
51
|
|
|
|
|
52
|
|
|
$this->info('User password was successfully restored!'); |
|
53
|
|
|
|
|
54
|
|
|
return 0; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Validate data and show errors |
|
59
|
|
|
* |
|
60
|
|
|
* @param array $data |
|
61
|
|
|
* @return bool |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function validate(array $data): bool |
|
64
|
|
|
{ |
|
65
|
|
|
$validator = Validator::make($data, $this->rules()); |
|
66
|
|
|
|
|
67
|
|
|
if ($validator->fails()) { |
|
68
|
|
|
$this->info('User not created. See error messages below:'); |
|
69
|
|
|
|
|
70
|
|
|
foreach ($validator->errors()->all() as $error) { |
|
71
|
|
|
$this->error($error); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return true; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Validation rules |
|
82
|
|
|
* |
|
83
|
|
|
* @return array |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function rules(): array |
|
86
|
|
|
{ |
|
87
|
|
|
return [ |
|
88
|
|
|
'password' => ['required', 'string', 'confirmed'], |
|
89
|
|
|
]; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|