protonemedia /
laravel-verify-new-email
| 1 | <?php |
||
| 2 | |||
| 3 | namespace ProtoneMedia\LaravelVerifyNewEmail; |
||
| 4 | |||
| 5 | use Illuminate\Auth\Events\Verified; |
||
| 6 | use Illuminate\Database\Eloquent\Model; |
||
| 7 | use Illuminate\Database\Eloquent\Relations\MorphTo; |
||
| 8 | use Illuminate\Support\Facades\URL; |
||
| 9 | use Illuminate\Support\Traits\Tappable; |
||
| 10 | |||
| 11 | class PendingUserEmail extends Model |
||
| 12 | { |
||
| 13 | use Tappable; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * This model won't be updated. |
||
| 17 | */ |
||
| 18 | const UPDATED_AT = null; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | protected $guarded = []; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * User relationship |
||
| 27 | * |
||
| 28 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo |
||
| 29 | */ |
||
| 30 | public function user(): MorphTo |
||
| 31 | { |
||
| 32 | return $this->morphTo('user'); |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Scope for the user. |
||
| 37 | * |
||
| 38 | * @param $query |
||
| 39 | * @param \Illuminate\Database\Eloquent\Model $user |
||
| 40 | * @return void |
||
| 41 | */ |
||
| 42 | public function scopeForUser($query, Model $user) |
||
| 43 | { |
||
| 44 | $query->where([ |
||
| 45 | $this->qualifyColumn('user_type') => get_class($user), |
||
| 46 | $this->qualifyColumn('user_id') => $user->getKey(), |
||
| 47 | ]); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Updates the associated user and removes all pending models with this email. |
||
| 52 | * |
||
| 53 | * @return void |
||
| 54 | */ |
||
| 55 | public function activate() |
||
| 56 | { |
||
| 57 | $user = $this->user; |
||
|
0 ignored issues
–
show
|
|||
| 58 | |||
| 59 | $dispatchEvent = !$user->hasVerifiedEmail() || $user->email !== $this->email; |
||
| 60 | |||
| 61 | $user->email = $this->email; |
||
| 62 | $user->save(); |
||
| 63 | $user->markEmailAsVerified(); |
||
| 64 | |||
| 65 | static::whereEmail($this->email)->get()->each->delete(); |
||
| 66 | |||
| 67 | $dispatchEvent ? event(new Verified($user)) : null; |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Creates a temporary signed URL to verify the pending email. |
||
| 72 | * |
||
| 73 | * @return string |
||
| 74 | */ |
||
| 75 | public function verificationUrl(): string |
||
| 76 | { |
||
| 77 | return URL::temporarySignedRoute( |
||
| 78 | config('verify-new-email.route') ?: 'pendingEmail.verify', |
||
| 79 | now()->addMinutes(config('auth.verification.expire', 60)), |
||
| 80 | ['token' => $this->token] |
||
|
0 ignored issues
–
show
|
|||
| 81 | ); |
||
| 82 | } |
||
| 83 | } |
||
| 84 |
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.