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

Commit 7e610f5

Browse files
committed
Fire events and utilize alternate username on provider
1 parent 1fd6164 commit 7e610f5

File tree

1 file changed

+65
-30
lines changed

1 file changed

+65
-30
lines changed

src/Resolvers/UserResolver.php

Lines changed: 65 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@
44

55
use Adldap\Models\User;
66
use Adldap\AdldapInterface;
7+
use Adldap\Laravel\Events\Authenticated;
8+
use Adldap\Laravel\Events\Authenticating;
9+
use Adldap\Laravel\Events\AuthenticationFailed;
10+
use Adldap\Laravel\Auth\DatabaseUserProvider;
11+
use Adldap\Laravel\Auth\NoDatabaseUserProvider;
12+
use Illuminate\Support\Facades\Event;
713
use Illuminate\Support\Facades\Config;
814
use Illuminate\Contracts\Auth\Authenticatable;
915

1016
class UserResolver implements ResolverInterface
1117
{
1218
/**
13-
* The underlying Adldap instance.
19+
* The Adldap instance.
1420
*
1521
* @var AdldapInterface
1622
*/
@@ -32,11 +38,7 @@ public function __construct(AdldapInterface $ldap)
3238
}
3339

3440
/**
35-
* Sets the LDAP connection to use.
36-
*
37-
* @param string $connection
38-
*
39-
* @return void
41+
* {@inheritdoc}
4042
*/
4143
public function setConnection($connection)
4244
{
@@ -60,19 +62,32 @@ public function byCredentials(array $credentials = [])
6062
return;
6163
}
6264

63-
return $this->query()
64-
->whereEquals($this->getLdapDiscoveryAttribute(), $credentials[$this->getEloquentUsernameAttribute()])
65-
->first();
65+
$provider = Config::get('adldap_auth.provider', DatabaseUserProvider::class);
66+
67+
// Depending on the configured user provider, the
68+
// username field will differ for retrieving
69+
// users by their credentials.
70+
if ($provider == NoDatabaseUserProvider::class) {
71+
$username = $credentials[$this->getLdapDiscoveryAttribute()];
72+
} else {
73+
$username = $credentials[$this->getEloquentUsernameAttribute()];
74+
}
75+
76+
$field = $this->getLdapDiscoveryAttribute();
77+
78+
return $this->query()->whereEquals($field, $username)->first();
6679
}
6780

6881
/**
6982
* {@inheritdoc}
7083
*/
7184
public function byModel(Authenticatable $model)
7285
{
73-
return $this->query()
74-
->whereEquals($this->getLdapDiscoveryAttribute(), $model->{$this->getEloquentUsernameAttribute()})
75-
->first();
86+
$field = $this->getLdapDiscoveryAttribute();
87+
88+
$username = $model->{$this->getEloquentUsernameAttribute()};
89+
90+
return $this->query()->whereEquals($field, $username)->first();
7691
}
7792

7893
/**
@@ -82,7 +97,19 @@ public function authenticate(User $user, array $credentials = [])
8297
{
8398
$username = $user->getFirstAttribute($this->getLdapAuthAttribute());
8499

85-
return $this->getProvider()->auth()->attempt($username, $credentials['password']);
100+
$password = $this->getPasswordFromCredentials($credentials);
101+
102+
Event::fire(new Authenticating($user, $username));
103+
104+
if ($this->getProvider()->auth()->attempt($username, $password)) {
105+
Event::fire(new Authenticated($user));
106+
107+
return true;
108+
}
109+
110+
Event::fire(new AuthenticationFailed($user));
111+
112+
return false;
86113
}
87114

88115
/**
@@ -92,27 +119,21 @@ public function query()
92119
{
93120
$query = $this->getProvider()->search()->users();
94121

95-
foreach ($this->getScopes() as $scope) {
96-
// Create the scope.
122+
$scopes = Config::get('adldap_auth.scopes', []);
123+
124+
foreach ($scopes as $scope) {
125+
// Here we will use Laravel's IoC container to construct our scope.
126+
// This allows us to utilize any Laravel dependencies in
127+
// the scopes constructor that may be needed.
97128
$scope = app($scope);
98129

99-
// Apply it to our query.
130+
// With the scope constructed, we can apply it to our query.
100131
$scope->apply($query);
101132
}
102133

103134
return $query;
104135
}
105136

106-
/**
107-
* Returns the configured connection provider.
108-
*
109-
* @return \Adldap\Connections\ProviderInterface
110-
*/
111-
protected function getProvider()
112-
{
113-
return $this->ldap->getProvider($this->connection);
114-
}
115-
116137
/**
117138
* {@inheritdoc}
118139
*/
@@ -138,12 +159,26 @@ public function getEloquentUsernameAttribute()
138159
}
139160

140161
/**
141-
* Returns the configured query scopes.
162+
* Returns the password field to retrieve from the credentials.
163+
*
164+
* @param array $credentials
165+
*
166+
* @return string|null
167+
*/
168+
protected function getPasswordFromCredentials($credentials)
169+
{
170+
return array_get($credentials, 'password');
171+
}
172+
173+
/**
174+
* Retrieves the provider for the current connection.
142175
*
143-
* @return array
176+
* @throws \Adldap\AdldapException
177+
*
178+
* @return \Adldap\Connections\ProviderInterface
144179
*/
145-
protected function getScopes()
180+
protected function getProvider()
146181
{
147-
return Config::get('adldap_auth.scopes', []);
182+
return $this->ldap->getProvider($this->connection);
148183
}
149184
}

0 commit comments

Comments
 (0)