|
12 | 12 | use Illuminate\Support\Facades\Event; |
13 | 13 | use Illuminate\Support\Facades\Config; |
14 | 14 | use Adldap\Laravel\Commands\SyncPassword; |
| 15 | +use Adldap\Laravel\Traits\ValidatesUsers; |
15 | 16 | use Adldap\Laravel\Auth\DatabaseUserProvider; |
16 | | -use Adldap\Laravel\Auth\NoDatabaseUserProvider; |
17 | 17 | use Adldap\Laravel\Events\AuthenticatedWithWindows; |
18 | 18 |
|
19 | 19 | class WindowsAuthenticate |
20 | 20 | { |
| 21 | + use ValidatesUsers; |
| 22 | + |
21 | 23 | /** |
22 | 24 | * The authenticator implementation. |
23 | 25 | * |
@@ -72,30 +74,34 @@ protected function retrieveAuthenticatedUser($username) |
72 | 74 | { |
73 | 75 | // Find the user in LDAP. |
74 | 76 | if ($user = $this->resolveUserByUsername($username)) { |
75 | | - $provider = $this->auth->getProvider(); |
76 | | - |
77 | | - if ($provider instanceof NoDatabaseUserProvider) { |
78 | | - $this->fireAuthenticatedEvent($user); |
| 77 | + $model = null; |
79 | 78 |
|
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 |
83 | 83 | // our local database, it will be returned from the importer. |
84 | 84 | $model = Bus::dispatch( |
85 | 85 | new Import($user, $this->model()) |
86 | 86 | ); |
| 87 | + } |
87 | 88 |
|
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 | + } |
95 | 101 |
|
96 | 102 | $this->fireAuthenticatedEvent($user, $model); |
97 | 103 |
|
98 | | - return $model; |
| 104 | + return $model ? $model : $user; |
99 | 105 | } |
100 | 106 | } |
101 | 107 | } |
|
0 commit comments