Skip to content

Commit

Permalink
LDAP model lookup from Auth Provider (#2750)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Chris Brown <[email protected]>
  • Loading branch information
crossplatformconsulting and drbyte authored Feb 13, 2025
1 parent 717d5c7 commit 6c7b1de
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
45 changes: 44 additions & 1 deletion src/Guard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand Down
7 changes: 3 additions & 4 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down

0 comments on commit 6c7b1de

Please sign in to comment.