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

Commit 47890e2

Browse files
committed
Import users via their LDAP identifier (objectguid)
1 parent 40876df commit 47890e2

File tree

1 file changed

+16
-55
lines changed

1 file changed

+16
-55
lines changed

src/Commands/Import.php

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

55
use Adldap\Models\User;
6+
use Adldap\Laravel\Facades\Resolver;
67
use Adldap\Laravel\Events\Importing;
78
use Adldap\Laravel\Events\Synchronized;
89
use Adldap\Laravel\Events\Synchronizing;
9-
use Illuminate\Support\Str;
1010
use Illuminate\Support\Facades\Event;
1111
use Illuminate\Support\Facades\Config;
1212
use Illuminate\Database\Eloquent\Model;
@@ -27,25 +27,16 @@ class Import
2727
*/
2828
protected $model;
2929

30-
/**
31-
* The LDAP users credentials.
32-
*
33-
* @var array
34-
*/
35-
protected $credentials;
36-
3730
/**
3831
* Constructor.
3932
*
4033
* @param User $user
4134
* @param Model $model
42-
* @param array $credentials
4335
*/
44-
public function __construct(User $user, Model $model, array $credentials = [])
36+
public function __construct(User $user, Model $model)
4537
{
46-
$this->user = $this->transformUsername($user);
38+
$this->user = $user;
4739
$this->model = $model;
48-
$this->credentials = $credentials;
4940
}
5041

5142
/**
@@ -58,7 +49,7 @@ public function handle()
5849
// Here we'll try to locate our local user model from
5950
// the LDAP users model. If one isn't located,
6051
// we'll create a new one for them.
61-
$model = $this->findByCredentials() ?: $this->model->newInstance();
52+
$model = $this->findById() ?: $this->model->newInstance();
6253

6354
if (! $model->exists) {
6455
Event::fire(new Importing($this->user, $model));
@@ -78,12 +69,8 @@ public function handle()
7869
*
7970
* @return Model|null
8071
*/
81-
protected function findByCredentials()
72+
protected function findById()
8273
{
83-
if (empty($this->credentials)) {
84-
return;
85-
}
86-
8774
$query = $this->model->newQuery();
8875

8976
if ($query->getMacro('withTrashed')) {
@@ -93,15 +80,11 @@ protected function findByCredentials()
9380
$query->withTrashed();
9481
}
9582

96-
foreach ($this->credentials as $key => $value) {
97-
if (! Str::contains($key, 'password')) {
98-
// We need to lowercase all values so we locate the
99-
// proper model. This avoids case sensitivity.
100-
$query->where($key, strtolower($value));
101-
}
102-
}
103-
104-
return $query->first();
83+
return $query->where(
84+
Resolver::getDatabaseIdentifierColumn(),
85+
'=',
86+
Resolver::getLdapUserIdentifier($this->user)
87+
)->first();
10588
}
10689

10790
/**
@@ -113,6 +96,12 @@ protected function findByCredentials()
11396
*/
11497
protected function sync(Model $model)
11598
{
99+
// Set the users identifier automatically.
100+
$model->setAttribute(
101+
Resolver::getDatabaseIdentifierColumn(),
102+
Resolver::getLdapUserIdentifier($this->user)
103+
);
104+
116105
foreach ($this->getLdapSyncAttributes() as $modelField => $ldapField) {
117106
// If the field is a loaded class and contains a `handle()` method,
118107
// we need to construct the attribute handler.
@@ -133,24 +122,6 @@ protected function sync(Model $model)
133122
}
134123
}
135124

136-
/**
137-
* Transforms the username of the given user to avoid case sensitivity issues.
138-
*
139-
* We want to transform the username on the user model so it persists through attribute handlers.
140-
*
141-
* @param User $user
142-
*
143-
* @return User
144-
*/
145-
protected function transformUsername(User $user)
146-
{
147-
$attribute = $this->getLdapDiscoveryUsername();
148-
149-
$user->setFirstAttribute($attribute, strtolower($user->getFirstAttribute($attribute)));
150-
151-
return $user;
152-
}
153-
154125
/**
155126
* Determines if the given handler value is a class that contains the 'handle' method.
156127
*
@@ -175,14 +146,4 @@ protected function getLdapSyncAttributes()
175146
'name' => 'cn',
176147
]);
177148
}
178-
179-
/**
180-
* Returns the configured LDAP discovery username attribute.
181-
*
182-
* @return string
183-
*/
184-
protected function getLdapDiscoveryUsername()
185-
{
186-
return Config::get('ldap_auth.usernames.ldap.discover', 'userprincipalname');
187-
}
188149
}

0 commit comments

Comments
 (0)