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

Commit b6ad8a7

Browse files
committed
Allow different username attribute for models
- Fixes #9
1 parent 819ca81 commit b6ad8a7

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/AdldapAuthUserProvider.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,28 @@ public function retrieveByCredentials(array $credentials)
8282
*/
8383
protected function getModelFromAdldap(User $user, $password)
8484
{
85-
$email = $user->getEmail();
85+
// Get the username attributes
86+
$attributes = $this->getUsernameAttribute();
87+
88+
// Get the model key
89+
$key = key($attributes);
90+
91+
// Get the username from the AD model
92+
$username = $user->{$attributes[$key]};
93+
94+
// Make sure we retrieve the first username
95+
// result if it's an array
96+
if (is_array($username)) {
97+
$username = Arr::get($username, 0);
98+
}
8699

87-
$model = $this->createModel()->newQuery()->where(compact('email'))->first();
100+
// Try to retrieve the model from the model key and AD username
101+
$model = $this->createModel()->newQuery()->where([$key => $username])->first();
88102

89103
if(!$model) {
90104
$model = $this->createModel();
91105

92-
$model->email = $email;
106+
$model->{$key} = $username;
93107
$model->password = bcrypt($password);
94108
}
95109

@@ -197,7 +211,7 @@ protected function authenticate($username, $password)
197211
*/
198212
protected function getUsernameAttribute()
199213
{
200-
return Config::get('adldap_auth.username_attribute', ['email' => ActiveDirectory::EMAIL]);
214+
return Config::get('adldap_auth.username_attribute', ['username' => ActiveDirectory::ACCOUNT_NAME]);
201215
}
202216

203217
/**

0 commit comments

Comments
 (0)