DirectoryTree / LdapRecord-Laravel

Multi-domain LDAP Authentication & Management for Laravel.
https://ldaprecord.com/docs/laravel/v3
MIT License
496 stars 52 forks source link

[Bug] Error 1054 Unkown column 'mail' in 'where clause' #438

Closed TheSoulGamer158 closed 2 years ago

TheSoulGamer158 commented 2 years ago

Environment: LDAP Server Type: OpenLDAP LdapRecord-Laravel Major Version: v.2 PHP Version: 8.0.11

Describe the bug: Hey there me again.. Your solution yesterday worked perfectly but for now if I try to login with a user, I get this error

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'mail' in 'where clause'
select * from `users` where `mail` = user01@ahdrive.local limit 1

I'm pretty sure that my Model doesn't work as it should be, maybe a little error I can't find. Here my Ldap\User.php:


namespace App\Ldap;

use LdapRecord\Laravel\Auth\LdapAuthenticatable;
use LdapRecord\Models\Concerns\CanAuthenticate;
use LdapRecord\Models\OpenLDAP\User as OpenLDAPUser;

class User extends OpenLDAPUser implements LdapAuthenticatable
{
    use CanAuthenticate;

    public static $objectClasses = [
        'top',
        'person',
        'organizationalperson',
        'user',
        'account'
    ];

My web.php:

Route::get('/dashboard', function () {
    return view('dashboard');
})->middleware(['auth:ldap,web'])->name('dashboard');

And my LoginRequest.php:

public function authenticate()
    {
        $this->ensureIsNotRateLimited();

        $credentials = [
            'mail' => $this->email,
            'password' => $this->password,
        ];

        if (! Auth::attempt($credentials, $this->boolean('remember'))) {
            RateLimiter::hit($this->throttleKey());

            throw ValidationException::withMessages([
                'email' => trans('auth.failed'),
            ]);
        }

        RateLimiter::clear($this->throttleKey());
    }

If I'm correct the error points to this config in my auth.php right?

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

  'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],
        'ldap' => [
            'driver' => 'ldap',
            'model' => App\LDAP\User::class,
            'rules' => [],
            'database' => [
                'model' => App\LDAP\User::class,
                'sync_passwords' => true,
                'sync_attributes' => [
                    'name' => 'cn',
                    'email' => 'mail',
                ],
            ],
        ], 

Would you be so kind and help me once again? :D

stevebauman commented 2 years ago

Hi @TheSoulGamer158, you’ve set the database.model config option to the same LDAP\User::class. This should instead be your Eloquent user model (either App\User::class or App\Models\User::class).

Give that a shot and it should resolve your issue 👍

TheSoulGamer158 commented 2 years ago

I edited like you said to this:

 'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],
        'ldap' => [
            'driver' => 'ldap',
            'model' => App\LDAP\User::class,
            'rules' => [],
            'database' => [
                'model' => App\Models\User::class,
                'sync_passwords' => true,
                'sync_attributes' => [
                    'name' => 'cn',
                    'email' => 'mail',
                ],
            ],
        ],

Even with it I get this error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'mail' in 'where clause' select * fromuserswheremail= user01@ahdrive.local limit 1

If I edit the authenticate() within the LoginRequest.php like this I don't get the error but can't find a user.

 public function authenticate()
    {
        $this->ensureIsNotRateLimited();

//        $credentials = [
//            'mail' => $this->email,
//            'password' => $this->password,
//        ];

        if (! Auth::attempt($this->only('email', 'password'), $this->boolean('remember'))) {
            RateLimiter::hit($this->throttleKey());

            throw ValidationException::withMessages([
                'email' => trans('auth.failed'),
            ]);
        }

        RateLimiter::clear($this->throttleKey());
    }
stevebauman commented 2 years ago

Hi @TheSoulGamer158,

You've defined an ldap guard inside of your config/auth.php file, but are using the default web guard. Ensure that your defaults.guard option inside of your config/auth.php file is set to ldap.

Also, the credentials array should be mail and password. Not email and password (you had it correct previously).

Fixing those above issues should allow you to authenticate. Closing for now. Let me know if you encounter further issues.