Role-based registration where users see resources and records based on their selected role. Investors can choose brokers with whom they don't have portfolios yet.
In the Panel Provider we enable registration and pass an action as our own custom registration page.
app/Providers/Filament/AdminPanelProvider.php:
use App\Filament\Pages\Auth\Register; class AdminPanelProvider extends PanelProvider{ public function panel(Panel $panel): Panel { return $panel ->default() ->id('admin') ->path('admin') ->login() ->registration(Register::class) // ... }}
The registration page must extend the registration page from the Filament. In the register page, we add a radio select field for the role and set the default role to the Broker
.
App/Filament/Pages/Auth/Register.php:
use Filament\Schemas\Schema;use App\Models\Role;use Filament\Forms\Components\Radio;use Filament\Forms\Components\Field;use Filament\Forms\Components\Component;use Filament\Auth\Pages\Register as BaseRegister; class Register extends BaseRegister{ public function form(Schema $schema): Schema { return $schema ->components([ $this->getNameFormComponent(), $this->getEmailFormComponent(), $this->getPasswordFormComponent(), $this->getPasswordConfirmationFormComponent(), $this->getRoleRadioFormComponent(), ]); } protected function getRoleRadioFormComponent(): Field { return Radio::make('role_id') ->label('Select role') ->required() ->inline() ->exists(Role::class, 'id') ->options(Role::pluck('name', 'id')) ->default(function (Radio $component): int { return array_search('Broker', $component->getOptions()); }); } }
The user belongs to a role. Permissions are managed in the Policies by simply checking the user's role name.