Passed
Push — master ( 7a263d...e0b6af )
by Ferry
05:04
created

ForgetController::postForget()   A

Complexity

Conditions 4
Paths 26

Size

Total Lines 46
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 30
c 1
b 0
f 0
nc 26
nop 0
dl 0
loc 46
rs 9.44
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"));
0 ignored issues
show
Bug introduced by
Are you sure request('email') of type Illuminate\Http\Request|array|string can be used in concatenation? ( Ignorable by Annotation )

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

36
                        Email Entered: "./** @scrutinizer ignore-type */ request("email"));
Loading history...
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
}