66use Illuminate \Contracts \Mail \Mailer as MailerContract ;
77use Illuminate \Database \Schema \Builder ;
88use Illuminate \Support \Str ;
9+ use Illuminate \Support \Facades \Crypt ;
10+ use Illuminate \Support \Facades \DB ;
911use Jrean \UserVerification \Exceptions \VerificationException ;
1012
1113class UserVerification
@@ -27,8 +29,8 @@ class UserVerification
2729 /**
2830 * Constructor.
2931 *
30- * @param \Illuminate\Contracts\Mail\Mailer $mailer
31- * @param \Illuminate\Database\Schema\Builder $schema
32+ * @param \Illuminate\Contracts\Mail\Mailer $mailer
33+ * @param \Illuminate\Database\Schema\Builder $schema
3234 * @return void
3335 */
3436 public function __construct (MailerContract $ mailer , Builder $ schema )
@@ -39,74 +41,104 @@ public function __construct(MailerContract $mailer, Builder $schema)
3941 }
4042
4143 /**
42- * Generate and save a verification token the given user .
44+ * Generate and save a verification token.
4345 *
44- * @param \Illuminate\Contracts\Auth\Authenticatable $model
46+ * @param \Illuminate\Contracts\Auth\Authenticatable $user
4547 * @return bool
4648 */
47- public function generate (AuthenticatableContract $ model )
49+ public function generate (AuthenticatableContract $ user )
4850 {
49- return $ this ->saveToken ($ model , $ this ->generateToken ());
51+ return $ this ->saveToken ($ user , $ this ->generateToken ($ user -> email ));
5052 }
5153
5254 /**
5355 * Send by email a link containing the verification token.
5456 *
55- * @param \Illuminate\Contracts\Auth\Authenticatable $model
57+ * @param \Illuminate\Contracts\Auth\Authenticatable $user
5658 * @param string $subject
5759 * @return bool
5860 */
59- public function send (AuthenticatableContract $ model , $ subject = null )
61+ public function send (AuthenticatableContract $ user , $ subject = null )
6062 {
61- if (! $ this ->isCompliant ($ model )) {
63+ if (! $ this ->isCompliant ($ user )) {
6264 throw new VerificationException ();
6365 }
6466
65- return (boolean ) $ this ->emailVerificationLink ($ model , $ subject );
67+ return (boolean ) $ this ->emailVerificationLink ($ user , $ subject );
68+ }
69+
70+ /**
71+ * Generate, save and send by email a link containing the verification token.
72+ *
73+ * @param \Illuminate\Contracts\Auth\Authenticatable $user
74+ * @param string $subject
75+ * @return bool
76+ */
77+ public function generateAndSend (AuthenticatableContract $ user , $ subject = null )
78+ {
79+ $ token = $ this ->generate ($ user );
80+
81+ return $ this ->send ($ user , $ subject );
6682 }
6783
6884 /**
6985 * Process the token verification.
7086 *
71- * @param \Illuminate\Contracts\Auth\Authenticatable $model
87+ * @param stdClass $user
7288 * @param string $token
7389 * @return bool
7490 */
75- public function process (AuthenticatableContract $ model , $ token )
91+ public function process ($ user , $ token )
7692 {
77- if (! $ this ->compareToken ($ model ->verification_token , $ token )) {
93+ if (! $ this ->compareToken ($ user ->verification_token , $ token )) {
7894 return false ;
7995 }
8096
81- $ this ->wasVerified ($ model );
97+ $ this ->wasVerified ($ user );
8298
8399 return true ;
84100 }
85101
86102 /**
87103 * Check if the user is verified.
88104 *
89- * @param \Illuminate\Contracts\Auth\Authenticatable $model
105+ * @param stdClass $user
90106 * @return bool
91107 */
92- public function isVerified (AuthenticatableContract $ model )
108+ public function isVerified ($ user )
93109 {
94- return $ model ->verified == true ;
110+ return $ user ->verified == true ;
95111 }
96112
97113 /**
98- * Update and save the model instance has verified.
114+ * Update and save the user has verified.
99115 *
100- * @param AuthenticatableContract $model
116+ * @param stdClass $user
101117 * @return void
102118 */
103- protected function wasVerified (AuthenticatableContract $ model )
119+ protected function wasVerified ($ user )
104120 {
105- $ model ->verification_token = null ;
121+ $ user ->verification_token = null ;
106122
107- $ model ->verified = true ;
123+ $ user ->verified = true ;
108124
109- $ model ->save ();
125+ $ this ->updateUser ($ user );
126+ }
127+
128+ /**
129+ * Update and save user object.
130+ *
131+ * @param stdClass $user
132+ * @return void
133+ */
134+ protected function updateUser ($ user )
135+ {
136+ DB ::table ($ user ->table )
137+ ->where ('email ' , $ user ->email )
138+ ->update ([
139+ 'verification_token ' => $ user ->verification_token ,
140+ 'verified ' => $ user ->verified
141+ ]);
110142 }
111143
112144 /**
@@ -124,14 +156,14 @@ protected function compareToken($storedToken, $requestToken)
124156 /**
125157 * Prepare and send the email with the verification token link.
126158 *
127- * @param \Illuminate\Contracts\Auth\Authenticatable $model
159+ * @param \Illuminate\Contracts\Auth\Authenticatable $user
128160 * @param string $subject
129161 * @return mixed
130162 */
131- protected function emailVerificationLink (AuthenticatableContract $ model , $ subject )
163+ protected function emailVerificationLink (AuthenticatableContract $ user , $ subject )
132164 {
133- return $ this ->mailer ->send ('emails.user-verification ' , compact ('model ' ), function ($ m ) use ($ model , $ subject ) {
134- $ m ->to ($ model ->email );
165+ return $ this ->mailer ->send ('emails.user-verification ' , compact ('user ' ), function ($ m ) use ($ user , $ subject ) {
166+ $ m ->to ($ user ->email );
135167
136168 $ m ->subject (is_null ($ subject ) ? 'Your Account Verification Link ' : $ subject );
137169 });
@@ -140,45 +172,57 @@ protected function emailVerificationLink(AuthenticatableContract $model, $subjec
140172 /**
141173 * Generate the verification token.
142174 *
175+ * @param string $email
176+ * @return string
177+ */
178+ protected function generateToken ($ email )
179+ {
180+ return Crypt::encrypt ($ email );
181+ }
182+
183+ /**
184+ * Decrypt the token to get the email.
185+ *
186+ * @param string $token
143187 * @return string
144188 */
145- protected function generateToken ( )
189+ public function decryptEmailFromToken ( $ token )
146190 {
147- return hash_hmac ( ' sha256 ' , Str:: random ( 40 ), config ( ' app.key ' ) );
191+ return Crypt:: decrypt ( $ token );
148192 }
149193
150194 /**
151195 * Update and save the model instance with the verification token.
152196 *
153- * @param \Illuminate\Contracts\Auth\Authenticatable $model
197+ * @param \Illuminate\Contracts\Auth\Authenticatable $user
154198 * @param string $token
155199 * @return bool
156200 *
157201 * @throws \Jrean\UserVerification\Exceptions\VerificationException
158202 */
159- protected function saveToken (AuthenticatableContract $ model , $ token )
203+ protected function saveToken (AuthenticatableContract $ user , $ token )
160204 {
161- if (! $ this ->isCompliant ($ model )) {
205+ if (! $ this ->isCompliant ($ user )) {
162206 throw new VerificationException ();
163207 }
164208
165- $ model ->verification_token = $ token ;
209+ $ user ->verification_token = $ token ;
166210
167- return $ model ->save ();
211+ return $ user ->save ();
168212 }
169213
170214 /**
171215 * Determine if the given model table has the verified and verification_token
172216 * columns.
173217 *
174- * @param \Illuminate\Contracts\Auth\Authenticatable $model
218+ * @param \Illuminate\Contracts\Auth\Authenticatable $user
175219 * @return bool
176220 */
177- protected function isCompliant (AuthenticatableContract $ model )
221+ protected function isCompliant (AuthenticatableContract $ user )
178222 {
179223 if (
180- $ this ->hasColumn ($ model , 'verified ' )
181- && $ this ->hasColumn ($ model , 'verification_token ' )
224+ $ this ->hasColumn ($ user , 'verified ' )
225+ && $ this ->hasColumn ($ user , 'verification_token ' )
182226 ) {
183227 return true ;
184228 }
@@ -189,12 +233,52 @@ protected function isCompliant(AuthenticatableContract $model)
189233 /**
190234 * Check if the given model talbe has the given column.
191235 *
192- * @param \Illuminate\Contracts\Auth\Authenticatable $model
236+ * @param \Illuminate\Contracts\Auth\Authenticatable $user
193237 * @param string $column
194238 * @return bool
195239 */
196- protected function hasColumn (AuthenticatableContract $ model , $ column )
240+ protected function hasColumn (AuthenticatableContract $ user , $ column )
197241 {
198- return $ this ->schema ->hasColumn ($ model ->getTable (), $ column );
242+ return $ this ->schema ->hasColumn ($ user ->getTable (), $ column );
199243 }
244+
245+ /**
246+ * Get user object.
247+ *
248+ * @param string $token
249+ * @param string $table
250+ * @return stdClass
251+ */
252+ public function getUser ($ token , $ table )
253+ {
254+ return $ this ->getUserByEmail ($ this ->getEmail ($ token ), $ table );
255+ }
256+
257+ /**
258+ * Fetch the user by email.
259+ *
260+ * @param string $email
261+ * @param string $table
262+ * @return stdClass
263+ */
264+ protected function getUserByEmail ($ email , $ table )
265+ {
266+ $ user = DB ::table ($ table )->where ('email ' , $ email )->first ();
267+
268+ $ user ->table = $ table ;
269+
270+ return $ user ;
271+ }
272+
273+ /**
274+ * Decrypt token and extract email.
275+ *
276+ * @param string $token
277+ * @return string
278+ */
279+ protected function getEmail ($ token )
280+ {
281+ return $ this ->decryptEmailFromToken ($ token );
282+ }
283+
200284}
0 commit comments