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

Commit 358446d

Browse files
committed
Always treat string sync attributes as LDAP fields
Closes #620
1 parent 9ab0f4b commit 358446d

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

src/Commands/Import.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ protected function sync(Model $model)
119119
foreach ($toSync as $modelField => $ldapField) {
120120
// If the field is a loaded class and contains a `handle()` method,
121121
// we need to construct the attribute handler.
122-
if (class_exists($ldapField) && method_exists($ldapField, 'handle')) {
122+
if (is_string($ldapField) && class_exists($ldapField) && method_exists($ldapField, 'handle')) {
123123
// We will construct the attribute handler using Laravel's
124124
// IoC to allow developers to utilize application
125125
// dependencies in the constructor.
@@ -128,9 +128,10 @@ protected function sync(Model $model)
128128

129129
$handler->handle($this->user, $model);
130130
} else {
131-
// We'll try to retrieve the value from the LDAP model. If nothing is returned
132-
// we'll assume it's a raw value (such as a boolean, array, integer etc.).
133-
$model->{$modelField} = $this->user->getFirstAttribute($ldapField) ?? $ldapField;
131+
// We'll try to retrieve the value from the LDAP model. If the LDAP field is a string,
132+
// we'll assume the developer wants the attribute, or a null value. Otherwise,
133+
// the raw value of the LDAP field will be used.
134+
$model->{$modelField} = is_string($ldapField) ? $this->user->getFirstAttribute($ldapField) : $ldapField;
134135
}
135136
}
136137
}

tests/DatabaseProviderTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,58 @@ public function invalid_attribute_handlers_does_not_throw_exception()
116116
$this->assertInstanceOf(EloquentUser::class, $importer->handle());
117117
}
118118

119+
/** @test */
120+
public function sync_attribute_as_string_will_return_null()
121+
{
122+
config([
123+
'ldap_auth.sync_attributes' => [
124+
'email' => 'userprincipalname',
125+
'name' => 'cn',
126+
]
127+
]);
128+
129+
// LDAP user does not have common name.
130+
$user = $this->makeLdapUser([
131+
'userprincipalname' => '[email protected]',
132+
]);
133+
134+
$importer = new Import($user, new EloquentUser());
135+
136+
$model = $importer->handle();
137+
138+
$this->assertInstanceOf(EloquentUser::class, $model);
139+
$this->assertNull($model->name);
140+
}
141+
142+
/** @test */
143+
public function sync_attribute_as_int_boolean_or_array_will_be_used()
144+
{
145+
config([
146+
'ldap_auth.sync_attributes' => [
147+
'email' => 'userprincipalname',
148+
'string' => 'not-an-LDAP-attribute',
149+
'int' => 1,
150+
'bool' => true,
151+
'array' => ['one', 'two']
152+
]
153+
]);
154+
155+
// LDAP user does not have common name.
156+
$user = $this->makeLdapUser([
157+
'userprincipalname' => '[email protected]',
158+
]);
159+
160+
$importer = new Import($user, new EloquentUser());
161+
162+
$model = $importer->handle();
163+
164+
$this->assertInstanceOf(EloquentUser::class, $model);
165+
$this->assertNull($model->string);
166+
$this->assertEquals($model->int, 1);
167+
$this->assertEquals($model->bool, true);
168+
$this->assertEquals($model->array, ['one', 'two']);
169+
}
170+
119171
/** @test */
120172
public function auth_attempts_fallback_using_config_option()
121173
{

0 commit comments

Comments
 (0)