66use Adldap \Models \User ;
77use Illuminate \Auth \EloquentUserProvider ;
88use Illuminate \Contracts \Auth \Authenticatable ;
9- use Illuminate \Database \Eloquent \Model ;
109use Illuminate \Support \Arr ;
1110use Illuminate \Support \Facades \Config ;
1211
@@ -87,111 +86,6 @@ public function retrieveByCredentials(array $credentials)
8786 }
8887 }
8988
90- /**
91- * Creates a local User from Active Directory.
92- *
93- * @param User $user
94- * @param string $password
95- *
96- * @return \Illuminate\Database\Eloquent\Model
97- */
98- protected function getModelFromAdldap (User $ user , $ password )
99- {
100- // Get the username attributes.
101- $ attributes = $ this ->getUsernameAttribute ();
102-
103- // Get the model key.
104- $ key = key ($ attributes );
105-
106- // Get the username from the AD model.
107- $ username = $ user ->{$ attributes [$ key ]};
108-
109- // Make sure we retrieve the first username
110- // result if it's an array.
111- if (is_array ($ username )) {
112- $ username = Arr::get ($ username , 0 );
113- }
114-
115- // Try to retrieve the model from the model key and AD username.
116- $ model = $ this ->createModel ()->newQuery ()->where ([$ key => $ username ])->first ();
117-
118- // Create the model instance of it isn't found.
119- if (!$ model instanceof Model) {
120- $ model = $ this ->createModel ();
121- }
122-
123- // Set the username and password in case
124- // of changes in active directory.
125- $ model ->{$ key } = $ username ;
126-
127- // Sync the users password.
128- $ model = $ this ->syncModelPassword ($ model , $ password );
129-
130- // Synchronize other active directory
131- // attributes on the model.
132- $ model = $ this ->syncModelFromAdldap ($ user , $ model );
133-
134- if ($ this ->getBindUserToModel ()) {
135- $ model = $ this ->bindAdldapToModel ($ user , $ model );
136- }
137-
138- return $ model ;
139- }
140-
141- /**
142- * Fills a models attributes by the specified Users attributes.
143- *
144- * @param User $user
145- * @param Authenticatable $model
146- *
147- * @return Authenticatable
148- */
149- protected function syncModelFromAdldap (User $ user , Authenticatable $ model )
150- {
151- $ attributes = $ this ->getSyncAttributes ();
152-
153- foreach ($ attributes as $ modelField => $ adField ) {
154- if ($ this ->isAttributeCallback ($ adField )) {
155- $ value = $ this ->handleAttributeCallback ($ user , $ adField );
156- } else {
157- $ value = $ this ->handleAttributeRetrieval ($ user , $ adField );
158- }
159-
160- $ model ->{$ modelField } = $ value ;
161- }
162-
163- if ($ model instanceof Model) {
164- $ model ->save ();
165- }
166-
167- return $ model ;
168- }
169-
170- /**
171- * Syncs the models password with the specified password.
172- *
173- * @param Authenticatable $model
174- * @param string $password
175- *
176- * @return Authenticatable
177- */
178- protected function syncModelPassword (Authenticatable $ model , $ password )
179- {
180- if ($ model instanceof Model && $ model ->hasSetMutator ('password ' )) {
181- // If the model has a set mutator for the password then
182- // we'll assume that the dev is using their
183- // own encryption method for passwords.
184- $ model ->password = $ password ;
185-
186- return $ model ;
187- }
188-
189- // Always encrypt the model password by default.
190- $ model ->password = bcrypt ($ password );
191-
192- return $ model ;
193- }
194-
19589 /**
19690 * Retrieves the Adldap User model from the
19791 * specified Laravel model.
@@ -221,32 +115,6 @@ protected function discoverAdldapFromModel($model)
221115 return $ model ;
222116 }
223117
224- /**
225- * Binds the Adldap User instance to the Eloquent model instance
226- * by setting its `adldapUser` public property.
227- *
228- * @param User $user
229- * @param Authenticatable $model
230- *
231- * @return Authenticatable
232- */
233- protected function bindAdldapToModel (User $ user , Authenticatable $ model )
234- {
235- $ model ->adldapUser = $ user ;
236-
237- return $ model ;
238- }
239-
240- /**
241- * Returns a new Adldap user query.
242- *
243- * @return \Adldap\Query\Builder
244- */
245- protected function newAdldapUserQuery ()
246- {
247- return $ this ->getAdldap ()->search ()->select ($ this ->getSelectAttributes ());
248- }
249-
250118 /**
251119 * Authenticates a user against Active Directory.
252120 *
@@ -260,68 +128,6 @@ protected function authenticate($username, $password)
260128 return $ this ->getAdldap ()->auth ()->attempt ($ username , $ password );
261129 }
262130
263- /**
264- * Returns true / false if the specified string
265- * is a callback for an attribute handler.
266- *
267- * @param string $string
268- *
269- * @return bool
270- */
271- protected function isAttributeCallback ($ string )
272- {
273- $ matches = preg_grep ("/(\w)@(\w)/ " , explode ("\n" , $ string ));
274-
275- return count ($ matches ) > 0 ;
276- }
277-
278- /**
279- * Handles retrieving the value from an attribute callback.
280- *
281- * @param User $user
282- * @param string $callback
283- *
284- * @return mixed
285- */
286- protected function handleAttributeCallback (User $ user , $ callback )
287- {
288- // Explode the callback into its class and method.
289- list ($ class , $ method ) = explode ('@ ' , $ callback );
290-
291- // Create the handler.
292- $ handler = app ($ class );
293-
294- // Call the attribute handler method and return the result.
295- return call_user_func_array ([$ handler , $ method ], [$ user ]);
296- }
297-
298- /**
299- * Handles retrieving the specified field from the User model.
300- *
301- * @param User $user
302- * @param string $field
303- *
304- * @return string|null
305- */
306- protected function handleAttributeRetrieval (User $ user , $ field )
307- {
308- if ($ field === $ this ->getSchema ()->thumbnail ()) {
309- // If the field we're retrieving is the users thumbnail photo, we need
310- // to retrieve it encoded so we're able to save it to the database.
311- $ value = $ user ->getThumbnailEncoded ();
312- } else {
313- $ value = $ user ->{$ field };
314-
315- if (is_array ($ value )) {
316- // If the AD Value is an array, we'll
317- // retrieve the first value.
318- $ value = Arr::get ($ value , 0 );
319- }
320- }
321-
322- return $ value ;
323- }
324-
325131 /**
326132 * Returns the password key to retrieve the
327133 * password from the user input array.
0 commit comments