Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Add column name customisation options for Auth used with database provider #51162

Closed

Conversation

Daniel-H123
Copy link
Contributor

@Daniel-H123 Daniel-H123 commented Apr 22, 2024

This pull request will add:

This pr will add the option to customise column names, in the users table when using the Auth login functions in combination with the database driver. Column name customisation is also possible when eloquent is used (by overriding the getter functions in the users model, but there is no way to do it when the database provider is used.

How to use:

Users can customise the column names by editing the providers config. My first thought was to add a new 'database' key, that is commented out by default, but since 'table' is already located in providers, it seems best to place it there.

By default, the eloquent provider is used, but when the database provider is used, you can easily replace the column names in the config.

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

        'users' => [
            'driver' => 'database',
            'table' => 'users',
            // The columns config is optional, to be backwards compatible. 
            // When not set, default column names will be used.
            'columns' => [
                'primary_key' => 'id',
                'password' => 'password',
                'remember_token' => 'remember_token',
            ],
        ],
    ],

@Daniel-H123
Copy link
Contributor Author

Daniel-H123 commented Apr 22, 2024

Updated the pr to pass the config to the GenericUser via the 'right' way. Not realy sure where the default values should be defined, but I did it in the GenericUser class.

Setting a default is necessary for backwards compatibility, because this update is depending on a change in the auth.php config. If it should be done different, let me know.

/**
 * Create a new generic User object.
 *
 * @param  array  $attributes
 * @param  array|null  $columns
 * @return void
 */
public function __construct(array $attributes, array $columns = null)
{
    $this->attributes = $attributes;
    $this->columns = $columns ?? [
        'primary_key' => 'id',
        'password' => 'password',
        'remember_token' => 'remember_token',
    ];
}

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If possible, please consider releasing your code as a package so that the community can still take advantage of your contributions!

If you feel absolutely certain that this code corrects a bug in the framework, please "@" mention me in a follow-up comment with further explanation so that GitHub will send me a notification of your response.

@Daniel-H123
Copy link
Contributor Author

@devfrey Can you elaborate on your feedback? I feel like the database auth provider is lacking customisation, even if it isn't used as much as the eloquent method.

Is there a better way to implement it, or do you think column name customisation just doesn't add enough value?

@devfrey
Copy link
Contributor

devfrey commented Apr 23, 2024

@Daniel-H123 The user providers can already be customized or extended. Simply implement UserProvider or extend the DatabaseUserProvider and make changes as necessary. There's no need to further complicate the existing implementation just for your edge case. See https://laravel.com/docs/11.x/authentication#adding-custom-user-providers for further guidance.

@Daniel-H123
Copy link
Contributor Author

I see. I agree that it makes the implementation more complicated. Thanks for explaining.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants