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

Merged
merged 8 commits into from
Feb 13, 2025
Merged
Next Next commit
Update Guard.php
crossplatformconsulting authored Oct 31, 2024
commit f0be321fb51071410b38b730fbb5f4ab448806ac
47 changes: 46 additions & 1 deletion src/Guard.php
Original file line number Diff line number Diff line change
@@ -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.
*