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

Commit 80cbeec

Browse files
committed
Added config sync attribute callbacks
- Added ability to define a callback class and method to retrieve fields from the user model upon synchronization.
1 parent 6ac598a commit 80cbeec

File tree

3 files changed

+99
-12
lines changed

3 files changed

+99
-12
lines changed

src/AdldapAuthUserProvider.php

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*

tests/AdldapTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,17 @@ public function testCredentialsKeyDoesNotExist()
161161

162162
Auth::attempt([$nonExistantInputKey => '[email protected]', 'password' => '12345']);
163163
}
164+
165+
public function testConfigCallbackAttributeHandler()
166+
{
167+
$this->app['config']->set('adldap_auth.sync_attributes', [
168+
'name' => 'Adldap\Laravel\Tests\Handlers\LdapAttributeHandler@name',
169+
]);
170+
171+
$this->testAuthPasses();
172+
173+
$user = \Auth::user();
174+
175+
$this->assertEquals('handled', $user->name);
176+
}
164177
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Adldap\Laravel\Tests\Handlers;
4+
5+
use Adldap\Models\User;
6+
7+
class LdapAttributeHandler
8+
{
9+
/**
10+
* Returns the common name of the AD User.
11+
*
12+
* @param User $user
13+
*
14+
* @return string
15+
*/
16+
public function name(User $user)
17+
{
18+
return 'handled';
19+
}
20+
}

0 commit comments

Comments
 (0)