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")); |
|
|
|
|
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
|
|
|
|