@@ -24,6 +24,30 @@ abstract public function createModel();
2424 * @return \Illuminate\Database\Eloquent\Model
2525 */
2626 protected function getModelFromAdldap (User $ user , $ password = null )
27+ {
28+ $ model = $ this ->findOrCreateModelFromAdldap ($ user );
29+
30+ // Sync the users password (if enabled). If no password is
31+ // given, we'll pass in a random 16 character string.
32+ $ model = $ this ->syncModelPassword ($ model , $ password ?: str_random ());
33+
34+ // Synchronize other active directory attributes on the model.
35+ $ model = $ this ->syncModelFromAdldap ($ user , $ model );
36+
37+ // Bind the Adldap model to the eloquent model (if enabled).
38+ $ model = ($ this ->getBindUserToModel () ? $ this ->bindAdldapToModel ($ user , $ model ) : $ model );
39+
40+ return $ model ;
41+ }
42+
43+ /**
44+ * Finds an Eloquent model from the specified Adldap user.
45+ *
46+ * @param \Adldap\Models\User $user
47+ *
48+ * @return \Illuminate\Database\Eloquent\Model
49+ */
50+ protected function findOrCreateModelFromAdldap (User $ user )
2751 {
2852 // Get the model key.
2953 $ attributes = $ this ->getUsernameAttribute ();
@@ -34,29 +58,18 @@ protected function getModelFromAdldap(User $user, $password = null)
3458 // Get the username from the AD model.
3559 $ username = $ user ->{$ attributes [$ key ]};
3660
37- // Make sure we retrieve the first username
38- // result if it's an array.
61+ // Make sure we retrieve the first username result if it's an array.
3962 $ username = (is_array ($ username ) ? array_get ($ username , 0 ) : $ username );
4063
41- // Try to retrieve the model from the model key and AD username .
42- $ model = $ this ->createModel ()-> newQuery ()-> where ([ $ key => $ username] )->first ();
64+ // Try to find the local database user record .
65+ $ model = $ this ->newEloquentQuery ( $ key, $ username )->first ();
4366
44- // Create the model instance of it isn't found.
67+ // Create a new model instance of it isn't found.
4568 $ model = ($ model instanceof Model ? $ model : $ this ->createModel ());
4669
4770 // Set the username in case of changes in active directory.
4871 $ model ->{$ key } = $ username ;
4972
50- // Sync the users password (if enabled). If no password is
51- // given, we'll assign a random 16 character string.
52- $ model = $ this ->syncModelPassword ($ model , $ password ?: str_random ());
53-
54- // Synchronize other active directory attributes on the model.
55- $ model = $ this ->syncModelFromAdldap ($ user , $ model );
56-
57- // Bind the Adldap model to the eloquent model (if enabled).
58- $ model = ($ this ->getBindUserToModel () ? $ this ->bindAdldapToModel ($ user , $ model ) : $ model );
59-
6073 return $ model ;
6174 }
6275
@@ -213,6 +226,28 @@ protected function newAdldapUserQuery($provider = null, $filter = null)
213226 return $ query ->select ($ this ->getSelectAttributes ());
214227 }
215228
229+ /**
230+ * Returns a new Eloquent user query.
231+ *
232+ * @param string $key
233+ * @param string $username
234+ *
235+ * @return \Illuminate\Database\Eloquent\Builder
236+ */
237+ protected function newEloquentQuery ($ key , $ username )
238+ {
239+ $ model = $ this ->createModel ();
240+
241+ if (method_exists ($ model , 'trashed ' )) {
242+ // If the trashed method exists on our User model, then we must be
243+ // using soft deletes. We need to make sure we include these
244+ // results so we don't create duplicate user records.
245+ $ model = $ model ->withTrashed ();
246+ }
247+
248+ return $ model ->where ([$ key => $ username ]);
249+ }
250+
216251 /**
217252 * Returns Adldap's current attribute schema.
218253 *
0 commit comments