@@ -142,21 +142,13 @@ protected function syncModelFromAdldap(User $user, Authenticatable $model)
142142 $ attributes = $ this ->getSyncAttributes ();
143143
144144 foreach ($ attributes as $ modelField => $ adField ) {
145- if ($ adField === ActiveDirectory::THUMBNAIL ) {
146- // If the field we're retrieving is the users thumbnail photo, we need
147- // to retrieve it encoded so we're able to save it to the database.
148- $ adValue = $ user ->getThumbnailEncoded ();
145+ if ($ this ->isAttributeCallback ($ adField )) {
146+ $ value = $ this ->handleAttributeCallback ($ user , $ adField );
149147 } else {
150- $ adValue = $ user ->{$ adField };
151-
152- if (is_array ($ adValue )) {
153- // If the AD Value is an array, we'll
154- // retrieve the first value.
155- $ adValue = Arr::get ($ adValue , 0 );
156- }
148+ $ value = $ this ->handleAttributeRetrieval ($ user , $ adField );
157149 }
158150
159- $ model ->{$ modelField } = $ adValue ;
151+ $ model ->{$ modelField } = $ value ;
160152 }
161153
162154 if ($ model instanceof Model) {
@@ -259,6 +251,68 @@ protected function authenticate($username, $password)
259251 return Adldap::authenticate ($ username , $ password );
260252 }
261253
254+ /**
255+ * Returns true / false if the specified string
256+ * is a callback for an attribute handler.
257+ *
258+ * @param string $string
259+ *
260+ * @return bool
261+ */
262+ protected function isAttributeCallback ($ string )
263+ {
264+ $ matches = preg_grep ("/(\w)@(\w)/ " , explode ("\n" , $ string ));
265+
266+ return (count ($ matches ) > 0 );
267+ }
268+
269+ /**
270+ * Handles retrieving the value from an attribute callback.
271+ *
272+ * @param User $user
273+ * @param string $callback
274+ *
275+ * @return mixed
276+ */
277+ protected function handleAttributeCallback (User $ user , $ callback )
278+ {
279+ // Explode the callback into its class and method.
280+ list ($ class , $ method ) = explode ('@ ' , $ callback );
281+
282+ // Create the handler.
283+ $ handler = app ($ class );
284+
285+ // Call the attribute handler method and return the result.
286+ return call_user_func_array ([$ handler , $ method ], [$ user ]);
287+ }
288+
289+ /**
290+ * Handles retrieving the specified field from the User model.
291+ *
292+ * @param User $user
293+ * @param string $field
294+ *
295+ * @return string|null
296+ */
297+ protected function handleAttributeRetrieval (User $ user , $ field )
298+ {
299+ if ($ field === ActiveDirectory::THUMBNAIL ) {
300+ // If the field we're retrieving is the users thumbnail photo, we need
301+ // to retrieve it encoded so we're able to save it to the database.
302+ $ value = $ user ->getThumbnailEncoded ();
303+ } else {
304+ $ value = $ user ->{$ field };
305+
306+ if (is_array ($ value )) {
307+ // If the AD Value is an array, we'll
308+ // retrieve the first value.
309+ $ value = Arr::get ($ value , 0 );
310+ }
311+ }
312+
313+ return $ value ;
314+ }
315+
262316 /**
263317 * Returns the username attribute for discovering LDAP users.
264318 *
0 commit comments