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
|
|
|
* @param string $tableName |
17
|
|
|
* @return mixed |
18
|
|
|
*/ |
19
|
|
|
public function table($tableName = null) |
20
|
|
|
{ |
21
|
|
|
$tableName = $tableName ?: $this->table; |
|
|
|
|
22
|
|
|
return \DB::table($tableName); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function getLockscreen() |
26
|
|
|
{ |
27
|
|
|
if (! CRUDBooster::myId()) { |
28
|
|
|
Session::flush(); |
29
|
|
|
return redirect()->route('getLogin')->with('message', cbTrans('alert_session_expired')); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
Session::put('admin_lock', 1); |
33
|
|
|
return view('CbAuth::lockscreen'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function postUnlockScreen() |
37
|
|
|
{ |
38
|
|
|
$user = CbUsersRepo::find(CRUDBooster::myId()); |
39
|
|
|
|
40
|
|
|
if (\Hash::check(request('password'), $user->password)) { |
|
|
|
|
41
|
|
|
Session::put('admin_lock', 0); |
42
|
|
|
|
43
|
|
|
return redirect()->route('AuthControllerGetIndex'); |
44
|
|
|
} |
45
|
|
|
echo "<script>alert('".cbTrans('alert_password_wrong')."');history.go(-1);</script>"; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getLogin() |
49
|
|
|
{ |
50
|
|
|
if (CRUDBooster::myId()) { |
51
|
|
|
return redirect(cbAdminPath()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return view('CbAuth::login'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getForgot() |
58
|
|
|
{ |
59
|
|
|
if (CRUDBooster::myId()) { |
60
|
|
|
return redirect()->action('\\'.AuthController::class.'@getIndex'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return view('CbAuth::forgot'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function postForgot() |
67
|
|
|
{ |
68
|
|
|
$this->validateForgotPass(); |
69
|
|
|
|
70
|
|
|
$randString = str_random(5); |
71
|
|
|
CbUsersRepo::updateByMail(request('email'), ['password' => \Hash::make($randString)]); |
72
|
|
|
|
73
|
|
|
//$appname = cbGetsetting('appname'); |
74
|
|
|
$user = CbUsersRepo::findByMail(request('email')); |
75
|
|
|
$user->password = $randString; |
76
|
|
|
(new Mailer())->send(['to' => $user->email, 'data' => $user, 'template' => 'forgot_password_backend']); |
77
|
|
|
|
78
|
|
|
CRUDBooster::insertLog(cbTrans('log_forgot', ['email' => request('email'), 'ip' => Request::server('REMOTE_ADDR')])); |
79
|
|
|
|
80
|
|
|
return redirect()->route('getLogin')->with('message', cbTrans('message_forgot_password')); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getLogout() |
84
|
|
|
{ |
85
|
|
|
CRUDBooster::insertLog(trans('crudbooster_logging.log_logout', ['email' => CRUDBooster::me()->email])); |
86
|
|
|
Session::flush(); |
87
|
|
|
|
88
|
|
|
return redirect()->route('getLogin')->with('message', cbTrans('message_after_logout')); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function validateForgotPass() |
92
|
|
|
{ |
93
|
|
|
$validator = Validator::make(request()->all(), ['email' => 'required|email|exists:cms_users',]); |
94
|
|
|
|
95
|
|
|
if ($validator->fails()) { |
96
|
|
|
$message = $validator->errors()->all(); |
97
|
|
|
backWithMsg(implode(', ', $message), 'danger'); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|