Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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