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] Provider key password_column not working #669

Closed guill94 closed 1 week ago

guill94 commented 3 weeks ago

Environment:

LDAP Server Type: ActiveDirectory Laravel 11 LdapRecord 3.3 PHP Version: 8.3 Sql Server

Describe the bug :

I am using the "password_column" key in the provider to redefine the column of the password (in USR_PASSWORD). Changed it in Model App\Models\User and in database of course. (It works perfectly with the column named password)

My problem is that I get an Sql Server error saying password column is invalid, like the key in the provier is not taken into account (I cleared cached the config). The thing really strange though, if I change the column in database to something else then I get the SQL server error USR_PASSWORD column is invalid which is true in this case but this time it did get the value defined inside the provider.

It seems like when the value define in provier and the column name in database are the same it keeps "password" as column name and ignore what's in the provider.

Here's my provider :

'users' => [ 'driver' => 'ldap', 'model' => LdapRecord\Models\ActiveDirectory\User::class, 'rules' => [], 'scopes' => [], 'database' => [ 'model' => App\Models\User::class, 'sync_passwords' => true, 'sync_attributes' => [ 'USR_NAME' => 'cn', 'USR_USERNAME' => 'samaccountname', 'USR_EMAIL' => 'mail', ], 'sync_existing' => [ 'USR_EMAIL' => 'mail', ], 'password_column' => 'USR_PASSWORD', ],

stevebauman commented 3 weeks ago

Hi @guill94,

Can you post the stack trace of the exception?

guill94 commented 3 weeks ago

Hi @stevebauman With everything set to USR_PASSWORD I have :

SQLSTATE[42S22]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Nom de colonne non valide : 'password'. (Connection: sqlsrv, SQL: update [users] set [password] = $2y$12$RY8FW7WIvj43wO6, [users].[updated_at] = 2024-20-08 16:02:30 where [id] = 2)

If I change in database column name to "pass" for example :

SQLSTATE[42S22]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Nom de colonne non valide : 'USR_PASSWORD'. (Connection: sqlsrv, SQL: update [users] set [USR_PASSWORD] = $2y$12$RY8FW7WIvj43wO6, [users].[updated_at] = 2024-20-08 16:05:25 where [id] = 2)

guill94 commented 3 weeks ago

laravel.log

stevebauman commented 3 weeks ago

@guill94 I'm not sure I'm understanding correctly. These exceptions indicate that the columns password and USR_PASSWORD are missing from your database table. The query displays that the USR_PASSWORD column (which is defined in your provided configuration via the password_column key) is correctly being populated with a password.

Can you elaborate on the issue here?

guill94 commented 3 weeks ago

@stevebauman In fact when I define in the provider the column as 'USR_PASSWORD' I get the error 'password' column not valid and of course cause in the database I have the column USR_PASSWORD. At that point I thought that the key in the provider was not taken into account as it executes the query UPDATE Users SET 'password' [...] instead of SET 'USR_PASSWORD'.

The second error is just a test I'd say, I randomly noticed when I jus change the value of the column in my database to 'pass' for exemple I get the error 'USR_PASSWORD' not valid and the query is right based on the key in provider (update Users SET 'USR_PASSWORD' [...])

So to summarize it's like when I get the same value for the column in my database and the key in provider the query executed uses the column 'password' so the default whereas it should be 'USR_PASSWORD' and the simple action of changing the name of my column in database it executes the right query with column 'USR_PASSWORD' which is really strange. Maybe I'm missing something.

stevebauman commented 3 weeks ago

@guill94 I think you may also need to override the getAuthPasswordName method on your Eloquent User model. Can you give this a shot?

// app/Models/User.php

class User extends Authenticatable
{
    public function getAuthPasswordName(): string
    {
        return 'USR_PASSWORD';
    }    
}
guill94 commented 2 weeks ago

@stevebauman Thanks, that was the problem. I didn't see this in documentation (Laravel version) did I miss it?

stevebauman commented 2 weeks ago

Excellent! Glad to hear it resolved the problem @guill94. It's mentioned here in the Laravel authentication documentation:

https://laravel.com/docs/11.x/authentication#the-authenticatable-contract

I'll add this into the LdapRecord-Laravel documentation and close this once complete 👍

stevebauman commented 1 week ago

Ok I've added a note in the documentation!

https://ldaprecord.com/docs/laravel/v3/auth/database/configuration/#laravel--11