Completed
Push — master ( 5ca9ad...782d0f )
by Song
03:18
created

ResetPasswordCommand::handle()   A

Complexity

Conditions 3
Paths 0

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 0
nop 0
dl 0
loc 30
rs 9.44
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Console;
4
5
use Encore\Admin\Auth\Database\Administrator;
6
use Illuminate\Console\Command;
7
8
class ResetPasswordCommand extends Command
9
{
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'admin:reset-password';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Reset password for a specific admin user';
23
24
    /**
25
     * Execute the console command.
26
     */
27
    public function handle()
28
    {
29
        $users = Administrator::all();
30
31
        askForUserName:
32
        $username = $this->askWithCompletion('Please enter a username who needs to reset his password', $users->pluck('username')->toArray());
33
34
        $user = $users->first(function ($user) use ($username) {
35
            return $user->username == $username;
36
        });
37
38
        if (is_null($user)) {
39
            $this->error('The user you entered is not exists');
40
            goto askForUserName;
41
        }
42
43
        enterPassword:
44
        $password = $this->secret('Please enter a password');
45
46
        if ($password !== $this->secret('Please confirm the password')) {
47
            $this->error('The passwords entered twice do not match, please re-enter');
48
            goto enterPassword;
49
        }
50
51
        $user->password = bcrypt($password);
52
53
        $user->save();
54
55
        $this->info('User password reset successfully.');
56
    }
57
}