|
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->getEmailForVerification() === $email && $this->hasVerifiedEmail()) { |
|
|
|
|
|
|
24
|
|
|
return null; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
return $this->createPendingUserEmailModel($email)->tap(function (PendingUserEmail $model) { |
|
28
|
|
|
$this->sendPendingEmailVerificationMail($model); |
|
29
|
|
|
}); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Createsa new PendingUserModel model for the given email. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $email |
|
36
|
|
|
* @return \ProtoneMedia\LaravelVerifyNewEmail\PendingUserEmail |
|
37
|
|
|
*/ |
|
38
|
|
|
public function createPendingUserEmailModel(string $email): PendingUserEmail |
|
39
|
|
|
{ |
|
40
|
|
|
return PendingUserEmail::create([ |
|
41
|
|
|
'user_type' => get_class($this), |
|
42
|
|
|
'user_id' => $this->getKey(), |
|
|
|
|
|
|
43
|
|
|
'email' => $email, |
|
44
|
|
|
'token' => Password::broker()->getRepository()->createNewToken(), |
|
45
|
|
|
]); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Returns the pending email address. |
|
50
|
|
|
* |
|
51
|
|
|
* @return string|null |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getPendingEmail():?string |
|
54
|
|
|
{ |
|
55
|
|
|
return PendingUserEmail::forUser($this)->value('email'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Deletes the pending email address models for this user. |
|
60
|
|
|
* |
|
61
|
|
|
* @return void |
|
62
|
|
|
*/ |
|
63
|
|
|
public function clearPendingEmail() |
|
64
|
|
|
{ |
|
65
|
|
|
PendingUserEmail::forUser($this)->get()->each->delete(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Sends the VerifyNewEmail Mailable to the new email address. |
|
70
|
|
|
* |
|
71
|
|
|
* @param \ProtoneMedia\LaravelVerifyNewEmail\PendingUserEmail $pendingUserEmail |
|
72
|
|
|
* @return mixed |
|
73
|
|
|
*/ |
|
74
|
|
|
public function sendPendingEmailVerificationMail(PendingUserEmail $pendingUserEmail) |
|
75
|
|
|
{ |
|
76
|
|
|
$mailableClass = config('verify-new-email.mailable_for_first_verification'); |
|
77
|
|
|
|
|
78
|
|
|
if ($pendingUserEmail->User->hasVerifiedEmail()) { |
|
79
|
|
|
$mailableClass = config('verify-new-email.mailable_for_new_email'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$mailable = new $mailableClass($pendingUserEmail); |
|
83
|
|
|
|
|
84
|
|
|
return Mail::to($pendingUserEmail->email)->send($mailable); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Grabs the pending user email address, generates a new token and sends the Mailable. |
|
89
|
|
|
* |
|
90
|
|
|
* @return \ProtoneMedia\LaravelVerifyNewEmail\PendingUserEmail|null |
|
91
|
|
|
*/ |
|
92
|
|
|
public function resendPendingEmailVerificationMail():?PendingUserEmail |
|
93
|
|
|
{ |
|
94
|
|
|
$pendingUserEmail = PendingUserEmail::forUser($this)->firstOrFail(); |
|
95
|
|
|
|
|
96
|
|
|
return $this->newEmail($pendingUserEmail->email); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.