@@ -83,8 +83,12 @@ public function issueChallenge(Authenticatable $user, string $method): ?MfaChall
8383 $ code = str_pad ((string ) random_int (0 , (10 ** $ codeLength ) - 1 ), $ codeLength , '0 ' , STR_PAD_LEFT );
8484
8585 $ challenge = new MfaChallenge ();
86- $ challenge ->user_type = get_class ($ user );
87- $ challenge ->user_id = $ user ->getAuthIdentifier ();
86+ $ morph = $ this ->config ['morph ' ] ?? [];
87+ $ morphName = $ morph ['name ' ] ?? 'model ' ;
88+ $ typeColumn = $ morphName . '_type ' ;
89+ $ idColumn = $ morphName . '_id ' ;
90+ $ challenge ->setAttribute ($ typeColumn , get_class ($ user ));
91+ $ challenge ->setAttribute ($ idColumn , $ user ->getAuthIdentifier ());
8892 $ challenge ->method = $ method ;
8993 $ challenge ->code = $ code ;
9094 $ challenge ->expires_at = Carbon::now ()->addSeconds ($ ttlSeconds );
@@ -120,9 +124,13 @@ public function verifyChallenge(Authenticatable $user, string $method, string $c
120124 {
121125 $ now = Carbon::now ();
122126
127+ $ morph = $ this ->config ['morph ' ] ?? [];
128+ $ morphName = $ morph ['name ' ] ?? 'model ' ;
129+ $ typeColumn = $ morphName . '_type ' ;
130+ $ idColumn = $ morphName . '_id ' ;
123131 $ challenge = MfaChallenge::query ()
124- ->where (' user_type ' , get_class ($ user ))
125- ->where (' user_id ' , $ user ->getAuthIdentifier ())
132+ ->where ($ typeColumn , get_class ($ user ))
133+ ->where ($ idColumn , $ user ->getAuthIdentifier ())
126134 ->where ('method ' , strtolower ($ method ))
127135 ->whereNull ('consumed_at ' )
128136 ->where ('expires_at ' , '> ' , $ now )
@@ -170,9 +178,13 @@ public function shouldSkipVerification(Authenticatable $user, ?string $token): b
170178
171179 $ hash = hash ('sha256 ' , $ token );
172180 $ now = Carbon::now ();
181+ $ morph = $ this ->config ['morph ' ] ?? [];
182+ $ morphName = $ morph ['name ' ] ?? 'model ' ;
183+ $ typeColumn = $ morphName . '_type ' ;
184+ $ idColumn = $ morphName . '_id ' ;
173185 $ record = MfaRememberedDevice::query ()
174- ->where (' user_type ' , get_class ($ user ))
175- ->where (' user_id ' , $ user ->getAuthIdentifier ())
186+ ->where ($ typeColumn , get_class ($ user ))
187+ ->where ($ idColumn , $ user ->getAuthIdentifier ())
176188 ->where ('token_hash ' , $ hash )
177189 ->where ('expires_at ' , '> ' , $ now )
178190 ->first ();
@@ -206,8 +218,12 @@ public function rememberDevice(Authenticatable $user, ?int $lifetimeDays = null,
206218 $ hash = hash ('sha256 ' , $ plainToken );
207219
208220 $ record = new MfaRememberedDevice ();
209- $ record ->user_type = get_class ($ user );
210- $ record ->user_id = $ user ->getAuthIdentifier ();
221+ $ morph = $ this ->config ['morph ' ] ?? [];
222+ $ morphName = $ morph ['name ' ] ?? 'model ' ;
223+ $ typeColumn = $ morphName . '_type ' ;
224+ $ idColumn = $ morphName . '_id ' ;
225+ $ record ->setAttribute ($ typeColumn , get_class ($ user ));
226+ $ record ->setAttribute ($ idColumn , $ user ->getAuthIdentifier ());
211227 $ record ->token_hash = $ hash ;
212228 $ record ->device_name = $ deviceName ;
213229 $ request = app ('request ' );
@@ -243,9 +259,13 @@ public function makeRememberCookie(string $token, ?int $lifetimeDays = null): Co
243259 public function forgetRememberedDevice (Authenticatable $ user , string $ token ): int
244260 {
245261 $ hash = hash ('sha256 ' , $ token );
262+ $ morph = $ this ->config ['morph ' ] ?? [];
263+ $ morphName = $ morph ['name ' ] ?? 'model ' ;
264+ $ typeColumn = $ morphName . '_type ' ;
265+ $ idColumn = $ morphName . '_id ' ;
246266 return MfaRememberedDevice::query ()
247- ->where (' user_type ' , get_class ($ user ))
248- ->where (' user_id ' , $ user ->getAuthIdentifier ())
267+ ->where ($ typeColumn , get_class ($ user ))
268+ ->where ($ idColumn , $ user ->getAuthIdentifier ())
249269 ->where ('token_hash ' , $ hash )
250270 ->delete ();
251271 }
@@ -255,8 +275,12 @@ public function enableMethod(Authenticatable $user, string $method, array $attri
255275 $ record = $ this ->getMethod ($ user , $ method );
256276 if (! $ record ) {
257277 $ record = new MfaMethod ();
258- $ record ->user_type = get_class ($ user );
259- $ record ->user_id = $ user ->getAuthIdentifier ();
278+ $ morph = $ this ->config ['morph ' ] ?? [];
279+ $ morphName = $ morph ['name ' ] ?? 'model ' ;
280+ $ typeColumn = $ morphName . '_type ' ;
281+ $ idColumn = $ morphName . '_id ' ;
282+ $ record ->setAttribute ($ typeColumn , get_class ($ user ));
283+ $ record ->setAttribute ($ idColumn , $ user ->getAuthIdentifier ());
260284 $ record ->method = strtolower ($ method );
261285 }
262286
@@ -288,9 +312,13 @@ public function isEnabled(Authenticatable $user, string $method): bool
288312
289313 public function getMethod (Authenticatable $ user , string $ method ): ?MfaMethod
290314 {
315+ $ morph = $ this ->config ['morph ' ] ?? [];
316+ $ morphName = $ morph ['name ' ] ?? 'model ' ;
317+ $ typeColumn = $ morphName . '_type ' ;
318+ $ idColumn = $ morphName . '_id ' ;
291319 return MfaMethod::query ()
292- ->where (' user_type ' , get_class ($ user ))
293- ->where (' user_id ' , $ user ->getAuthIdentifier ())
320+ ->where ($ typeColumn , get_class ($ user ))
321+ ->where ($ idColumn , $ user ->getAuthIdentifier ())
294322 ->where ('method ' , strtolower ($ method ))
295323 ->first ();
296324 }
@@ -305,9 +333,13 @@ public function generateRecoveryCodes(Authenticatable $user, ?int $count = null,
305333 $ length = $ length ?? (int ) Arr::get ($ this ->config , 'recovery.code_length ' , 10 );
306334
307335 if ($ replaceExisting ) {
336+ $ morph = $ this ->config ['morph ' ] ?? [];
337+ $ morphName = $ morph ['name ' ] ?? 'model ' ;
338+ $ typeColumn = $ morphName . '_type ' ;
339+ $ idColumn = $ morphName . '_id ' ;
308340 MfaRecoveryCode::query ()
309- ->where (' user_type ' , get_class ($ user ))
310- ->where (' user_id ' , $ user ->getAuthIdentifier ())
341+ ->where ($ typeColumn , get_class ($ user ))
342+ ->where ($ idColumn , $ user ->getAuthIdentifier ())
311343 ->delete ();
312344 }
313345
@@ -317,8 +349,12 @@ public function generateRecoveryCodes(Authenticatable $user, ?int $count = null,
317349 $ hash = $ this ->hashRecoveryCode ($ code );
318350
319351 $ record = new MfaRecoveryCode ();
320- $ record ->user_type = get_class ($ user );
321- $ record ->user_id = $ user ->getAuthIdentifier ();
352+ $ morph = $ this ->config ['morph ' ] ?? [];
353+ $ morphName = $ morph ['name ' ] ?? 'model ' ;
354+ $ typeColumn = $ morphName . '_type ' ;
355+ $ idColumn = $ morphName . '_id ' ;
356+ $ record ->setAttribute ($ typeColumn , get_class ($ user ));
357+ $ record ->setAttribute ($ idColumn , $ user ->getAuthIdentifier ());
322358 $ record ->code_hash = $ hash ;
323359 $ record ->used_at = null ;
324360 $ record ->save ();
@@ -333,9 +369,13 @@ public function generateRecoveryCodes(Authenticatable $user, ?int $count = null,
333369 public function verifyRecoveryCode (Authenticatable $ user , string $ code ): bool
334370 {
335371 $ hash = $ this ->hashRecoveryCode ($ code );
372+ $ morph = $ this ->config ['morph ' ] ?? [];
373+ $ morphName = $ morph ['name ' ] ?? 'model ' ;
374+ $ typeColumn = $ morphName . '_type ' ;
375+ $ idColumn = $ morphName . '_id ' ;
336376 $ record = MfaRecoveryCode::query ()
337- ->where (' user_type ' , get_class ($ user ))
338- ->where (' user_id ' , $ user ->getAuthIdentifier ())
377+ ->where ($ typeColumn , get_class ($ user ))
378+ ->where ($ idColumn , $ user ->getAuthIdentifier ())
339379 ->whereNull ('used_at ' )
340380 ->where ('code_hash ' , $ hash )
341381 ->first ();
@@ -359,19 +399,27 @@ public function verifyRecoveryCode(Authenticatable $user, string $code): bool
359399 /** Get remaining (unused) recovery codes count for the user. */
360400 public function getRemainingRecoveryCodesCount (Authenticatable $ user ): int
361401 {
402+ $ morph = $ this ->config ['morph ' ] ?? [];
403+ $ morphName = $ morph ['name ' ] ?? 'model ' ;
404+ $ typeColumn = $ morphName . '_type ' ;
405+ $ idColumn = $ morphName . '_id ' ;
362406 return MfaRecoveryCode::query ()
363- ->where (' user_type ' , get_class ($ user ))
364- ->where (' user_id ' , $ user ->getAuthIdentifier ())
407+ ->where ($ typeColumn , get_class ($ user ))
408+ ->where ($ idColumn , $ user ->getAuthIdentifier ())
365409 ->whereNull ('used_at ' )
366410 ->count ();
367411 }
368412
369413 /** Delete all recovery codes for the user. Returns number deleted. */
370414 public function clearRecoveryCodes (Authenticatable $ user ): int
371415 {
416+ $ morph = $ this ->config ['morph ' ] ?? [];
417+ $ morphName = $ morph ['name ' ] ?? 'model ' ;
418+ $ typeColumn = $ morphName . '_type ' ;
419+ $ idColumn = $ morphName . '_id ' ;
372420 return MfaRecoveryCode::query ()
373- ->where (' user_type ' , get_class ($ user ))
374- ->where (' user_id ' , $ user ->getAuthIdentifier ())
421+ ->where ($ typeColumn , get_class ($ user ))
422+ ->where ($ idColumn , $ user ->getAuthIdentifier ())
375423 ->delete ();
376424 }
377425
0 commit comments