Adldap2 / Adldap2-Laravel

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

Column not found: 1054 Unknown column 'username' in 'where clause' #414

Closed rnrstar closed 7 years ago

rnrstar commented 7 years ago

Laravel Version: v5.5.19 Adldap2-Laravel Version: v3.0.5 PHP Version: 7.0

Description:

After creating a test route, the error I'm receiving is as follows:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'username' in 'where clause' (SQL: select * from users where username = todd@cfusacorp.com and users.deleted_at is null limit 1)

Steps To Reproduce:

I've created a route in Routes/web.php as follows:

Route::get('/test',function () {
$username = '[username]';
$password = '[secret]';

 if(Adldap::auth()->attempt($username, $password, $bindAsUser = true)) {
            echo "Worked";
 }
$auth = Auth::attempt(['username'=>$username,
                  'password'=>$password]);
dd($auth);
});

When I don't have the Auth::attempt in there, the Adldap::auth() seems to work as it shows "Worked" when loading the page. However, the Auth::attempt() generates the above error.

My user migration looks like this:

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
        $table->string('ipphone');
        $table->string('infinitylogin');
            $table->rememberToken();
            $table->timestamps();
        });
    }

As you can see, there is no username column but as I understand it, there shouldn't be.

My adldap.php file looks like this:

return [
    'connections' => [
        'default' => [
            'auto_connect' => true,
            'connection' => Adldap\Connections\Ldap::class,
            'schema' => Adldap\Schemas\ActiveDirectory::class,
            'connection_settings' => [
                'account_prefix' => env('ADLDAP_ACCOUNT_PREFIX', ''),
                'account_suffix' => env('ADLDAP_ACCOUNT_SUFFIX', ''),
                'domain_controllers' => explode(' ', env('ADLDAP_CONTROLLERS', '[domain controller]')),
                'port' => env('ADLDAP_PORT', 389),
                'timeout' => env('ADLDAP_TIMEOUT', 5),
                'base_dn' => env('ADLDAP_BASEDN', 'dc=internal,dc=domain,dc=com'),
                'admin_account_suffix' => env('ADLDAP_ADMIN_ACCOUNT_SUFFIX', ''),
                'admin_username' => env('ADLDAP_ADMIN_USERNAME', '[admin username]'),
                'admin_password' => env('ADLDAP_ADMIN_PASSWORD', '[admin secret]'),
                'follow_referrals' => false,
                'use_ssl' => false,
                'use_tls' => true,
            ],
        ],
    ],
];

My adldap_auth.php file looks like this:

return [
    'connection' => env('ADLDAP_CONNECTION', 'default'),
    'provider' => Adldap\Laravel\Auth\DatabaseUserProvider::class,
    'resolver' => Adldap\Laravel\Auth\Resolver::class,
    'importer' => Adldap\Laravel\Auth\Importer::class,
    'rules' => [
        // Denys deleted users from authenticating.
        Adldap\Laravel\Validation\Rules\DenyTrashed::class,
        // Allows only manually imported users to authenticate.
        // Adldap\Laravel\Validation\Rules\OnlyImported::class,
    ],
    'scopes' => [
        // Only allows users with a user principal name to authenticate.
        Adldap\Laravel\Scopes\UpnScope::class,
    ],
    'usernames' => [
        'ldap' => 'userprincipalname',
        'eloquent' => 'email',
    'login_fallback' => env('ADLDAP_LOGIN_FALLBACK', false),
    'password_sync' => env('ADLDAP_PASSWORD_SYNC', true),
    'windows_auth_attribute' => ['samaccountname' => 'AUTH_USER'],
    'sync_attributes' => [
        'email' => 'userprincipalname',
        'name' => 'cn',
    'infinitylogin' => 'infinitylogin',
    'ipphone' => 'ipPhone',
    ],
];
stevebauman commented 7 years ago

Hi @rnrstar, this is because you're using username as the array key when passing the users credentials through Auth::attempt() when you're actually using email as your Laravel username key.

This:

$auth = Auth::attempt(['username'=> $username, 'password'= $password]);

Should be corrected to:

$auth = Auth::attempt(['email'=> $username, 'password'=>$password]);

Give that a shot, and if that doesn't correct your issue I'll re-open this.

Thanks :)

rnrstar commented 7 years ago

Yes, that was it. Thanks.

stevebauman commented 7 years ago

Great, no problem.