chiiya / filament-access-control

Admin user, role and permission management for Laravel Filament
MIT License
192 stars 24 forks source link

Custom User model causes Adminsitration menu to be lost #44

Closed rhukster closed 4 months ago

rhukster commented 1 year ago

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.

chiiya commented 1 year ago

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

rhukster commented 1 year ago

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.

chiiya commented 11 months ago

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

class TestFilamentUser extends FilamentUser
{
    use HasApiTokens;
}
'user_model' => \App\Models\TestFilamentUser::class,
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' => 'john.doe@example.org'
        ],
    ];
    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.

arbet commented 8 months ago

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!