Adldap2 / Adldap2-Laravel

LDAP Authentication & Management for Laravel
MIT License
911 stars 184 forks source link

Does not bind LDAP user to model with custom guard #775

Open jschwendener opened 4 years ago

jschwendener commented 4 years ago

First of all, thank you for this awesome package!

Description:

We run a project with two separate user tables and auth providers (the Laravel default users for the admin panel, and a 'members' table for frontend users).

We are using the Adldap2 package for authenticating only our frontend users, which are set up with a custom guard frontend in config/auth.php.

Everything went super smooth (we use the DatabaseUserProvider and import users to our DB), also authentication works 👍.

The only problem we have now is, that the LDAP user is not being bound to our Laravel member model. The HasLdapUser trait is set on the model. $user->ldap stays null after a successful auth attempt.

I found out that in the BindsLdapUserModel class, you are checking for a 'guard' property on the event object.

// We'll retrieve the auth guard if available.
if (property_exists($event, 'guard')) {
   $guard = $event->guard;
}

dd($guard) // returns null

Binding actually works if I manually set $guard = 'frontend'; in this file. So now my question is, how does the event even get a guard property? Am I missing something?

Our LoginController is more or less untouched and uses the Laravel default logic. Only thing we do is override the guard method to return our custom guard:

protected function guard() {
   return Auth::guard('frontend');
}

Config

ldap_auth.php

return [

    'connection' => env('LDAP_CONNECTION', 'default'),
    'provider' => Adldap\Laravel\Auth\DatabaseUserProvider::class,
    'model' => RF\Member\Member::class,
    'rules' => [
        Adldap\Laravel\Validation\Rules\DenyTrashed::class,
    ],

    'scopes' => [
    ],

    'identifiers' => [
        'ldap' => [
            'locate_users_by' => 'mail',
            'bind_users_by' => 'dn',
        ],

        'database' => [
            'guid_column' => 'objectguid',
            'username_column' => 'email',
        ],

        'windows' => [
            'locate_users_by' => 'samaccountname',
            'server_key' => 'AUTH_USER',
        ],
    ],

    'passwords' => [
        'sync' => env('LDAP_PASSWORD_SYNC', false),
        'column' => 'password',
    ],

    'login_fallback' => env('LDAP_LOGIN_FALLBACK', false),
    'sync_attributes' => [
        'email' => 'mail',
        'name' => 'cn',
    ],

    'logging' => [
        'enabled' => env('LDAP_LOGGING', true),
        'events' => [
            \Adldap\Laravel\Events\Importing::class                 => \Adldap\Laravel\Listeners\LogImport::class,
            \Adldap\Laravel\Events\Synchronized::class              => \Adldap\Laravel\Listeners\LogSynchronized::class,
            \Adldap\Laravel\Events\Synchronizing::class             => \Adldap\Laravel\Listeners\LogSynchronizing::class,
            \Adldap\Laravel\Events\Authenticated::class             => \Adldap\Laravel\Listeners\LogAuthenticated::class,
            \Adldap\Laravel\Events\Authenticating::class            => \Adldap\Laravel\Listeners\LogAuthentication::class,
            \Adldap\Laravel\Events\AuthenticationFailed::class      => \Adldap\Laravel\Listeners\LogAuthenticationFailure::class,
            \Adldap\Laravel\Events\AuthenticationRejected::class    => \Adldap\Laravel\Listeners\LogAuthenticationRejection::class,
            \Adldap\Laravel\Events\AuthenticationSuccessful::class  => \Adldap\Laravel\Listeners\LogAuthenticationSuccess::class,
            \Adldap\Laravel\Events\DiscoveredWithCredentials::class => \Adldap\Laravel\Listeners\LogDiscovery::class,
            \Adldap\Laravel\Events\AuthenticatedWithWindows::class  => \Adldap\Laravel\Listeners\LogWindowsAuth::class,
            \Adldap\Laravel\Events\AuthenticatedModelTrashed::class => \Adldap\Laravel\Listeners\LogTrashedModel::class,
        ],
    ],
];
rummanrc commented 3 years ago

Hi, did you manage to solve this? I'm also facing a similar issue right now.

stevebauman commented 3 years ago

Hi @jschwendener, can you post your config/auth.php file?