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

Commit 488245e

Browse files
committed
Added 'usesAdldapProvider' method to the 'BindsLdapUserModel' listener
1 parent ead9646 commit 488245e

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

src/Listeners/BindsLdapUserModel.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace Adldap\Laravel\Listeners;
44

5+
use Adldap\Laravel\Auth\Provider;
56
use Adldap\Laravel\Facades\Resolver;
67
use Adldap\Laravel\Traits\HasLdapUser;
78
use Illuminate\Auth\Events\Authenticated;
89
use Illuminate\Contracts\Auth\Authenticatable;
10+
use Illuminate\Support\Facades\Auth;
911

1012
class BindsLdapUserModel
1113
{
@@ -18,13 +20,23 @@ class BindsLdapUserModel
1820
*/
1921
public function handle(Authenticated $event)
2022
{
21-
if ($this->canBind($event->user)) {
23+
if ($this->usesAdldapProvider() && $this->canBind($event->user)) {
2224
$event->user->setLdapUser(
2325
Resolver::byModel($event->user)
2426
);
2527
}
2628
}
2729

30+
/**
31+
* Determines if the Auth Provider is an instance of the Adldap Provider.
32+
*
33+
* @return bool
34+
*/
35+
protected function usesAdldapProvider() : bool
36+
{
37+
return Auth::getProvider() instanceof Provider;
38+
}
39+
2840
/**
2941
* Determines if we're able to bind to the user.
3042
*

tests/DatabaseTestCase.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace Adldap\Laravel\Tests;
44

55
use Adldap\Connections\Ldap;
6-
use Adldap\Schemas\ActiveDirectory;
7-
use Adldap\Laravel\Tests\Models\User;
86
use Adldap\Laravel\Auth\DatabaseUserProvider;
7+
use Adldap\Laravel\Tests\Models\User;
8+
use Adldap\Schemas\ActiveDirectory;
99
use Illuminate\Support\Facades\Schema;
1010

1111
class DatabaseTestCase extends TestCase
@@ -60,6 +60,10 @@ protected function getEnvironmentSetup($app)
6060
'driver' => 'adldap',
6161
'model' => User::class,
6262
],
63+
'users' => [
64+
'driver' => 'eloquent',
65+
'model' => User::class,
66+
],
6367
]);
6468
}
6569
}

tests/EloquentAuthenticateTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Adldap\Laravel\Tests;
4+
5+
use Adldap\Laravel\Commands\Import;
6+
use Adldap\Laravel\Facades\Resolver;
7+
use Adldap\Laravel\Tests\Models\User;
8+
use Illuminate\Support\Facades\Auth;
9+
10+
class EloquentAuthenticateTest extends DatabaseTestCase
11+
{
12+
/** @test */
13+
public function it_doenst_set_the_ldap_user_if_the_auth_provider_is_not_ldap()
14+
{
15+
$this->app['config']->set('auth.guards.web.provider', 'users');
16+
17+
$user = $this->makeLdapUser([
18+
'cn' => 'John Doe',
19+
'userprincipalname' => 'jdoe@email.com',
20+
]);
21+
22+
$importer = new Import($user, new User());
23+
24+
$model = $importer->handle();
25+
26+
Resolver::spy();
27+
Resolver::shouldNotReceive('byModel');
28+
29+
Auth::login($model);
30+
}
31+
}

0 commit comments

Comments
 (0)