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

Commit be9a381

Browse files
committed
Use ldap validation rules in SSO middleware
Closes #748
1 parent aaaadf3 commit be9a381

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

src/Middleware/WindowsAuthenticate.php

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
use Illuminate\Support\Facades\Event;
1313
use Illuminate\Support\Facades\Config;
1414
use Adldap\Laravel\Commands\SyncPassword;
15+
use Adldap\Laravel\Traits\ValidatesUsers;
1516
use Adldap\Laravel\Auth\DatabaseUserProvider;
16-
use Adldap\Laravel\Auth\NoDatabaseUserProvider;
1717
use Adldap\Laravel\Events\AuthenticatedWithWindows;
1818

1919
class WindowsAuthenticate
2020
{
21+
use ValidatesUsers;
22+
2123
/**
2224
* The authenticator implementation.
2325
*
@@ -72,30 +74,34 @@ protected function retrieveAuthenticatedUser($username)
7274
{
7375
// Find the user in LDAP.
7476
if ($user = $this->resolveUserByUsername($username)) {
75-
$provider = $this->auth->getProvider();
76-
77-
if ($provider instanceof NoDatabaseUserProvider) {
78-
$this->fireAuthenticatedEvent($user);
77+
$model = null;
7978

80-
return $user;
81-
} elseif ($provider instanceof DatabaseUserProvider) {
82-
// Here we'll import the LDAP user. If the user already exists in
79+
// If we are using the DatabaseUserProvider, we must locate or import
80+
// the users model that is currently authenticated with SSO.
81+
if ($this->auth->getProvider() instanceof DatabaseUserProvider) {
82+
// Here we will import the LDAP user. If the user already exists in
8383
// our local database, it will be returned from the importer.
8484
$model = Bus::dispatch(
8585
new Import($user, $this->model())
8686
);
87+
}
8788

88-
// We'll sync / set the users password after
89-
// our model has been synchronized.
90-
Bus::dispatch(new SyncPassword($model));
91-
92-
// We also want to save the returned model in case it doesn't
93-
// exist yet, or there are changes to be synced.
94-
$model->save();
89+
// Here we will validate that the authenticating user
90+
// passes our LDAP authentication rules in place.
91+
if ($this->passesValidation($user, $model)) {
92+
if ($model) {
93+
// We will sync / set the users password after
94+
// our model has been synchronized.
95+
Bus::dispatch(new SyncPassword($model));
96+
97+
// We also want to save the model in case it doesn't
98+
// exist yet, or there are changes to be synced.
99+
$model->save();
100+
}
95101

96102
$this->fireAuthenticatedEvent($user, $model);
97103

98-
return $model;
104+
return $model ? $model : $user;
99105
}
100106
}
101107
}

0 commit comments

Comments
 (0)