1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ProtoneMedia\LaravelVerifyNewEmail; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Mail; |
6
|
|
|
use Illuminate\Support\Facades\Password; |
7
|
|
|
use ProtoneMedia\LaravelVerifyNewEmail\Mail\VerifyNewEmail; |
8
|
|
|
|
9
|
|
|
trait MustVerifyNewEmail |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Deletes all previous attempts for this user, creates a new model/token |
13
|
|
|
* to verify the given email address and send the verification URL |
14
|
|
|
* to the new email address. |
15
|
|
|
* |
16
|
|
|
* @param string $email |
17
|
|
|
* @return \ProtoneMedia\LaravelVerifyNewEmail\PendingUserEmail|null |
18
|
|
|
*/ |
19
|
|
|
public function newEmail(string $email):?PendingUserEmail |
20
|
|
|
{ |
21
|
|
|
$this->clearPendingEmail(); |
22
|
|
|
|
23
|
|
|
if ($this->email === $email) { |
|
|
|
|
24
|
|
|
return null; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
return tap(PendingUserEmail::create([ |
28
|
|
|
'user_type' => get_class($this), |
29
|
|
|
'user_id' => $this->getKey(), |
|
|
|
|
30
|
|
|
'email' => $email, |
31
|
|
|
'token' => Password::broker()->getRepository()->createNewToken(), |
32
|
|
|
]), function ($pendingUserEmail) { |
33
|
|
|
$this->sendPendingEmailVerificationMail($pendingUserEmail); |
34
|
|
|
}); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Returns the pending email address. |
39
|
|
|
* |
40
|
|
|
* @return string|null |
41
|
|
|
*/ |
42
|
|
|
public function getPendingEmail():?string |
43
|
|
|
{ |
44
|
|
|
return PendingUserEmail::forUser($this)->value('email'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Deletes the pending email address models for this user. |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
public function clearPendingEmail() |
53
|
|
|
{ |
54
|
|
|
PendingUserEmail::forUser($this)->get()->each->delete(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Sends the VerifyNewEmail Mailable to the new email address. |
59
|
|
|
* |
60
|
|
|
* @param \ProtoneMedia\LaravelVerifyNewEmail\PendingUserEmail $pendingUserEmail |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
|
|
public function sendPendingEmailVerificationMail(PendingUserEmail $pendingUserEmail) |
64
|
|
|
{ |
65
|
|
|
return Mail::to($pendingUserEmail->email)->send(new VerifyNewEmail($pendingUserEmail)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Grabs the pending user email address, generates a new token and sends the Mailable. |
70
|
|
|
* |
71
|
|
|
* @return \ProtoneMedia\LaravelVerifyNewEmail\PendingUserEmail|null |
72
|
|
|
*/ |
73
|
|
|
public function resendPendingEmailVerificationMail():?PendingUserEmail |
74
|
|
|
{ |
75
|
|
|
$pendingUserEmail = PendingUserEmail::forUser($this)->firstOrFail(); |
76
|
|
|
|
77
|
|
|
return $this->newEmail($pendingUserEmail->email); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: