diff --git a/src/Guard.php b/src/Guard.php index 4f697ce20..55c5d68a3 100644 --- a/src/Guard.php +++ b/src/Guard.php @@ -38,6 +38,25 @@ public static function getNames($model): Collection return self::getConfigAuthGuards($class); } + /** + * Get the model class associated with a given provider. + * + * @param string $provider + * @return string|null + */ + protected static function getProviderModel(string $provider): ?string + { + // Get the provider configuration + $providerConfig = config("auth.providers.{$provider}"); + + // Handle LDAP provider or standard Eloquent provider + if (isset($providerConfig['driver']) && $providerConfig['driver'] === 'ldap') { + return $providerConfig['database']['model'] ?? null; + } + + return $providerConfig['model'] ?? null; + } + /** * Get list of relevant guards for the $class model based on config(auth) settings. * @@ -50,11 +69,35 @@ public static function getNames($model): Collection protected static function getConfigAuthGuards(string $class): Collection { return collect(config('auth.guards')) - ->map(fn ($guard) => isset($guard['provider']) ? config("auth.providers.{$guard['provider']}.model") : null) + ->map(function ($guard) { + if (!isset($guard['provider'])) { + return null; + } + + return static::getProviderModel($guard['provider']); + }) ->filter(fn ($model) => $class === $model) ->keys(); } + /** + * Get the model associated with a given guard name. + * + * @param string $guard + * @return string|null + */ + public static function getModelForGuard(string $guard): ?string + { + // Get the provider configuration for the given guard + $provider = config("auth.guards.{$guard}.provider"); + + if (!$provider) { + return null; + } + + return static::getProviderModel($provider); + } + /** * Lookup a guard name relevant for the $class model and the current user. * diff --git a/src/helpers.php b/src/helpers.php index 55048d753..cf7d38873 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -4,12 +4,11 @@ /** * @return string|null */ - function getModelForGuard(string $guard) + function getModelForGuard(string $guard): ?string { - return collect(config('auth.guards')) - ->map(fn ($guard) => isset($guard['provider']) ? config("auth.providers.{$guard['provider']}.model") : null) - ->get($guard); + return Spatie\Permission\Guard::getModelForGuard($guard); } + } if (! function_exists('setPermissionsTeamId')) {