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

Commit 7fe2d34

Browse files
committed
Merge remote-tracking branch 'origin/master'
# Conflicts: # src/Resolvers/UserResolver.php
2 parents 67d6de5 + d37b67c commit 7fe2d34

30 files changed

+579
-182
lines changed

docs/auth/events.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Events
2+
3+
There are several events fired during each operation that takes place in Adldap2-Laravel.
4+
5+
| Event | Fired | Limitations |
6+
|----------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------|
7+
| `Adldap\Laravel\Events\AuthenticatedModelTrashed` | When the configured authentication rule `Adldap\Laravel\Validation\Rules\DenyTrashed` is used, this event will be fired when a user has passed LDAP authentication, but their Eloquent model has been soft deleted. | |
8+
| `Adldap\Laravel\Events\AuthenticatedWithCredentials` | When a user successfully passes LDAP authentication. | |
9+
| `Adldap\Laravel\Events\AuthenticatedWithWindows` | When a user has authenticated successfully using the `WindowsAuthenticate` middleware. | |
10+
| `Adldap\Laravel\Events\DiscoveredWithCredentials` | When a users LDAP record has been successfully been found. Fired before LDAP authentication. | |
11+
| `Adldap\Laravel\Events\Importing` | When an LDAP user is being imported for the first time. Not fired on subsequent logins after being imported. Use the `Synchronized` / `Synchronizing` event for this purpose. | Only fired when using the `DatabaseUserProvider` |
12+
| `Adldap\Laravel\Events\Synchronizing` | When an LDAP users attributes are being synchronized. Fired on every authentication attempt after the LDAP user has been located. | Onlyfired when using the `DatabaseUserProvider` |
13+
| `Adldap\Laravel\Events\Synchronized` | When an LDAP users attributes have been fully synchronized. Fired on every authentication attempt after the LDAP user has been located. | Onlyfired when using the `DatabaseUserProvider` |
14+

docs/auth/scopes.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ Now that we've created our scope (`app/Scopes/AccountingScope.php`), we can inse
6666
Once you've inserted your scope into the configuration file, you will now only be able
6767
to authenticate with users that are a member of the `Accounting` group.
6868

69-
All other users will be denied authentication to your app,
70-
even if their credentials are valid.
69+
All other users will be denied authentication, even if their credentials are valid.
7170

7271
> **Note**: If you're caching your configuration files, make sure you run `php artisan config:clear`.

docs/auth/syncing.md

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

33
Inside your `config/adldap_auth.php` file there is a configuration option named `sync_attributes`. This
44
is an array of attributes where the key is the eloquent `User` model attribute, and the
5-
value is the active directory users attribute.
5+
value is the active directory users attribute:
6+
7+
```php
8+
'sync_attributes' => [
9+
10+
'email' => 'userprincipalname',
11+
12+
'name' => 'cn',
13+
],
14+
```
615

716
By default, the `User` models `email` and `name` attributes are synchronized to
817
the LDAP users `userprincipalname` and `cn` attributes.

readme.md

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,12 @@ To use Adldap2-Laravel, your application and server must meet the following requ
4343

4444
## Installation
4545

46-
Insert Adldap2-Laravel into your `composer.json` file:
47-
48-
```json
49-
"adldap2/adldap2-laravel": "3.0.*",
50-
```
51-
52-
Or via command line:
46+
Run the following command:
5347

5448
```bash
5549
composer require adldap2/adldap2-laravel
5650
```
5751

58-
Then run `composer update`.
59-
6052
Once finished, insert the service provider in your `config/app.php` file:
6153
```php
6254
Adldap\Laravel\AdldapServiceProvider::class,

src/AdldapAuthServiceProvider.php

Lines changed: 46 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22

33
namespace Adldap\Laravel;
44

5+
use Adldap\AdldapInterface;
56
use InvalidArgumentException;
6-
use Adldap\Laravel\Facades\Adldap;
77
use Adldap\Laravel\Resolvers\UserResolver;
88
use Adldap\Laravel\Resolvers\ResolverInterface;
99
use Adldap\Laravel\Commands\Console\Import;
1010
use Adldap\Laravel\Auth\DatabaseUserProvider;
11-
use Adldap\Laravel\Auth\NoDatabaseUserProvider;
12-
use Adldap\Laravel\Listeners\BindsLdapUserModel;
1311
use Illuminate\Support\Facades\Auth;
1412
use Illuminate\Support\Facades\Event;
1513
use Illuminate\Support\Facades\Config;
@@ -28,7 +26,6 @@ public function boot()
2826
{
2927
$config = __DIR__.'/Config/auth.php';
3028

31-
// Add publishable configuration.
3229
$this->publishes([
3330
$config => config_path('adldap_auth.php'),
3431
], 'adldap');
@@ -38,16 +35,12 @@ public function boot()
3835
$auth = Auth::getFacadeRoot();
3936

4037
if (method_exists($auth, 'provider')) {
41-
// If the provider method exists, we're running Laravel >= 5.2
42-
// Register the adldap auth user provider.
4338
$auth->provider('adldap', function ($app, array $config) {
44-
return $this->newUserProvider($app['hash'], $config);
39+
return $this->makeUserProvider($app['hash'], $config);
4540
});
4641
} else {
47-
// Otherwise we're using 5.0 || 5.1
48-
// Extend Laravel authentication with Adldap driver.
4942
$auth->extend('adldap', function ($app) {
50-
return $this->newUserProvider($app['hash'], $app['config']['auth']);
43+
return $this->makeUserProvider($app['hash'], $app['config']['auth']);
5144
});
5245
}
5346

@@ -84,7 +77,9 @@ public function provides()
8477
protected function registerBindings()
8578
{
8679
$this->app->bind(ResolverInterface::class, function () {
87-
return $this->newUserResolver();
80+
$ad = $this->app->make(AdldapInterface::class);
81+
82+
return new UserResolver($ad);
8883
});
8984
}
9085

@@ -95,7 +90,27 @@ protected function registerBindings()
9590
*/
9691
protected function registerListeners()
9792
{
98-
Event::listen(Authenticated::class, BindsLdapUserModel::class);
93+
// Here we will register the event listener that will bind the users LDAP
94+
// model to their Eloquent model upon authentication (if configured).
95+
// This allows us to utilize their LDAP model right
96+
// after authentication has passed.
97+
Event::listen(Authenticated::class, Listeners\BindsLdapUserModel::class);
98+
99+
if ($this->isLogging()) {
100+
// If logging is enabled, we will set up our event listeners that
101+
// log each event fired throughout the authentication process.
102+
Event::listen(Events\Importing::class, Listeners\LogImport::class);
103+
Event::listen(Events\Synchronized::class, Listeners\LogSynchronized::class);
104+
Event::listen(Events\Synchronizing::class, Listeners\LogSynchronizing::class);
105+
Event::listen(Events\Authenticated::class, Listeners\LogAuthenticated::class);
106+
Event::listen(Events\Authenticating::class, Listeners\LogAuthentication::class);
107+
Event::listen(Events\AuthenticationFailed::class, Listeners\LogAuthenticationFailure::class);
108+
Event::listen(Events\AuthenticationRejected::class, Listeners\LogAuthenticationRejection::class);
109+
Event::listen(Events\AuthenticationSuccessful::class, Listeners\LogAuthenticationSuccess::class);
110+
Event::listen(Events\DiscoveredWithCredentials::class, Listeners\LogDiscovery::class);
111+
Event::listen(Events\AuthenticatedWithWindows::class, Listeners\LogWindowsAuth::class);
112+
Event::listen(Events\AuthenticatedModelTrashed::class, Listeners\LogTrashedModel::class);
113+
}
99114
}
100115

101116
/**
@@ -104,69 +119,39 @@ protected function registerListeners()
104119
* @param Hasher $hasher
105120
* @param array $config
106121
*
107-
* @return \Illuminate\Contracts\Auth\UserProvider
108-
*
109122
* @throws InvalidArgumentException
123+
*
124+
* @return \Illuminate\Contracts\Auth\UserProvider
110125
*/
111-
protected function newUserProvider(Hasher $hasher, array $config)
126+
protected function makeUserProvider(Hasher $hasher, array $config)
112127
{
113-
$provider = $this->userProvider();
128+
$provider = Config::get('adldap_auth.provider', DatabaseUserProvider::class);
114129

115-
switch ($provider) {
116-
case DatabaseUserProvider::class:
117-
if (array_key_exists('model', $config)) {
118-
return new $provider($hasher, $config['model']);
119-
}
130+
// The DatabaseUserProvider has some extra dependencies needed,
131+
// so we will validate that we have them before
132+
// constructing a new instance.
133+
if ($provider == DatabaseUserProvider::class) {
134+
$model = array_get($config, 'model');
120135

136+
if (!$model) {
121137
throw new InvalidArgumentException(
122138
"No model is configured. You must configure a model to use with the {$provider}."
123139
);
124-
case NoDatabaseUserProvider::class:
125-
return new $provider;
126-
}
140+
}
127141

128-
throw new InvalidArgumentException(
129-
"The configured Adldap provider [{$provider}] is not supported or does not exist."
130-
);
131-
}
132-
133-
/**
134-
* Returns a new user resolver.
135-
*
136-
* @return ResolverInterface
137-
*/
138-
protected function newUserResolver()
139-
{
140-
return new UserResolver($this->ldapProvider());
141-
}
142-
143-
/**
144-
* Retrieves a connection provider from the Adldap instance.
145-
*
146-
* @return \Adldap\Connections\ProviderInterface
147-
*/
148-
protected function ldapProvider()
149-
{
150-
return Adldap::getProvider($this->connection());
151-
}
152-
153-
/**
154-
* Returns the configured user provider class.
155-
*
156-
* @return string
157-
*/
158-
protected function userProvider()
159-
{
160-
return Config::get('adldap_auth.provider', DatabaseUserProvider::class);
142+
return new $provider($hasher, $model);
143+
}
144+
145+
return new $provider;
161146
}
162147

163148
/**
164-
* Returns the configured default connection name.
149+
* Determines if authentication requests are logged.
165150
*
166-
* @return string
151+
* @return bool
167152
*/
168-
public function connection()
153+
protected function isLogging()
169154
{
170-
return Config::get('adldap_auth.connection', 'default');
155+
return Config::get('adldap_auth.logging', false);
171156
}
172157
}

src/Auth/DatabaseUserProvider.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use Adldap\Laravel\Facades\Resolver;
77
use Adldap\Laravel\Commands\Import;
88
use Adldap\Laravel\Commands\SyncPassword;
9+
use Adldap\Laravel\Events\AuthenticationRejected;
10+
use Adldap\Laravel\Events\AuthenticationSuccessful;
911
use Adldap\Laravel\Events\DiscoveredWithCredentials;
1012
use Adldap\Laravel\Events\AuthenticatedWithCredentials;
1113
use Illuminate\Support\Facades\Bus;
@@ -131,15 +133,19 @@ public function validateCredentials(Authenticatable $model, array $credentials)
131133
// validation rules pass, we will allow the authentication
132134
// attempt. Otherwise, it is automatically rejected.
133135
if ($this->passesValidation($this->user, $model)) {
134-
// Sync / set the users password since it has been verified.
136+
// Here we can now synchronize / set the users password since
137+
// they have successfully passed authentication
138+
// and our validation rules.
135139
Bus::dispatch(new SyncPassword($model, $credentials));
136140

137-
// All of our validation rules have passed and we can
138-
// finally save the model in case of changes.
139141
$model->save();
140142

143+
Event::fire(new AuthenticationSuccessful($this->user));
144+
141145
return true;
142146
}
147+
148+
Event::fire(new AuthenticationRejected($this->user));
143149
}
144150

145151
if ($this->isFallingBack() && $model->exists) {

src/Auth/NoDatabaseUserProvider.php

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

55
use Adldap\Laravel\Facades\Resolver;
6+
use Adldap\Laravel\Events\AuthenticationRejected;
7+
use Adldap\Laravel\Events\AuthenticationSuccessful;
68
use Adldap\Laravel\Events\DiscoveredWithCredentials;
79
use Adldap\Laravel\Events\AuthenticatedWithCredentials;
810
use Illuminate\Support\Facades\Event;
@@ -57,14 +59,16 @@ public function retrieveByCredentials(array $credentials)
5759
*/
5860
public function validateCredentials(Authenticatable $user, array $credentials)
5961
{
60-
// Perform LDAP authentication and validate the authenticated model.
61-
if (
62-
Resolver::authenticate($user, $credentials) &&
63-
$this->passesValidation($user)
64-
) {
62+
if (Resolver::authenticate($user, $credentials)) {
6563
Event::fire(new AuthenticatedWithCredentials($user));
6664

67-
return true;
65+
if ($this->passesValidation($user)) {
66+
Event::fire(new AuthenticationSuccessful($user));
67+
68+
return true;
69+
}
70+
71+
Event::fire(new AuthenticationRejected($user));
6872
}
6973

7074
return false;

0 commit comments

Comments
 (0)