protonemedia /
laravel-verify-new-email
| 1 | <?php |
||
| 2 | |||
| 3 | namespace ProtoneMedia\LaravelVerifyNewEmail; |
||
| 4 | |||
| 5 | use Illuminate\Database\Eloquent\Model; |
||
| 6 | use Illuminate\Support\Facades\Mail; |
||
| 7 | use Illuminate\Support\Facades\Password; |
||
| 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 \Illuminate\Database\Eloquent\Model|null |
||
| 18 | */ |
||
| 19 | public function newEmail(string $email): ?Model |
||
| 20 | { |
||
| 21 | if ($this->getEmailForVerification() === $email && $this->hasVerifiedEmail()) { |
||
| 22 | return null; |
||
| 23 | } |
||
| 24 | |||
| 25 | return $this->createPendingUserEmailModel($email)->tap(function ($model) { |
||
| 26 | $this->sendPendingEmailVerificationMail($model); |
||
| 27 | }); |
||
| 28 | } |
||
| 29 | |||
| 30 | public function getEmailVerificationModel(): Model |
||
| 31 | { |
||
| 32 | $modelClass = config('verify-new-email.model'); |
||
| 33 | |||
| 34 | if (!$modelClass) { |
||
| 35 | throw new InvalidEmailVerificationModelException; |
||
| 36 | } |
||
| 37 | |||
| 38 | return app($modelClass); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Creates new PendingUserModel model for the given email. |
||
| 43 | * |
||
| 44 | * @param string $email |
||
| 45 | * @return \Illuminate\Database\Eloquent\Model |
||
| 46 | */ |
||
| 47 | public function createPendingUserEmailModel(string $email): Model |
||
| 48 | { |
||
| 49 | $this->clearPendingEmail(); |
||
| 50 | |||
| 51 | return $this->getEmailVerificationModel()->create([ |
||
| 52 | 'user_type' => get_class($this), |
||
| 53 | 'user_id' => $this->getKey(), |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 54 | 'email' => $email, |
||
| 55 | 'token' => Password::broker()->getRepository()->createNewToken(), |
||
| 56 | ]); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Returns the pending email address. |
||
| 61 | * |
||
| 62 | * @return string|null |
||
| 63 | */ |
||
| 64 | public function getPendingEmail(): ?string |
||
| 65 | { |
||
| 66 | return $this->getEmailVerificationModel()->forUser($this)->value('email'); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Deletes the pending email address models for this user. |
||
| 71 | * |
||
| 72 | * @return void |
||
| 73 | */ |
||
| 74 | public function clearPendingEmail() |
||
| 75 | { |
||
| 76 | $this->getEmailVerificationModel()->forUser($this)->get()->each->delete(); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Sends the VerifyNewEmail Mailable to the new email address. |
||
| 81 | * |
||
| 82 | * @param \Illuminate\Database\Eloquent\Model $pendingUserEmail |
||
| 83 | * @return mixed |
||
| 84 | */ |
||
| 85 | public function sendPendingEmailVerificationMail(Model $pendingUserEmail) |
||
| 86 | { |
||
| 87 | $mailableClass = config('verify-new-email.mailable_for_first_verification'); |
||
| 88 | |||
| 89 | if ($pendingUserEmail->User->hasVerifiedEmail()) { |
||
| 90 | $mailableClass = config('verify-new-email.mailable_for_new_email'); |
||
| 91 | } |
||
| 92 | |||
| 93 | $mailable = new $mailableClass($pendingUserEmail); |
||
| 94 | |||
| 95 | return Mail::to($pendingUserEmail->email)->send($mailable); |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Grabs the pending user email address, generates a new token and sends the Mailable. |
||
| 100 | * |
||
| 101 | * @return \Illuminate\Database\Eloquent\Model|null |
||
| 102 | */ |
||
| 103 | public function resendPendingEmailVerificationMail(): ?Model |
||
| 104 | { |
||
| 105 | $pendingUserEmail = $this->getEmailVerificationModel()->forUser($this)->firstOrFail(); |
||
| 106 | |||
| 107 | return $this->newEmail($pendingUserEmail->email); |
||
| 108 | } |
||
| 109 | } |
||
| 110 |