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

Commit 17b4444

Browse files
committed
Retrieve username depending on user provider
Closes #387
1 parent dca401c commit 17b4444

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

src/Resolvers/UserResolver.php

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Adldap\Laravel\Resolvers;
44

55
use Adldap\Models\User;
6+
use Adldap\Laravel\Auth\DatabaseUserProvider;
7+
use Adldap\Laravel\Auth\NoDatabaseUserProvider;
68
use Adldap\Connections\ProviderInterface;
79
use Illuminate\Support\Facades\Config;
810
use Illuminate\Contracts\Auth\Authenticatable;
@@ -41,27 +43,38 @@ public function byCredentials(array $credentials = [])
4143
return;
4244
}
4345

44-
return $this->query()
45-
->whereEquals($this->getLdapUsername(), $credentials[$this->getEloquentUsername()])
46-
->first();
46+
$field = $this->getLdapDiscoveryAttribute();
47+
48+
switch ($this->getUserProvider()) {
49+
case NoDatabaseUserProvider::class:
50+
$username = $credentials[$this->getLdapDiscoveryAttribute()];
51+
break;
52+
default:
53+
$username = $credentials[$this->getEloquentUsernameAttribute()];
54+
break;
55+
}
56+
57+
return $this->query()->whereEquals($field, $username)->first();
4758
}
4859

4960
/**
5061
* {@inheritdoc}
5162
*/
5263
public function byModel(Authenticatable $model)
5364
{
54-
return $this->query()
55-
->whereEquals($this->getLdapUsername(), $model->{$this->getEloquentUsername()})
56-
->first();
65+
$field = $this->getLdapDiscoveryAttribute();
66+
67+
$username = $model->{$this->getEloquentUsernameAttribute()};
68+
69+
return $this->query()->whereEquals($field, $username)->first();
5770
}
5871

5972
/**
6073
* {@inheritdoc}
6174
*/
6275
public function authenticate(User $user, array $credentials = [])
6376
{
64-
$attribute = $user->getAttribute($this->getLdapAuthUsername());
77+
$attribute = $user->getAttribute($this->getLdapAuthAttribute());
6578

6679
$username = is_array($attribute) ? array_first($attribute) : $attribute;
6780

@@ -89,23 +102,23 @@ public function query()
89102
/**
90103
* {@inheritdoc}
91104
*/
92-
public function getLdapUsername()
105+
public function getLdapDiscoveryAttribute()
93106
{
94107
return Config::get('adldap_auth.usernames.ldap.discover', 'userprincipalname');
95108
}
96109

97110
/**
98111
* {@inheritdoc}
99112
*/
100-
public function getLdapAuthUsername()
113+
public function getLdapAuthAttribute()
101114
{
102115
return Config::get('adldap_auth.usernames.ldap.authenticate', 'userprincipalname');
103116
}
104117

105118
/**
106119
* {@inheritdoc}
107120
*/
108-
public function getEloquentUsername()
121+
public function getEloquentUsernameAttribute()
109122
{
110123
return Config::get('adldap_auth.usernames.eloquent', 'email');
111124
}
@@ -119,4 +132,14 @@ protected function getScopes()
119132
{
120133
return Config::get('adldap_auth.scopes', []);
121134
}
135+
136+
/**
137+
* Returns the configured LDAP user provider.
138+
*
139+
* @return string
140+
*/
141+
protected function getUserProvider()
142+
{
143+
return Config::get('adldap_auth.provider', DatabaseUserProvider::class);
144+
}
122145
}

0 commit comments

Comments
 (0)