Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LDAP model lookup from Auth Provider #2750

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
47 changes: 46 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,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.
*
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