filament-access-control
filament-access-control copied to clipboard
Custom User model causes Adminsitration menu to be lost
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.
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
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.
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.
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!