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

Commit 9fe1443

Browse files
committed
Ensure we can retrieve a GUID and username from the LDAP user
- Make sure we can retrieve the LDAP users GUID and configured username by throwing an exception if one does not exist - #693
1 parent c52c007 commit 9fe1443

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

src/Commands/Import.php

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

33
namespace Adldap\Laravel\Commands;
44

5+
use UnexpectedValueException;
56
use Adldap\Models\User;
67
use Adldap\Laravel\Facades\Resolver;
78
use Adldap\Laravel\Events\Importing;
@@ -65,9 +66,11 @@ public function handle()
6566
}
6667

6768
/**
68-
* Retrieves an eloquent user by their credentials.
69+
* Retrieves an eloquent user by their GUID or their username.
6970
*
7071
* @return Model|null
72+
*
73+
* @throws UnexpectedValueException
7174
*/
7275
protected function findUser()
7376
{
@@ -85,11 +88,11 @@ protected function findUser()
8588
return $query->where(
8689
Resolver::getDatabaseIdColumn(),
8790
'=',
88-
$this->user->getConvertedGuid()
91+
$this->getObjectGuid()
8992
)->orWhere(
9093
Resolver::getDatabaseUsernameColumn(),
9194
'=',
92-
$this->user->getFirstAttribute(Resolver::getLdapDiscoveryAttribute())
95+
$this->getUsername()
9396
)->first();
9497
}
9598

@@ -127,6 +130,48 @@ protected function sync(Model $model)
127130
}
128131
}
129132

133+
/**
134+
* Returns the LDAP users configured username.
135+
*
136+
* @return string
137+
*/
138+
protected function getUsername()
139+
{
140+
$attribute = Resolver::getLdapDiscoveryAttribute();
141+
142+
$username = $this->user->getFirstAttribute($attribute);
143+
144+
if (trim($username) == false) {
145+
throw new UnexpectedValueException(
146+
"Unable to locate a user without a {$attribute}"
147+
);
148+
}
149+
150+
return $username;
151+
}
152+
153+
/**
154+
* Returns the LDAP users object GUID.
155+
*
156+
* @return string
157+
*
158+
* @throws UnexpectedValueException
159+
*/
160+
protected function getObjectGuid()
161+
{
162+
$guid = $this->user->getConvertedGuid();
163+
164+
if (trim($guid) == false) {
165+
$attribute = $this->user->getSchema()->objectGuid();
166+
167+
throw new UnexpectedValueException(
168+
"Unable to locate a user without a {$attribute}"
169+
);
170+
}
171+
172+
return $guid;
173+
}
174+
130175
/**
131176
* Determines if the given handler value is a class that contains the 'handle' method.
132177
*

0 commit comments

Comments
 (0)