Passed
Push — master ( 707992...a76c69 )
by Iman
03:43
created

AuthController::postUnlockScreen()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
nc 2
nop 0
dl 0
loc 10
c 0
b 0
f 0
cc 2
rs 9.4285
1
<?php
2
3
namespace crocodicstudio\crudbooster\Modules\AuthModule;
4
5
use crocodicstudio\crudbooster\CBCoreModule\CbUsersRepo;
6
use crocodicstudio\crudbooster\controllers\Controller;
7
use crocodicstudio\crudbooster\helpers\Mailer;
8
use Illuminate\Support\Facades\Request;
9
use Illuminate\Support\Facades\Session;
10
use Illuminate\Support\Facades\Validator;
11
use crocodicstudio\crudbooster\helpers\CRUDBooster, CB;
12
13
class AuthController extends Controller
14
{
15
    /**
16
     * @var \crocodicstudio\crudbooster\CBCoreModule\CbUsersRepo
17
     */
18
    private $usersRepo;
19
20
    /**
21
     * AuthController constructor.
22
     *
23
     * @param \crocodicstudio\crudbooster\CBCoreModule\CbUsersRepo $usersRepo
24
     */
25
    public function __construct(CbUsersRepo $usersRepo)
26
    {
27
        $this->usersRepo = $usersRepo;
28
    }
29
30
    /**
31
     * @param string $tableName
32
     * @return mixed
33
     */
34
    public function table($tableName = null)
35
    {
36
        $tableName = $tableName ?: $this->table;
0 ignored issues
show
Bug Best Practice introduced by
The property table does not exist on crocodicstudio\crudboost...thModule\AuthController. Did you maybe forget to declare it?
Loading history...
37
        return \DB::table($tableName);
38
    }
39
40
    public function getLockscreen()
41
    {
42
        if (! CRUDBooster::myId()) {
43
            Session::flush();
44
            return redirect()->route('getLogin')->with('message', cbTrans('alert_session_expired'));
45
        }
46
47
        Session::put('admin_lock', 1);
48
        return view('CbAuth::lockscreen');
49
    }
50
51
    public function postUnlockScreen()
52
    {
53
        $user = $this->usersRepo->find(CRUDBooster::myId());
54
55
        if (\Hash::check(request('password'), $user->password)) {
0 ignored issues
show
Bug introduced by
It seems like request('password') can also be of type array; however, parameter $value of Illuminate\Support\Facades\Hash::check() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
        if (\Hash::check(/** @scrutinizer ignore-type */ request('password'), $user->password)) {
Loading history...
56
            Session::put('admin_lock', 0);
57
58
            return redirect()->route('CbDashboard');
59
        }
60
        echo "<script>alert('".cbTrans('alert_password_wrong')."');history.go(-1);</script>";
61
    }
62
63
    public function getLogin()
64
    {
65
        if (CRUDBooster::myId()) {
66
            return redirect(cbAdminPath());
67
        }
68
69
        return view('CbAuth::login');
70
    }
71
72
    public function getForgot()
73
    {
74
        if (CRUDBooster::myId()) {
75
            return redirect()->route('CbDashboard');
76
        }
77
78
        return view('CbAuth::forgot');
79
    }
80
81
    public function postForgot()
82
    {
83
        $this->validateForgotPass();
84
85
        $randString = str_random(5);
86
        $this->usersRepo->updateByMail(request('email'), ['password' => \Hash::make($randString)]);
87
88
        //$appname = cbGetsetting('appname');
89
        $user = $this->usersRepo->findByMail(request('email'));
90
        $user->password = $randString;
91
        (new Mailer())->send(['to' => $user->email, 'data' => $user, 'template' => 'forgot_password_backend']);
92
93
        CRUDBooster::insertLog(trans('crudbooster_logging.log_forgot', ['email' => request('email'), 'ip' => Request::server('REMOTE_ADDR')]));
94
95
        return redirect()->route('getLogin')->with('message', cbTrans('message_forgot_password'));
96
    }
97
98
    public function getLogout()
99
    {
100
        CRUDBooster::insertLog(trans('crudbooster_logging.log_logout', ['email' => CRUDBooster::me()->email]));
101
        Session::flush();
102
103
        return redirect()->route('getLogin')->with('message', cbTrans('message_after_logout'));
104
    }
105
106
    private function validateForgotPass()
107
    {
108
        $validator = Validator::make(request()->all(), ['email' => 'required|email|exists:cms_users',]);
109
110
        if ($validator->fails()) {
111
            $message = $validator->errors()->all();
112
            backWithMsg(implode(', ', $message), 'danger');
113
        }
114
    }
115
}
116