Skip to content
This repository was archived by the owner on Jul 24, 2023. It is now read-only.

Commit 145e7d6

Browse files
committed
Rework importing from WindowsAuthenticate
1 parent 9007721 commit 145e7d6

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/Auth/Importer.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Adldap\Models\User;
66
use Adldap\AdldapException;
7+
use Illuminate\Support\Str;
78
use Illuminate\Database\Eloquent\Model;
89

910
class Importer implements ImporterInterface
@@ -59,10 +60,13 @@ protected function findByCredentials(Model $model, array $credentials = [])
5960
$model = $model->withTrashed();
6061
}
6162

62-
$username = $this->getEloquentUsername();
63+
foreach ($credentials as $key => $value) {
64+
if (! Str::contains($key, 'password')) {
65+
$model->where($key, $value);
66+
}
67+
}
6368

64-
return $model->where([$username => $credentials[$username]])
65-
->first();
69+
return $model->first();
6670
}
6771

6872
/**

src/Middleware/WindowsAuthenticate.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,10 @@ protected function retrieveAuthenticatedUser($key, $username)
8383
$provider = $this->auth->getProvider();
8484

8585
try {
86+
$resolver = $this->getResolver();
87+
8688
// Find the user in AD.
87-
$user = $this->getResolver()->query()
89+
$user = $resolver->query()
8890
->where([$key => $username])
8991
->firstOrFail();
9092

@@ -93,10 +95,14 @@ protected function retrieveAuthenticatedUser($key, $username)
9395

9496
return $user;
9597
} elseif ($provider instanceof DatabaseUserProvider) {
98+
$credentials = [
99+
$resolver->getEloquentUsername() => $user->getFirstAttribute($resolver->getLdapUsername()),
100+
];
101+
96102
// Retrieve the Eloquent user model from our AD user instance.
97103
// We'll assign the user a random password since we don't
98104
// have access to it through SSO auth.
99-
$model = $this->getImporter()->run($user, $this->getModel(), ['password' => str_random()]);
105+
$model = $this->getImporter()->run($user, $this->getModel(), $credentials);
100106

101107
// Save model in case of changes.
102108
$model->save();

0 commit comments

Comments
 (0)