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