From b23b9268dff144dce3f4ad52ed0eb2f4225d59bf Mon Sep 17 00:00:00 2001 From: crossplatformconsulting <68124218+crossplatformconsulting@users.noreply.github.com> Date: Wed, 23 Oct 2024 16:28:36 +0200 Subject: [PATCH 1/3] Update helpers.php --- src/helpers.php | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/helpers.php b/src/helpers.php index 55048d75..15d36a96 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -6,10 +6,28 @@ */ function getModelForGuard(string $guard) { - return collect(config('auth.guards')) - ->map(fn ($guard) => isset($guard['provider']) ? config("auth.providers.{$guard['provider']}.model") : null) - ->get($guard); + // Get the guard configuration + $guardConfig = config("auth.guards.{$guard}"); + + // If the guard has a provider and the provider is defined + if (isset($guardConfig['provider'])) { + $provider = $guardConfig['provider']; + + // Check if the provider uses LDAP + $providerConfig = config("auth.providers.{$provider}"); + + if (isset($providerConfig['driver']) && $providerConfig['driver'] === 'ldap') { + // Return the Eloquent model defined in the LDAP provider's database configuration + return $providerConfig['database']['model'] ?? null; + } + + // Otherwise, return the standard Eloquent model + return config("auth.providers.{$provider}.model"); + } + + return null; } + } if (! function_exists('setPermissionsTeamId')) { From 8ebcc8e3c7c1c5cb613ab25b57f24ee5370298a1 Mon Sep 17 00:00:00 2001 From: crossplatformconsulting <68124218+crossplatformconsulting@users.noreply.github.com> Date: Wed, 23 Oct 2024 20:16:00 +0200 Subject: [PATCH 2/3] Update helpers.php --- src/helpers.php | 23 ++--------------------- 1 file changed, 2 insertions(+), 21 deletions(-) diff --git a/src/helpers.php b/src/helpers.php index 15d36a96..cf7d3887 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -4,28 +4,9 @@ /** * @return string|null */ - function getModelForGuard(string $guard) + function getModelForGuard(string $guard): ?string { - // Get the guard configuration - $guardConfig = config("auth.guards.{$guard}"); - - // If the guard has a provider and the provider is defined - if (isset($guardConfig['provider'])) { - $provider = $guardConfig['provider']; - - // Check if the provider uses LDAP - $providerConfig = config("auth.providers.{$provider}"); - - if (isset($providerConfig['driver']) && $providerConfig['driver'] === 'ldap') { - // Return the Eloquent model defined in the LDAP provider's database configuration - return $providerConfig['database']['model'] ?? null; - } - - // Otherwise, return the standard Eloquent model - return config("auth.providers.{$provider}.model"); - } - - return null; + return Spatie\Permission\Guard::getModelForGuard($guard); } } From e56c439a3f4ff121266c8cfa4930ce01a866333b Mon Sep 17 00:00:00 2001 From: crossplatformconsulting <68124218+crossplatformconsulting@users.noreply.github.com> Date: Wed, 23 Oct 2024 20:16:39 +0200 Subject: [PATCH 3/3] Update Guard.php --- src/Guard.php | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/Guard.php b/src/Guard.php index 4f697ce2..83fc6cda 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,37 @@ 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; + } + + // Use the new getProviderModel method to fetch the model + 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; + } + + // Use the new getProviderModel method to fetch the model + return static::getProviderModel($provider); + } + /** * Lookup a guard name relevant for the $class model and the current user. *