@@ -82,14 +82,28 @@ public function retrieveByCredentials(array $credentials)
82
82
*/
83
83
protected function getModelFromAdldap (User $ user , $ password )
84
84
{
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
+ }
86
99
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 ();
88
102
89
103
if (!$ model ) {
90
104
$ model = $ this ->createModel ();
91
105
92
- $ model ->email = $ email ;
106
+ $ model ->{ $ key } = $ username ;
93
107
$ model ->password = bcrypt ($ password );
94
108
}
95
109
@@ -197,7 +211,7 @@ protected function authenticate($username, $password)
197
211
*/
198
212
protected function getUsernameAttribute ()
199
213
{
200
- return Config::get ('adldap_auth.username_attribute ' , ['email ' => ActiveDirectory::EMAIL ]);
214
+ return Config::get ('adldap_auth.username_attribute ' , ['username ' => ActiveDirectory::ACCOUNT_NAME ]);
201
215
}
202
216
203
217
/**
0 commit comments