Passed
Push — master ( c14ede...c50061 )
by Ferry
03:59
created

AdminAuthController::getLogoutDeveloper()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php namespace crocodicstudio\crudbooster\controllers;
2
3
use crocodicstudio\crudbooster\exceptions\CBValidationException;
4
use Illuminate\Support\Facades\Cache;
5
use Illuminate\Support\Facades\Session;
6
7
class AdminAuthController extends CBController
8
{
9
    use DeveloperAuthController;
10
11
    private function incrementFailedLogin()
12
    {
13
        $key = md5(request()->ip().request()->userAgent());
14
        Cache::increment("loginFailed".$key, 1);
15
    }
16
17
    private function isSuspendedLogin()
18
    {
19
        $key = md5(request()->ip().request()->userAgent());
20
21
        if(Cache::has("loginSuspended".$key)) {
22
            return true;
23
        }
24
25
        if(env("CB_AUTO_SUSPEND_LOGIN") && Cache::get("loginFailed".$key) >= env("CB_AUTO_SUSPEND_LOGIN")) {
26
            Cache::put("loginSuspended".$key, true, 30);
27
            Cache::forget("loginFailed".$key);
28
            return true;
29
        }
30
31
        return false;
32
    }
33
34
    public function getLogin()
35
    {
36
        if(!auth()->guest()) return redirect(cb()->getAdminUrl());
37
38
        cbHook()->hookGetLogin();
39
40
        return view(cbConfig('LOGIN_FORM_VIEW'));
41
    }
42
43
    public function postLogin()
44
    {
45
        try{
46
            if($this->isSuspendedLogin()) throw new CBValidationException(cbLang("you_have_been_suspended"));
0 ignored issues
show
Bug introduced by
It seems like cbLang('you_have_been_suspended') can also be of type array; however, parameter $message of crocodicstudio\crudboost...xception::__construct() 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

46
            if($this->isSuspendedLogin()) throw new CBValidationException(/** @scrutinizer ignore-type */ cbLang("you_have_been_suspended"));
Loading history...
47
48
            cb()->validation([
49
                'email'=>'required|email',
50
                'password'=>'required'
51
            ]);
52
53
            $credential = request()->only(['email','password']);
54
            if (auth()->attempt($credential)) {
55
                cbHook()->hookPostLogin();
56
                return redirect(cb()->getAdminUrl());
57
            } else {
58
                $this->incrementFailedLogin();
59
                return redirect(cb()->getLoginUrl())->with(['message'=>cbLang('password_and_username_is_wrong'),'message_type'=>'warning']);
60
            }
61
        }catch (CBValidationException $e) {
62
            return cb()->redirect(cb()->getAdminUrl("login"),$e->getMessage(),'warning');
63
        }
64
    }
65
66
    public function getLogout()
67
    {
68
        auth()->logout();
69
        return cb()->redirect(cb()->getAdminUrl("login"), cbLang('you_have_been_logged_out'), 'success');
70
    }
71
72
73
}
74