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

Commit e19ce8d

Browse files
committed
Added local database login fallback option
- Closes #26
1 parent d03c18f commit e19ce8d

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

src/AdldapAuthUserProvider.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ public function retrieveByCredentials(array $credentials)
7575
}
7676
}
7777

78+
if ($this->getLoginFallback()) {
79+
// Login failed. If login fallback is enabled
80+
// we'll call the eloquent driver.
81+
return parent::retrieveByCredentials($credentials);
82+
}
83+
7884
return;
7985
}
8086

@@ -107,7 +113,7 @@ protected function getModelFromAdldap(User $user, $password)
107113
$model = $this->createModel()->newQuery()->where([$key => $username])->first();
108114

109115
// Create the model instance of it isn't found.
110-
if (!$model) {
116+
if (!$model instanceof Model) {
111117
$model = $this->createModel();
112118
}
113119

@@ -376,4 +382,15 @@ protected function getSelectAttributes()
376382
{
377383
return Config::get('adldap_auth.select_attributes', []);
378384
}
385+
386+
/**
387+
* Retrieves the Adldap login fallback option for falling back
388+
* to the local database if AD authentication fails.
389+
*
390+
* @return bool
391+
*/
392+
protected function getLoginFallback()
393+
{
394+
return Config::get('adldap_auth.login_fallback', false);
395+
}
379396
}

src/Config/auth.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@
2121

2222
'username_attribute' => ['username' => 'samaccountname'],
2323

24+
/*
25+
|--------------------------------------------------------------------------
26+
| Login Fallback
27+
|--------------------------------------------------------------------------
28+
|
29+
| The login fallback option allows you to login as a user located on the
30+
| local database if active directory authentication fails.
31+
|
32+
| Set this to true if you would like to enable it.
33+
|
34+
*/
35+
36+
'login_fallback' => false,
37+
2438
/*
2539
|--------------------------------------------------------------------------
2640
| Password Key

tests/AdldapTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Adldap\Models\User;
77
use Illuminate\Support\Facades\App;
88
use Illuminate\Support\Facades\Auth;
9+
use Adldap\Laravel\Tests\Models\User as EloquentUser;
910
use Mockery;
1011

1112
class AdldapTest extends FunctionalTestCase
@@ -155,7 +156,7 @@ public function testCredentialsKeyDoesNotExist()
155156

156157
Adldap::shouldReceive('users')->once()->andReturn($mockedUsers);
157158

158-
$nonExistantInputKey = 'non-existant-key';
159+
$nonExistantInputKey = 'non-existent-key';
159160

160161
$this->setExpectedException('ErrorException');
161162

@@ -174,4 +175,23 @@ public function testConfigCallbackAttributeHandler()
174175

175176
$this->assertEquals('handled', $user->name);
176177
}
178+
179+
public function testConfigLoginFallback()
180+
{
181+
$this->app['config']->set('adldap_auth.login_fallback', true);
182+
183+
EloquentUser::create([
184+
'email' => 'jdoe@email.com',
185+
'name' => 'John Doe',
186+
'password' => bcrypt('Password123'),
187+
]);
188+
189+
$outcome = Auth::attempt(['email' => 'jdoe@email.com', 'password' => 'Password123']);
190+
191+
$user = \Auth::user();
192+
193+
$this->assertTrue($outcome);
194+
$this->assertInstanceOf('Adldap\Laravel\Tests\Models\User', $user);
195+
$this->assertEquals('jdoe@email.com', $user->email);
196+
}
177197
}

0 commit comments

Comments
 (0)