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

Commit de5f6ad

Browse files
committed
Properly check for attribute handler
- #560 (cherry picked from commit f5ff704)
1 parent 7daa878 commit de5f6ad

File tree

3 files changed

+9
-22
lines changed

3 files changed

+9
-22
lines changed

src/Commands/Import.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Adldap\Laravel\Commands;
44

55
use Adldap\Models\User;
6-
use Adldap\AdldapException;
76
use Adldap\Laravel\Events\Importing;
87
use Adldap\Laravel\Events\Synchronized;
98
use Adldap\Laravel\Events\Synchronizing;
@@ -53,8 +52,6 @@ public function __construct(User $user, Model $model, array $credentials = [])
5352
* Imports the current LDAP user.
5453
*
5554
* @return Model
56-
*
57-
* @throws AdldapException
5855
*/
5956
public function handle()
6057
{
@@ -110,8 +107,6 @@ protected function findByCredentials()
110107
*
111108
* @param Model $model
112109
*
113-
* @throws AdldapException
114-
*
115110
* @return void
116111
*/
117112
protected function sync(Model $model)
@@ -122,18 +117,15 @@ protected function sync(Model $model)
122117
]);
123118

124119
foreach ($toSync as $modelField => $ldapField) {
125-
// If the field is a loaded class, we can
126-
// assume it's an attribute handler.
127-
if (class_exists($ldapField)) {
120+
// If the field is a loaded class and contains a `handle()` method,
121+
// we need to construct the attribute handler.
122+
if (class_exists($ldapField) && method_exists($ldapField, 'handle')) {
128123
// We will construct the attribute handler using Laravel's
129124
// IoC to allow developers to utilize application
130125
// dependencies in the constructor.
126+
/** @var mixed $handler */
131127
$handler = app($ldapField);
132128

133-
if (! method_exists($handler, 'handle')) {
134-
throw new AdldapException("A public 'handle()' method must be defined when using an attribute handler.");
135-
}
136-
137129
$handler->handle($this->user, $model);
138130
} else {
139131
$model->{$modelField} = $this->user->getFirstAttribute($ldapField);

src/Listeners/BindsLdapUserModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
use Adldap\Laravel\Auth\Provider;
66
use Adldap\Laravel\Facades\Resolver;
77
use Adldap\Laravel\Traits\HasLdapUser;
8+
use Illuminate\Support\Facades\Auth;
89
use Illuminate\Auth\Events\Authenticated;
910
use Illuminate\Contracts\Auth\Authenticatable;
10-
use Illuminate\Support\Facades\Auth;
1111

1212
class BindsLdapUserModel
1313
{

tests/DatabaseProviderTest.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,8 @@ public function attribute_handlers_are_used()
103103
$this->assertEquals('handled', $user->name);
104104
}
105105

106-
/**
107-
* @test
108-
* @expectedException \Adldap\AdldapException
109-
*/
110-
public function invalid_attribute_handlers_throw_exception()
106+
/** @test */
107+
public function invalid_attribute_handlers_does_not_throw_exception()
111108
{
112109
// Inserting an invalid attribute handler that
113110
// does not contain a `handle` method.
@@ -118,11 +115,9 @@ public function invalid_attribute_handlers_throw_exception()
118115
'userprincipalname' => '[email protected]',
119116
]);
120117

121-
$model = new EloquentUser();
122-
123-
$importer = new Import($user, $model);
118+
$importer = new Import($user, new EloquentUser());
124119

125-
$importer->handle();
120+
$this->assertInstanceOf(EloquentUser::class, $importer->handle());
126121
}
127122

128123
/** @test */

0 commit comments

Comments
 (0)