1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: User |
5
|
|
|
* Date: 8/14/2019 |
6
|
|
|
* Time: 8:45 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace crocodicstudio\crudbooster\controllers; |
10
|
|
|
|
11
|
|
|
use crocodicstudio\crudbooster\exceptions\CBValidationException; |
12
|
|
|
use crocodicstudio\crudbooster\helpers\MailHelper; |
13
|
|
|
use Illuminate\Support\Facades\Cache; |
14
|
|
|
use Illuminate\Support\Facades\DB; |
15
|
|
|
use Illuminate\Support\Facades\Hash; |
16
|
|
|
use Illuminate\Support\Facades\Log; |
17
|
|
|
use Illuminate\Support\Facades\Session; |
18
|
|
|
use Illuminate\Support\Str; |
19
|
|
|
|
20
|
|
|
trait ForgetController |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
public function postForget() { |
24
|
|
|
try { |
25
|
|
|
cb()->validation(['email'=>'required|email']); |
26
|
|
|
|
27
|
|
|
if($user = cb()->find("users",["email"=>request("email")])) { |
28
|
|
|
$token = Str::random(6); |
29
|
|
|
$linkToken = cb()->getAdminUrl("continue-reset/".$token); |
30
|
|
|
Cache::put("forget_".$token, $user->id, now()->addHours(12)); |
31
|
|
|
|
32
|
|
|
Log::info("Someone trying reset password:\n |
33
|
|
|
Time: ".date("Y-m-d H:i:s")."\n |
34
|
|
|
IP: ".request()->ip()."\n |
35
|
|
|
User Agent: ".request()->userAgent()."\n |
36
|
|
|
Email Entered: ".request("email")); |
|
|
|
|
37
|
|
|
|
38
|
|
|
$mail = new MailHelper(); |
39
|
|
|
$mail->to($user->email); |
40
|
|
|
$mail->sender(getSetting("forget_email_sender","noreply@".$_SERVER['SERVER_NAME']),cb()->getAppName()); |
41
|
|
|
$mail->content("Please Confirm Your Forgot Password"," |
42
|
|
|
Hi $user->name,<br/><br> |
43
|
|
|
Someone with the detail bellow: <br/> |
44
|
|
|
Time = ".now()->format("Y-m-d H:i:s")."<br/> |
45
|
|
|
IP Address = ".request()->ip()."<br/> |
46
|
|
|
Browser = ".request()->userAgent()."<br/> |
47
|
|
|
<p> |
48
|
|
|
Trying to reset your password. If this is you, please click the following url to continue: <br> |
49
|
|
|
<a href='$linkToken' target='_blank'>$linkToken</a> |
50
|
|
|
</p> |
51
|
|
|
<br><br> |
52
|
|
|
Thank you <br/> |
53
|
|
|
".cb()->getAppName()." |
54
|
|
|
"); |
55
|
|
|
$mail->send(); |
56
|
|
|
|
57
|
|
|
} else { |
58
|
|
|
return cb()->redirectBack("Your email is not registered"); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
}catch (CBValidationException $e) { |
62
|
|
|
return cb()->redirectBack($e->getMessage()); |
63
|
|
|
} catch (\Exception $e) { |
64
|
|
|
Log::error($e); |
65
|
|
|
return cb()->redirectBack(cbLang("something_went_wrong")); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return cb()->redirectBack("We've sent you an email instruction. Please follow the instruction inside the email","success"); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getContinueReset($token) { |
72
|
|
|
if(Cache::has("forget_".$token)) { |
73
|
|
|
$id = Cache::get("forget_".$token); |
74
|
|
|
$newPassword = Str::random(6); |
75
|
|
|
cb()->update("users", $id, ["password"=>Hash::make($newPassword)]); |
76
|
|
|
|
77
|
|
|
$user = cb()->find("users",$id); |
78
|
|
|
|
79
|
|
|
$mail = new MailHelper(); |
80
|
|
|
$mail->to($user->email); |
81
|
|
|
$mail->sender(getSetting("forget_email_sender","noreply@".$_SERVER['SERVER_NAME']),cb()->getAppName()); |
82
|
|
|
$mail->content("This Is Your New Password"," |
83
|
|
|
Hi $user->name,<br/><br> |
84
|
|
|
Thank you for confirming the request new password. Here is your new password: <br> |
85
|
|
|
<h2>$newPassword</h2> |
86
|
|
|
|
87
|
|
|
<br><br> |
88
|
|
|
Thank you <br/> |
89
|
|
|
".cb()->getAppName()." |
90
|
|
|
"); |
91
|
|
|
$mail->send(); |
92
|
|
|
|
93
|
|
|
return cb()->redirect(cb()->getAdminUrl("login"),"We've sent you new password email. Please check at your mail inbox or spambox","success"); |
94
|
|
|
} else { |
95
|
|
|
return cb()->redirect(cb()->getAdminUrl("login"),"It looks like the url has been expired!"); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
} |