filament-access-control icon indicating copy to clipboard operation
filament-access-control copied to clipboard

Custom User model causes Adminsitration menu to be lost

Open rhukster opened this issue 1 year ago • 4 comments

To implement Laravel Sanctum and specifically the Filament Sanctum plugin (https://github.com/devtical/filament-sanctum) I need to add the HasApiTokens trait to the FilamentUser model.

I have tried the two approaches supported for custom user models, but the simplest for me is to use extend the FilamentUser base class. This works fine, however, as soon as I set a custom user_model class in the configuration, the entire "Administration" section in the side panel disappears. It seems the FilamentUserResource is only rendering properly when the original user model is specified.

rhukster avatar Aug 31 '23 13:08 rhukster

Are your permissions set up correctly? e.g. the admin user you created has the necessary permissions to view the admin sections? If you've changed your user model after creating the admin user the relationship in model_has_roles will probably be wrong

chiiya avatar Sep 25 '23 11:09 chiiya

Permssions are good, i've not touched the user model class. This is a vanilla install of Filament plus the filemant-access-control plugin to support the separate user/admin accounts.

rhukster avatar Sep 26 '23 15:09 rhukster

Sorry for the late response, been busy lately. I've tried to replicate this but it's working correctly for me:

  • Installed Filament + Sanctum + Filament Access Control
  • Created a custom user class that extends the base user class:
class TestFilamentUser extends FilamentUser
{
    use HasApiTokens;
}
  • Updated the config to use this custom user class:
'user_model' => \App\Models\TestFilamentUser::class,
  • I also have a seeder that creates some base users and assigns them the Super Admin role:
use App\Models\Category;
use App\Models\TestFilamentUser;
use App\Models\Role;
use Chiiya\FilamentAccessControl\Database\Seeders\FilamentAccessControlSeeder;
use Chiiya\FilamentAccessControl\Enumerators\RoleName;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\PermissionRegistrar;

class AdminSeeder extends Seeder
{
    public static array $users = [
        [
            'first_name' => 'John',
            'last_name' => 'Doe',
            'email' => '[email protected]'
        ],
    ];
    public static array $permissions = [];

    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $this->call(FilamentAccessControlSeeder::class);

        /** @var Role $role */
        $role = Role::findByName(RoleName::SUPER_ADMIN, 'filament');

        foreach (self::$users as $user) {
            $password = config('app.admin_password');
            $admin = TestFilamentUser::query()->create(array_merge($user, [
                'password' => Hash::make($password ?: Str::random(40)),
                'expires_at' => now()->addMonths(12),
            ]));
            $admin->assignRole($role);
        }

        foreach (self::$permissions as $permission) {
            Permission::query()->create([
                'name' => $permission,
                'guard_name' => 'filament',
            ]);
            $role->givePermissionTo($permission);
        }

        app(PermissionRegistrar::class)->forgetCachedPermissions();
    }
}

Afterwards I can login with the created user and have access to the admin sections.

chiiya avatar Oct 10 '23 11:10 chiiya

For whoever is reading, the problem happens if you change user model after creating users, if you manually change the field model_has_roles.model_type in the db to point to your custom model, e.g. App\Models\CustomUser, the problem resolves.

@chiiya a minor bug, but something you might want to fix in future version. Your plugin rocks btw!

arbet avatar Jan 26 '24 06:01 arbet