1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* NOTICE OF LICENSE |
5
|
|
|
* |
6
|
|
|
* Part of the Rinvex Fort Package. |
7
|
|
|
* |
8
|
|
|
* This source file is subject to The MIT License (MIT) |
9
|
|
|
* that is bundled with this package in the LICENSE file. |
10
|
|
|
* |
11
|
|
|
* Package: Rinvex Fort Package |
12
|
|
|
* License: The MIT License (MIT) |
13
|
|
|
* Link: https://rinvex.com |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
declare(strict_types=1); |
17
|
|
|
|
18
|
|
|
namespace Rinvex\Fort\Services; |
19
|
|
|
|
20
|
|
|
use Closure; |
21
|
|
|
use Illuminate\Support\Arr; |
22
|
|
|
use UnexpectedValueException; |
23
|
|
|
use Illuminate\Contracts\Auth\UserProvider; |
24
|
|
|
use Rinvex\Fort\Contracts\CanVerifyEmailContract; |
25
|
|
|
use Rinvex\Fort\Contracts\EmailVerificationBrokerContract; |
26
|
|
|
use Rinvex\Fort\Contracts\EmailVerificationTokenRepositoryContract; |
27
|
|
|
|
28
|
|
|
class EmailVerificationBroker implements EmailVerificationBrokerContract |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* The verification token repository. |
32
|
|
|
* |
33
|
|
|
* @var \Rinvex\Fort\Contracts\EmailVerificationTokenRepositoryContract |
34
|
|
|
*/ |
35
|
|
|
protected $tokens; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The user provider implementation. |
39
|
|
|
* |
40
|
|
|
* @var \Illuminate\Contracts\Auth\UserProvider |
41
|
|
|
*/ |
42
|
|
|
protected $users; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Create a new verification broker instance. |
46
|
|
|
* |
47
|
|
|
* @param \Rinvex\Fort\Contracts\EmailVerificationTokenRepositoryContract $tokens |
48
|
|
|
* @param \Illuminate\Contracts\Auth\UserProvider $users |
49
|
|
|
*/ |
50
|
|
|
public function __construct(EmailVerificationTokenRepositoryContract $tokens, UserProvider $users) |
51
|
|
|
{ |
52
|
|
|
$this->users = $users; |
53
|
|
|
$this->tokens = $tokens; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function sendVerificationLink(array $credentials) |
60
|
|
|
{ |
61
|
|
|
// First we will check to see if we found a user at the given credentials and |
62
|
|
|
// if we did not we will redirect back to this current URI with a piece of |
63
|
|
|
// "flash" data in the session to indicate to the developers the errors. |
64
|
|
|
if (is_null($user = $this->getUser($credentials))) { |
65
|
|
|
return static::INVALID_USER; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// Once we have the verification token, we are ready to send the message out |
69
|
|
|
// to this user with a link for verification. We will then redirect back to |
70
|
|
|
// the current URI having nothing set in the session to indicate errors. |
71
|
|
|
$data = $this->tokens->getData($user, $token = $this->tokens->create($user)); |
|
|
|
|
72
|
|
|
$expiration = $this->tokens->getExpiration(); |
73
|
|
|
|
74
|
|
|
// Returned token is hashed, and we need the |
75
|
|
|
// public token to be sent to the user |
76
|
|
|
$data['token'] = $token; |
77
|
|
|
|
78
|
|
|
$user->sendEmailVerificationNotification($data, $expiration); |
79
|
|
|
|
80
|
|
|
return static::LINK_SENT; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public function verify(array $credentials, Closure $callback) |
87
|
|
|
{ |
88
|
|
|
// If the responses from the validate method is not a user instance, we will |
89
|
|
|
// assume that it is a redirect and simply return it from this method and |
90
|
|
|
// the user is properly redirected having an error message on the post. |
91
|
|
|
$user = $this->validateVerification($credentials); |
92
|
|
|
|
93
|
|
|
if (! $user instanceof CanVerifyEmailContract) { |
94
|
|
|
return $user; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// Fire the email verification start event |
98
|
|
|
event('rinvex.fort.emailverification.start', [$user]); |
99
|
|
|
|
100
|
|
|
// Once the email has been verified, we'll call the given |
101
|
|
|
// callback, then we'll delete the token and return. |
102
|
|
|
$callback($user); |
103
|
|
|
|
104
|
|
|
$this->tokens->delete($user); |
105
|
|
|
|
106
|
|
|
// Fire the email verification success event |
107
|
|
|
event('rinvex.fort.emailverification.success', [$user]); |
108
|
|
|
|
109
|
|
|
return static::EMAIL_VERIFIED; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get the user for the given credentials. |
114
|
|
|
* |
115
|
|
|
* @param array $credentials |
116
|
|
|
* |
117
|
|
|
* @throws \UnexpectedValueException |
118
|
|
|
* |
119
|
|
|
* @return \Rinvex\Fort\Contracts\CanVerifyEmailContract |
|
|
|
|
120
|
|
|
*/ |
121
|
|
|
public function getUser(array $credentials) |
122
|
|
|
{ |
123
|
|
|
$credentials = Arr::except($credentials, ['token']); |
124
|
|
|
|
125
|
|
|
$user = $this->users->retrieveByCredentials($credentials); |
126
|
|
|
|
127
|
|
|
if ($user && ! $user instanceof CanVerifyEmailContract) { |
128
|
|
|
throw new UnexpectedValueException('User must implement CanVerifyEmailContract interface.'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $user; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Create a new email verification token for the given user. |
136
|
|
|
* |
137
|
|
|
* @param \Rinvex\Fort\Contracts\CanVerifyEmailContract $user |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
public function createToken(CanVerifyEmailContract $user) |
142
|
|
|
{ |
143
|
|
|
return $this->tokens->create($user); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Delete email verification tokens of the given user. |
148
|
|
|
* |
149
|
|
|
* @param \Rinvex\Fort\Contracts\CanVerifyEmailContract $user |
150
|
|
|
* |
151
|
|
|
* @return void |
152
|
|
|
*/ |
153
|
|
|
public function deleteToken(CanVerifyEmailContract $user) |
154
|
|
|
{ |
155
|
|
|
$this->tokens->delete($user); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Validate the given verification token. |
160
|
|
|
* |
161
|
|
|
* @param \Rinvex\Fort\Contracts\CanVerifyEmailContract $user |
162
|
|
|
* @param string $token |
163
|
|
|
* |
164
|
|
|
* @return bool |
165
|
|
|
*/ |
166
|
|
|
public function tokenExists(CanVerifyEmailContract $user, $token) |
167
|
|
|
{ |
168
|
|
|
return $this->tokens->exists($user, $token); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Get the verification token repository implementation. |
173
|
|
|
* |
174
|
|
|
* @return \Rinvex\Fort\Contracts\EmailVerificationTokenRepositoryContract |
175
|
|
|
*/ |
176
|
|
|
public function getRepository() |
177
|
|
|
{ |
178
|
|
|
return $this->tokens; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Validate an email verification for the given credentials. |
183
|
|
|
* |
184
|
|
|
* @param array $credentials |
185
|
|
|
* |
186
|
|
|
* @return \Rinvex\Fort\Contracts\CanVerifyEmailContract|string |
|
|
|
|
187
|
|
|
*/ |
188
|
|
|
protected function validateVerification(array $credentials) |
189
|
|
|
{ |
190
|
|
|
if (is_null($user = $this->getUser($credentials))) { |
191
|
|
|
return static::INVALID_USER; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
if (! $this->tokens->exists($user, $credentials['token'])) { |
|
|
|
|
195
|
|
|
return static::INVALID_TOKEN; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return $user; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: