Skip to content

Commit 6c7b1de

Browse files
LDAP model lookup from Auth Provider (#2750)
--------- Co-authored-by: Chris Brown <[email protected]>
1 parent 717d5c7 commit 6c7b1de

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

src/Guard.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,25 @@ public static function getNames($model): Collection
3838
return self::getConfigAuthGuards($class);
3939
}
4040

41+
/**
42+
* Get the model class associated with a given provider.
43+
*
44+
* @param string $provider
45+
* @return string|null
46+
*/
47+
protected static function getProviderModel(string $provider): ?string
48+
{
49+
// Get the provider configuration
50+
$providerConfig = config("auth.providers.{$provider}");
51+
52+
// Handle LDAP provider or standard Eloquent provider
53+
if (isset($providerConfig['driver']) && $providerConfig['driver'] === 'ldap') {
54+
return $providerConfig['database']['model'] ?? null;
55+
}
56+
57+
return $providerConfig['model'] ?? null;
58+
}
59+
4160
/**
4261
* Get list of relevant guards for the $class model based on config(auth) settings.
4362
*
@@ -50,11 +69,35 @@ public static function getNames($model): Collection
5069
protected static function getConfigAuthGuards(string $class): Collection
5170
{
5271
return collect(config('auth.guards'))
53-
->map(fn ($guard) => isset($guard['provider']) ? config("auth.providers.{$guard['provider']}.model") : null)
72+
->map(function ($guard) {
73+
if (!isset($guard['provider'])) {
74+
return null;
75+
}
76+
77+
return static::getProviderModel($guard['provider']);
78+
})
5479
->filter(fn ($model) => $class === $model)
5580
->keys();
5681
}
5782

83+
/**
84+
* Get the model associated with a given guard name.
85+
*
86+
* @param string $guard
87+
* @return string|null
88+
*/
89+
public static function getModelForGuard(string $guard): ?string
90+
{
91+
// Get the provider configuration for the given guard
92+
$provider = config("auth.guards.{$guard}.provider");
93+
94+
if (!$provider) {
95+
return null;
96+
}
97+
98+
return static::getProviderModel($provider);
99+
}
100+
58101
/**
59102
* Lookup a guard name relevant for the $class model and the current user.
60103
*

src/helpers.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
/**
55
* @return string|null
66
*/
7-
function getModelForGuard(string $guard)
7+
function getModelForGuard(string $guard): ?string
88
{
9-
return collect(config('auth.guards'))
10-
->map(fn ($guard) => isset($guard['provider']) ? config("auth.providers.{$guard['provider']}.model") : null)
11-
->get($guard);
9+
return Spatie\Permission\Guard::getModelForGuard($guard);
1210
}
11+
1312
}
1413

1514
if (! function_exists('setPermissionsTeamId')) {

0 commit comments

Comments
 (0)