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

Commit c932497

Browse files
committed
Fixed UserResolver
1 parent 7fe2d34 commit c932497

File tree

1 file changed

+43
-14
lines changed

1 file changed

+43
-14
lines changed

src/Resolvers/UserResolver.php

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

55
use Adldap\Models\User;
6-
use Adldap\Connections\ProviderInterface;
6+
use Adldap\AdldapInterface;
77
use Illuminate\Support\Facades\Config;
88
use Illuminate\Contracts\Auth\Authenticatable;
99

1010
class UserResolver implements ResolverInterface
1111
{
1212
/**
13-
* The LDAP connection provider.
13+
* The underlying Adldap instance.
1414
*
15-
* @var ProviderInterface
15+
* @var AdldapInterface
1616
*/
17-
protected $provider;
17+
protected $ldap;
18+
19+
/**
20+
* The LDAP connection to utilize.
21+
*
22+
* @var string
23+
*/
24+
protected $connection = 'default';
1825

1926
/**
2027
* {@inheritdoc}
2128
*/
22-
public function __construct(ProviderInterface $provider)
29+
public function __construct(AdldapInterface $ldap)
2330
{
24-
$this->provider = $provider;
31+
$this->ldap = $ldap;
32+
}
33+
34+
/**
35+
* Sets the LDAP connection to use.
36+
*
37+
* @param string $connection
38+
*
39+
* @return void
40+
*/
41+
public function setConnection($connection)
42+
{
43+
$this->connection = $connection;
2544
}
2645

2746
/**
@@ -42,7 +61,7 @@ public function byCredentials(array $credentials = [])
4261
}
4362

4463
return $this->query()
45-
->whereEquals($this->getLdapUsername(), $credentials[$this->getEloquentUsername()])
64+
->whereEquals($this->getLdapDiscoveryAttribute(), $credentials[$this->getEloquentUsernameAttribute()])
4665
->first();
4766
}
4867

@@ -52,7 +71,7 @@ public function byCredentials(array $credentials = [])
5271
public function byModel(Authenticatable $model)
5372
{
5473
return $this->query()
55-
->whereEquals($this->getLdapUsername(), $model->{$this->getEloquentUsername()})
74+
->whereEquals($this->getLdapDiscoveryAttribute(), $model->{$this->getEloquentUsernameAttribute()})
5675
->first();
5776
}
5877

@@ -61,17 +80,17 @@ public function byModel(Authenticatable $model)
6180
*/
6281
public function authenticate(User $user, array $credentials = [])
6382
{
64-
$username = $user->getFirstAttribute($this->getLdapAuthUsername());
83+
$username = $user->getFirstAttribute($this->getLdapAuthAttribute());
6584

66-
return $this->provider->auth()->attempt($username, $credentials['password']);
85+
return $this->getProvider()->auth()->attempt($username, $credentials['password']);
6786
}
6887

6988
/**
7089
* {@inheritdoc}
7190
*/
7291
public function query()
7392
{
74-
$query = $this->provider->search()->users();
93+
$query = $this->getProvider()->search()->users();
7594

7695
foreach ($this->getScopes() as $scope) {
7796
// Create the scope.
@@ -84,26 +103,36 @@ public function query()
84103
return $query;
85104
}
86105

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+
87116
/**
88117
* {@inheritdoc}
89118
*/
90-
public function getLdapUsername()
119+
public function getLdapDiscoveryAttribute()
91120
{
92121
return Config::get('adldap_auth.usernames.ldap.discover', 'userprincipalname');
93122
}
94123

95124
/**
96125
* {@inheritdoc}
97126
*/
98-
public function getLdapAuthUsername()
127+
public function getLdapAuthAttribute()
99128
{
100129
return Config::get('adldap_auth.usernames.ldap.authenticate', 'userprincipalname');
101130
}
102131

103132
/**
104133
* {@inheritdoc}
105134
*/
106-
public function getEloquentUsername()
135+
public function getEloquentUsernameAttribute()
107136
{
108137
return Config::get('adldap_auth.usernames.eloquent', 'email');
109138
}

0 commit comments

Comments
 (0)