RobTrehy / LaravelUserPreferences

A package for Laravel to store user preferences for your project
MIT License
2 stars 0 forks source link

Manage preferences for multi guard application #6

Open theVannu opened 3 years ago

theVannu commented 3 years ago

Hi all, I would like to describe a possible (little) problem and propose possible solutions Consider following scenario:

We have a multi guard application with the following auth.php configuration

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

        'admin' => [
            'driver' => 'session',
            'provider' => 'admin'
        ],
    ],

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

        'admin' => [
           'driver' => 'eloquent',
           'model' => App\Models\Admin::class,
        ],
    ],

As you can se a "logged-user" can be a user or an admin Suppose that we want use this package for manage the preference of both type of users, so we have a column prefernces on table users and admin. I think we have two possible strategies:

1) Simply create a before middleware and set configuration user-preferences.php at runtime, for example if we want manage the preferences for Admin we set the configuration file as:

'database' => [
        'table' => 'admin',
        'column' => 'preferences',
        'primary_key' => 'id'
    ],
    'cache' => [
        'prefix' => 'user-admin-',  //Very important!!!, change the prefix and suffix cache name
        'suffix' => '-preferences-admin',
    ],
    'defaults' => [
        // 'Set the Default Preferences for Admin
        // 'key' => 'value'
    ]

As you can see this strategy doesn't need modify to the package code.

2) Second strategy, change the configuration file in order to manage multi guard application, for example:

<?php

return [
    'web' => [
      'database' => [
        'table' => 'users',
        'column' => 'preferences',
        'primary_key' => 'id'
      ],
      'cache' => [
        'prefix' => 'user-',
        'suffix' => '-preferences',
      ],
      'defaults' => [
        // 'Default Preferences go here
        // 'key' => 'value'
      ]
    ],

   'admin' => [... set the configuration for guard admin ...];
];

So in the class UserPreferences,php we can check the guard and choose the right configuration (this is a breaking change!).

The question is, what is the better strategy in your opinion? Change the package code? Or simply write some DOC in order to show some example on "How to resolve the multi-guard problem" ?

Thank you, Gianluca.

dircm commented 2 months ago

If anyone comes across an issue like this one. A general approach for "dynamic values" in config files is to set the desired configuration items at runtime like this:

Example for Admin:

config([
    'user-preferences.database.table' => 'admin',
    'user-preferences.cache.prefix' => 'user-admin-',
    'user-preferences.cache.suffix' => '-preferences-admin',
]);

To achieve this I would create a new Listener that reacts to the Illuminate\Auth\Events\Login

Determine what type of login this is and then set the configuration appropriately.

Ref: https://laravel.com/docs/11.x/events#generating-events-and-listeners