LdapRecord-Laravel: [Bug] LdapUserModel is null when calling a user

Environment (please complete the following information):

  • LDAP Server Type: ActiveDirectory
  • LdapRecord-Laravel Major Version: v2 (latest)
  • PHP Version: 7.3
  • Laravel: 8

Describe the bug: Hey,

i’m currently testing the ldap authentication over a laravel api (sanctum). The authentication over ldap, sanctum and the sync of the attributes works but when calling the ldap property on the user model the value is null.

My UserModel:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use LdapRecord\Laravel\Auth\AuthenticatesWithLdap;
use LdapRecord\Laravel\Auth\HasLdapUser;
use LdapRecord\Laravel\Auth\LdapAuthenticatable;

class User extends Authenticatable implements LdapAuthenticatable
{
    use HasFactory, Notifiable, HasApiTokens, AuthenticatesWithLdap, HasLdapUser;

    ...
}

My auth.php

<?php

return [
    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
    ],

    'providers' => [
        // 'users' => [
        //     'driver' => 'eloquent',
        //     'model' => App\Models\User::class,
        // ],

        'users' => [
            'driver' => 'ldap',
            'model' => LdapRecord\Models\ActiveDirectory\User::class,
            'rules' => [
                App\Ldap\Rules\OnlyCompanyRule::class,
            ],
            'database' => [
                'model' => App\Models\User::class,
                'sync_passwords' => true,
                'sync_attributes' => [
                    'firstname' => 'givenname',
                    'lastname' => 'sn',
                    'email' => 'mail',
                    'username' => 'samaccountname'
                ],
                'password_column' => 'password',
            ],
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],
];

My test route:

Route::group(['middleware' => ['auth:sanctum']], function () {
    Route::get('/me', function (Request $request) {
        dd($request->user(), $request->user()->ldap);
    });
});

Did i miss something? Thanks!

Greetz

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 30 (15 by maintainers)

Commits related to this issue

Most upvoted comments

Haha no worries @CmdrSharp! I’m so glad that you’re up and running now! 🎉

Apologies for the late reply.

I’ve just released v2.3.0 that contains this patch. Run composer update and you’re good to go!

@stevebauman Gosh darnit. That of course solved it immediately. I never thought to change it initially, as I wasn’t doing stuff like this on the user model to start with - and as such, I never needed to go beyond the users provider for API calls.

Hopefully the original issue here was resolved anyway. I’ll just crawl away with my tail between my legs 😉

Happy to help @manuprieto!

@CmdrSharp

That’s fair! Doesn’t sound too bad to me. Though I completely understand that trial and error never feels like a proper solution 😃

Yea exactly – that’s really my only concern.

I’m going to do some digging & testing later today (and this week) with Sanctum so I can have this resolved for you guys. Hoping that we can have this feature work without any configuration on your end 👍

I think I’m being a little over-zealous with the “expensive” claim – we’d really only be iterating through each guard, creating their user provider, and attempting an LDAP query if the user provider is an instance of the LdapRecord DatabaseUserProvider.

That’s fair! Doesn’t sound too bad to me. Though I completely understand that trial and error never feels like a proper solution 😃

To know your use-case better, you mentioned in an earlier comment that the recent update wasn’t hitting the new switch case for the $guard variable. When you visit that route that you created which is covered by the sanctum guard middleware, does this not return true?

Route::group(['middleware' => ['auth:sanctum']], function () {
    Route::get('/me', function (Request $request) {
        dd(auth('sanctum')->check());
    });
});

It does return true.

Hi @stevebauman!

thanks for the replay - yes sure.

The $guard value is "sanctum" and when calling getCurrentAuthProvider it returns.

https://github.com/DirectoryTree/LdapRecord-Laravel/blob/781c7854cf4dee3c2c86b6a0649694482c8f5cdd/src/Auth/CreatesUserProvider.php#L30-L35

Because the functions looks for "auth.guards.sanctum.provider" and this is not defined in the auth.php file. When i overwrite the $guard to web i get the ldap model.

Greetz