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

Commit 38de0cf

Browse files
committed
More documentation.
1 parent a2ef8f5 commit 38de0cf

File tree

5 files changed

+210
-212
lines changed

5 files changed

+210
-212
lines changed

docs/auth/binding.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Binding the Adldap2 User Model to the Laravel User Model
2+
3+
> **Note**: Before we begin, enabling this option will perform a single query on your AD server for a logged in user
4+
**per request**. Eloquent already does this for authentication, however this could lead to slightly longer load times
5+
(depending on your AD server and network speed of course).
6+
7+
To begin, insert the `Adldap\Laravel\Traits\HasLdapUser` trait onto your `User` model:
8+
9+
```php
10+
namespace App;
11+
12+
use Adldap\Laravel\Traits\HasLdapUser;
13+
use Illuminate\Database\Eloquent\SoftDeletes;
14+
use Illuminate\Foundation\Auth\User as Authenticatable;
15+
16+
class User extends Authenticatable
17+
{
18+
use SoftDeletes, HasLdapUser;
19+
```
20+
21+
Now, after you've authenticated a user via the `adldap` driver, their LDAP model is available on their `User` model:
22+
23+
```php
24+
if (Auth::attempt($credentials)) {
25+
$user = Auth::user();
26+
27+
var_dump($user); // Returns instance of App\User;
28+
29+
var_dump($user->ldap); // Returns instance of Adldap\Models\User;
30+
31+
// Examples:
32+
33+
$user->ldap->getGroups();
34+
35+
$user->ldap->getCommonName();
36+
37+
$user->ldap->getConvertedSid();
38+
}
39+
```

docs/auth/fallback.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Login Fallback
2+
3+
The login fallback option allows you to login as a local database user using the Eloquent authentication driver if
4+
active directory authentication fails. This option would be handy in environments where:
5+
6+
- You may have some active directory users and other users registering through the website itself (user does not exist in your AD).
7+
- Local development where your AD server may be unavailable
8+
9+
To enable it, simply set the option to true in your `config/adldap_auth.php` configuration file:
10+
11+
```php
12+
'login_fallback' => false, // Set to true.
13+
```
14+
15+
## Developing Locally without an AD connection
16+
17+
You can continue to develop and login to your application without a connection to your AD server in the following scenario:
18+
19+
* You have `auto_connect` set to `false` in your `adldap.php` configuration
20+
> This is necessary so we don't automatically try and bind to your AD server when your application boots.
21+
22+
* You have `login_fallback` set to `true` in your `adldap_auth.php` configuration
23+
> This is necessary so we fallback to the standard `eloquent` auth driver.
24+
25+
* You have `password_sync` set to `true` in your `adldap_auth.php` configuration
26+
> This is necessary so we can login to the account with the last password that was used when an AD connection was present.
27+
28+
* You have logged into the synchronized LDAP account previously
29+
> This is necessary so the account actually exists in your local app's database.
30+
31+
If you have this configuration, you will have no issues developing an
32+
application without a persistent connection to your LDAP server.

docs/auth/multiple-connections.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Using Multiple LDAP Connections
2+
3+
To swap connections on the fly, set your configurations default connection and try re-authenticating the user:
4+
5+
```php
6+
$auth = false;
7+
8+
if (Auth::attempt($credentials)) {
9+
$auth = true; // Logged in successfully
10+
} else {
11+
// Login failed, swap and try other connection.
12+
Config::set('adldap_auth.connection', 'other-connection');
13+
14+
if (Auth::attempt($credentials)) {
15+
$auth = true; // Passed logging in with other connection.
16+
}
17+
}
18+
19+
if ($auth === true) {
20+
return redirect()
21+
->to('dashboard')
22+
->with(['message' => 'Successfully logged in!']);
23+
}
24+
25+
return redirect()
26+
->to('login')
27+
->with(['message' => 'Your credentials are incorrect.']);
28+
```
29+
30+
Or, if you'd like to all of your LDAP connections:
31+
32+
```php
33+
$connections = config('adldap.connections');
34+
35+
foreach ($connections as $connection => $config) {
36+
37+
// Set the LDAP connection to authenticate with.
38+
config(['adldap_auth.connection' => $connection]);
39+
40+
if (Auth::attempt($credentials)) {
41+
return redirect()
42+
->to('dashboard')
43+
->with(['message' => 'Successfully logged in!']);
44+
}
45+
}
46+
47+
return redirect()
48+
->to('login')
49+
->with(['message' => 'Your credentials are incorrect.']);
50+
```

docs/auth/syncing.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Syncing Attributes
2+
3+
Inside your `config/adldap_auth.php` file there is a configuration option named `sync_attributes`. This
4+
is an array of attributes where the key is the eloquent `User` model attribute, and the
5+
value is the active directory users attribute.
6+
7+
By default, the `User` models `email` and `name` attributes are synchronized to
8+
the LDAP users `userprincipalname` and `cn` attributes.
9+
10+
This means, upon login, the users `email` and `name` attribute on Laravel `User` Model will be set to the
11+
LDAP users `userprincipalname` and common name (`cn`) attribute, **then saved**.
12+
13+
Feel free to add more attributes here, however be sure that your `users` database table
14+
contains the key you've entered, otherwise you will receive a SQL exception.
15+
16+
## Sync Attribute Handlers
17+
18+
If you're looking to synchronize an attribute from an Adldap2 model that contains an array or an
19+
object, or sync attributes yourself, you can use an attribute handler class
20+
to sync your model attributes manually. For example:
21+
22+
> **Note**: The class must contain a `handle()` method. Otherwise you will receive an exception.
23+
24+
> **Tip**: Attribute handlers are constructed using the `app()` helper. This means you can type-hint any application
25+
> dependencies you may need in the handlers constructor.
26+
27+
```php
28+
'sync_attributes' => [
29+
30+
App\Handlers\LdapAttributeHandler::class,
31+
32+
],
33+
```
34+
35+
The `LdapAttributeHandler`:
36+
37+
```php
38+
namespace App\Handlers;
39+
40+
use App\User as EloquentUser;
41+
use Adldap\Models\User as LdapUser;
42+
43+
class LdapAttributeHandler
44+
{
45+
/**
46+
* Synchronizes ldap attributes to the specified model.
47+
*
48+
* @param LdapUser $ldapUser
49+
* @param EloquentUser $eloquentUser
50+
*
51+
* @return void
52+
*/
53+
public function handle(LdapUser $ldapUser, EloquentUser $eloquentUser)
54+
{
55+
$eloquentUser->name = $ldapUser->getCommonName();
56+
}
57+
}
58+
```
59+
60+
## Password Synchronization
61+
62+
The password sync option allows you to automatically synchronize
63+
users AD passwords to your local database. These passwords are
64+
hashed natively by laravel.
65+
66+
Enabling this option would also allow users to login to their
67+
accounts using the password last used when an AD connection
68+
was present.
69+
70+
If this option is disabled, the local user account is applied
71+
a random 16 character hashed password, and will lose access
72+
to this account upon loss of AD connectivity.
73+
74+
This feature is enabled by default.
75+
76+
```php
77+
'password_sync' => env('ADLDAP_PASSWORD_SYNC', true),
78+
```

0 commit comments

Comments
 (0)