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

Commit 3d43cb2

Browse files
committed
Rework providers to utilize new resolver facade
1 parent 4274814 commit 3d43cb2

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

src/Auth/DatabaseUserProvider.php

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
namespace Adldap\Laravel\Auth;
44

55
use Adldap\Models\User;
6+
use Adldap\Laravel\Facades\Resolver;
7+
use Adldap\Laravel\Commands\Import;
8+
use Adldap\Laravel\Commands\SyncPassword;
9+
use Adldap\Laravel\Events\DiscoveredWithCredentials;
10+
use Adldap\Laravel\Events\AuthenticatedWithCredentials;
11+
use Illuminate\Support\Facades\Bus;
12+
use Illuminate\Support\Facades\Event;
613
use Illuminate\Auth\EloquentUserProvider;
714
use Illuminate\Contracts\Hashing\Hasher;
815
use Illuminate\Contracts\Auth\UserProvider;
@@ -87,17 +94,18 @@ public function updateRememberToken(Authenticatable $user, $token)
8794
public function retrieveByCredentials(array $credentials)
8895
{
8996
// Retrieve the LDAP user who is authenticating.
90-
$user = $this->getResolver()->byCredentials($credentials);
97+
$user = Resolver::byCredentials($credentials);
9198

9299
if ($user instanceof User) {
93100
// Set the currently authenticating LDAP user.
94101
$this->user = $user;
95102

96-
$this->handleDiscoveredWithCredentials($user);
103+
Event::fire(new DiscoveredWithCredentials($user));
97104

98105
// Import / locate the local user account.
99-
return $this->getImporter()
100-
->run($user, $this->createModel(), $credentials);
106+
return Bus::dispatch(
107+
new Import($user, $this->createModel(), $credentials)
108+
);
101109
}
102110

103111
if ($this->isFallingBack()) {
@@ -114,14 +122,17 @@ public function validateCredentials(Authenticatable $model, array $credentials)
114122
// they pass authentication before going further.
115123
if (
116124
$this->user instanceof User &&
117-
$this->getResolver()->authenticate($this->user, $credentials)
125+
Resolver::authenticate($this->user, $credentials)
118126
) {
119-
$this->handleAuthenticatedWithCredentials($this->user, $model);
127+
Event::fire(new AuthenticatedWithCredentials($this->user, $model));
120128

121129
// Here we will perform authorization on the LDAP user. If all
122130
// validation rules pass, we will allow the authentication
123131
// attempt. Otherwise, it is automatically rejected.
124-
if ($this->passesValidation($model)) {
132+
if ($this->passesValidation($this->user, $model)) {
133+
// Sync / set the users password since it has been verified.
134+
Bus::dispatch(new SyncPassword($model, $credentials));
135+
125136
// All of our validation rules have passed and we can
126137
// finally save the model in case of changes.
127138
$model->save();
@@ -176,19 +187,6 @@ public function __call($name, $arguments)
176187
return call_user_func_array([$this->fallback, $name], $arguments);
177188
}
178189

179-
/**
180-
* Determines if the model passes validation.
181-
*
182-
* @param Authenticatable $model
183-
*
184-
* @return bool
185-
*/
186-
protected function passesValidation(Authenticatable $model)
187-
{
188-
return $this->newValidator($this->getRules($this->user, $model))
189-
->passes();
190-
}
191-
192190
/**
193191
* Determines if login fallback is enabled.
194192
*

src/Auth/NoDatabaseUserProvider.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace Adldap\Laravel\Auth;
44

5+
use Adldap\Laravel\Facades\Resolver;
6+
use Adldap\Laravel\Events\DiscoveredWithCredentials;
7+
use Adldap\Laravel\Events\AuthenticatedWithCredentials;
8+
use Illuminate\Support\Facades\Event;
59
use Illuminate\Contracts\Auth\Authenticatable;
610

711
class NoDatabaseUserProvider extends Provider
@@ -11,7 +15,7 @@ class NoDatabaseUserProvider extends Provider
1115
*/
1216
public function retrieveById($identifier)
1317
{
14-
$user = $this->getResolver()->byId($identifier);
18+
$user = Resolver::byId($identifier);
1519

1620
if ($user instanceof Authenticatable) {
1721
// We'll verify we have the correct instance just to ensure we
@@ -41,8 +45,8 @@ public function updateRememberToken(Authenticatable $user, $token)
4145
*/
4246
public function retrieveByCredentials(array $credentials)
4347
{
44-
if ($user = $this->getResolver()->byCredentials($credentials)) {
45-
$this->handleDiscoveredWithCredentials($user);
48+
if ($user = Resolver::byCredentials($credentials)) {
49+
Event::fire(new DiscoveredWithCredentials($user));
4650

4751
return $user;
4852
}
@@ -55,10 +59,10 @@ public function validateCredentials(Authenticatable $user, array $credentials)
5559
{
5660
// Perform LDAP authentication and validate the authenticated model.
5761
if (
58-
$this->getResolver()->authenticate($user, $credentials) &&
59-
$this->newValidator($this->getRules($user))->passes()
62+
Resolver::authenticate($user, $credentials) &&
63+
$this->passesValidation($user)
6064
) {
61-
$this->handleAuthenticatedWithCredentials($user);
65+
Event::fire(new AuthenticatedWithCredentials($user));
6266

6367
return true;
6468
}

0 commit comments

Comments
 (0)