|
3 | 3 | namespace Adldap\Laravel\Middleware;
|
4 | 4 |
|
5 | 5 | use Closure;
|
6 |
| -use Adldap\Models\ModelNotFoundException; |
7 |
| -use Adldap\Laravel\Traits\UsesAdldap; |
8 |
| -use Adldap\Laravel\Traits\DispatchesAuthEvents; |
| 6 | +use Adldap\Models\User; |
| 7 | +use Adldap\Laravel\Facades\Resolver; |
| 8 | +use Adldap\Laravel\Commands\Import; |
| 9 | +use Adldap\Laravel\Commands\SyncPassword; |
9 | 10 | use Adldap\Laravel\Auth\DatabaseUserProvider;
|
10 | 11 | use Adldap\Laravel\Auth\NoDatabaseUserProvider;
|
| 12 | +use Adldap\Laravel\Events\AuthenticatedWithWindows; |
11 | 13 | use Illuminate\Http\Request;
|
12 | 14 | use Illuminate\Contracts\Auth\Guard;
|
| 15 | +use Illuminate\Support\Facades\Bus; |
| 16 | +use Illuminate\Support\Facades\Event; |
13 | 17 |
|
14 | 18 | class WindowsAuthenticate
|
15 | 19 | {
|
16 |
| - use UsesAdldap, DispatchesAuthEvents; |
17 |
| - |
18 | 20 | /**
|
19 | 21 | * The authenticator implementation.
|
20 | 22 | *
|
@@ -44,7 +46,7 @@ public function handle(Request $request, Closure $next)
|
44 | 46 | {
|
45 | 47 | if (!$this->auth->check()) {
|
46 | 48 | // Retrieve the SSO login attribute.
|
47 |
| - $auth = $this->getWindowsAuthAttribute(); |
| 49 | + $auth = $this->attribute(); |
48 | 50 |
|
49 | 51 | // Retrieve the SSO input key.
|
50 | 52 | $key = key($auth);
|
@@ -95,61 +97,66 @@ protected function retrieveAuthenticatedUser($key, $username)
|
95 | 97 | {
|
96 | 98 | $provider = $this->auth->getProvider();
|
97 | 99 |
|
98 |
| - try { |
99 |
| - $resolver = $this->getResolver(); |
100 |
| - |
101 |
| - // Find the user in AD. |
102 |
| - $user = $resolver->query()->where([$key => $username])->firstOrFail(); |
103 |
| - |
| 100 | + // Find the user in AD. |
| 101 | + if ($user = Resolver::query()->where([$key => $username])->first()) { |
104 | 102 | if ($provider instanceof NoDatabaseUserProvider) {
|
105 |
| - $this->handleAuthenticatedWithWindows($user); |
| 103 | + Event::fire(new AuthenticatedWithWindows($user)); |
106 | 104 |
|
107 | 105 | return $user;
|
108 | 106 | } elseif ($provider instanceof DatabaseUserProvider) {
|
109 |
| - $credentials = [ |
110 |
| - $resolver->getEloquentUsername() => $user->getFirstAttribute($resolver->getLdapUsername()), |
111 |
| - ]; |
| 107 | + $credentials = $this->makeCredentials($user); |
112 | 108 |
|
113 | 109 | // Here we'll import the AD user. If the user already exists in
|
114 | 110 | // our local database, it will be returned from the importer.
|
115 |
| - $model = $this->getImporter()->run($user, $this->getModel(), $credentials); |
| 111 | + $model = Bus::dispatch( |
| 112 | + new Import($user, $this->model(), $credentials) |
| 113 | + ); |
116 | 114 |
|
117 |
| - // We'll assign a random password for the authenticating user. |
118 |
| - $password = str_random(); |
119 |
| - |
120 |
| - // Set the models password. |
121 |
| - $model->password = $model->hasSetMutator('password') ? |
122 |
| - $password : bcrypt($password); |
| 115 | + // We'll sync / set the users password after |
| 116 | + // our model has been synchronized. |
| 117 | + Bus::dispatch(new SyncPassword($model)); |
123 | 118 |
|
124 | 119 | // We also want to save the returned model in case it doesn't
|
125 | 120 | // exist yet, or there are changes to be synced.
|
126 | 121 | $model->save();
|
127 | 122 |
|
128 |
| - $this->handleAuthenticatedWithWindows($user, $model); |
| 123 | + Event::fire(new AuthenticatedWithWindows($user, $model)); |
129 | 124 |
|
130 | 125 | return $model;
|
131 | 126 | }
|
132 |
| - } catch (ModelNotFoundException $e) { |
133 |
| - // User could not be located. |
134 | 127 | }
|
135 | 128 | }
|
136 | 129 |
|
| 130 | + /** |
| 131 | + * Returns a credentials array to be used in the import command. |
| 132 | + * |
| 133 | + * @param User $user |
| 134 | + * |
| 135 | + * @return array |
| 136 | + */ |
| 137 | + protected function makeCredentials(User $user) |
| 138 | + { |
| 139 | + return [ |
| 140 | + Resolver::getEloquentUsername() => $user->getFirstAttribute(Resolver::getLdapUsername()), |
| 141 | + ]; |
| 142 | + } |
| 143 | + |
137 | 144 | /**
|
138 | 145 | * Returns the configured authentication model.
|
139 | 146 | *
|
140 | 147 | * @return \Illuminate\Database\Eloquent\Model
|
141 | 148 | */
|
142 |
| - protected function getModel() |
| 149 | + protected function model() |
143 | 150 | {
|
144 |
| - return auth()->getProvider()->createModel(); |
| 151 | + return $this->auth->getProvider()->createModel(); |
145 | 152 | }
|
146 | 153 |
|
147 | 154 | /**
|
148 | 155 | * Returns the windows authentication attribute.
|
149 | 156 | *
|
150 | 157 | * @return string
|
151 | 158 | */
|
152 |
| - protected function getWindowsAuthAttribute() |
| 159 | + protected function attribute() |
153 | 160 | {
|
154 | 161 | return config('adldap_auth.windows_auth_attribute', ['samaccountname' => 'AUTH_USER']);
|
155 | 162 | }
|
|
0 commit comments