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

Commit be6f8c8

Browse files
committed
Added configuration option for binding Adldap user to laravel model
1 parent b004ec1 commit be6f8c8

File tree

6 files changed

+52
-2
lines changed

6 files changed

+52
-2
lines changed

src/AdldapAuthUserProvider.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ protected function getModelFromAdldap(User $user, $password)
8484
$model->save();
8585
}
8686

87+
if($this->getBindUserToModel()) {
88+
$model->adldapUser = $user;
89+
}
90+
8791
return $model;
8892
}
8993

@@ -135,6 +139,17 @@ protected function getLoginAttribute()
135139
return Config::get('adldap_auth.login_attribute', 'samaccountname');
136140
}
137141

142+
/**
143+
* Retrieves the Adldap bind user to model config option for binding
144+
* the Adldap user model instance to the laravel model.
145+
*
146+
* @return bool
147+
*/
148+
protected function getBindUserToModel()
149+
{
150+
return Config::get('adldap_auth.bind_user_to_model', false);
151+
}
152+
138153
/**
139154
* Retrieves the Adldap sync attributes for filling the
140155
* Laravel user model with active directory fields.

src/Config/auth.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,25 @@
1515

1616
'login_attribute' => 'samaccountname',
1717

18+
/*
19+
|--------------------------------------------------------------------------
20+
| Bind User to Model
21+
|--------------------------------------------------------------------------
22+
|
23+
| The bind User to Model option allows you to access the Adldap user model
24+
| instance on your laravel database model to be able run operations
25+
| or retrieve extra attributes on the Adldap user model instance.
26+
|
27+
| If this option is true, you must insert the trait:
28+
|
29+
| `Adldap\Laravel\Traits\AdldapUserModelTrait`
30+
|
31+
| Onto your User model configured in `config/auth.php`.
32+
|
33+
*/
34+
35+
'bind_user_to_model' => false,
36+
1837
/*
1938
|--------------------------------------------------------------------------
2039
| Sync Attributes

src/Traits/AdldapUserModelTrait.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Adldap\Laravel\Traits;
4+
5+
trait AdldapUserModelTrait
6+
{
7+
/**
8+
* The Adldap User that is bound to the current model.
9+
*
10+
* @var \Adldap\Models\User
11+
*/
12+
public $adldapUser;
13+
}

tests/AdldapTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ public function testAuthPasses()
5151

5252
$this->assertTrue(Auth::attempt(['mail' => '[email protected]', 'password' => '12345']));
5353

54-
$user = \DB::table('users')->where('id', '=', 1)->first();
54+
$user = Auth::user();
5555

5656
$this->assertEquals('[email protected]', $user->email);
5757
$this->assertTrue(\Hash::check('12345', $user->password));
58+
$this->assertEquals($adUser, $user->adldapUser);
5859
}
5960

6061
public function testAuthFails()

tests/FunctionalTestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ protected function getEnvironmentSetup($app)
3737
]);
3838

3939
$app['config']->set('adldap.auto_connect', false);
40+
$app['config']->set('adldap_auth.bind_user_to_model', true);
4041

4142
$app['config']->set('auth.driver', 'adldap');
4243
$app['config']->set('auth.model', 'Adldap\Laravel\Tests\Models\User');

tests/Models/User.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Adldap\Laravel\Tests\Models;
44

5+
use Adldap\Laravel\Traits\AdldapUserModelTrait;
56
use Illuminate\Auth\Authenticatable;
67
use Illuminate\Database\Eloquent\Model;
78
use Illuminate\Auth\Passwords\CanResetPassword;
@@ -10,7 +11,7 @@
1011

1112
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
1213
{
13-
use Authenticatable, CanResetPassword;
14+
use Authenticatable, CanResetPassword, AdldapUserModelTrait;
1415

1516
/**
1617
* The database table used by the model.

0 commit comments

Comments
 (0)