Skip to content

Commit e56c439

Browse files
Update Guard.php
1 parent 8ebcc8e commit e56c439

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

src/Guard.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,25 @@ public static function getNames($model): Collection
3838
return self::getConfigAuthGuards($class);
3939
}
4040

41+
/**
42+
* Get the model class associated with a given provider.
43+
*
44+
* @param string $provider
45+
* @return string|null
46+
*/
47+
protected static function getProviderModel(string $provider): ?string
48+
{
49+
// Get the provider configuration
50+
$providerConfig = config("auth.providers.{$provider}");
51+
52+
// Handle LDAP provider or standard Eloquent provider
53+
if (isset($providerConfig['driver']) && $providerConfig['driver'] === 'ldap') {
54+
return $providerConfig['database']['model'] ?? null;
55+
}
56+
57+
return $providerConfig['model'] ?? null;
58+
}
59+
4160
/**
4261
* Get list of relevant guards for the $class model based on config(auth) settings.
4362
*
@@ -50,11 +69,37 @@ public static function getNames($model): Collection
5069
protected static function getConfigAuthGuards(string $class): Collection
5170
{
5271
return collect(config('auth.guards'))
53-
->map(fn ($guard) => isset($guard['provider']) ? config("auth.providers.{$guard['provider']}.model") : null)
72+
->map(function ($guard) {
73+
if (!isset($guard['provider'])) {
74+
return null;
75+
}
76+
77+
// Use the new getProviderModel method to fetch the model
78+
return static::getProviderModel($guard['provider']);
79+
})
5480
->filter(fn ($model) => $class === $model)
5581
->keys();
5682
}
5783

84+
/**
85+
* Get the model associated with a given guard name.
86+
*
87+
* @param string $guard
88+
* @return string|null
89+
*/
90+
public static function getModelForGuard(string $guard): ?string
91+
{
92+
// Get the provider configuration for the given guard
93+
$provider = config("auth.guards.{$guard}.provider");
94+
95+
if (!$provider) {
96+
return null;
97+
}
98+
99+
// Use the new getProviderModel method to fetch the model
100+
return static::getProviderModel($provider);
101+
}
102+
58103
/**
59104
* Lookup a guard name relevant for the $class model and the current user.
60105
*

0 commit comments

Comments
 (0)