F
Filament3mo ago
Bryan

Filament Shield Have a Default Role

hi everyone i just wondering how to make it posible when the user register to the panel he/she can have a default role value as a user
8 Replies
vhicxs
vhicxs3mo ago
Isn't it by default that when there is a new registration you automatically get Panel_user access rights?
Bryan
Bryan3mo ago
i want to point it in another type of user role
JustNoOne
JustNoOne3mo ago
create a observer
micraux
micraux3mo ago
Hi, You can use the boot() function in your User model:
protected static function boot(): void
{
parent::boot();

static::creating(
function($record)
{
// add your logic here...
}
);
}
protected static function boot(): void
{
parent::boot();

static::creating(
function($record)
{
// add your logic here...
}
);
}
toeknee
toeknee3mo ago
As JustNoOne pointed out, you need an observer, when user is created check for roles, if none exist assign one. You have to assing one to the user and not just set a global default really. Or add the role to the user creation as normal which has the default.
truthzeeker
truthzeeker3mo ago
If you have many roles and have a specific user role for that panel, you need to extend the Register class and assign it to your panel, then override the register method and add your role assignment.
...
use Filament\Pages\Auth\Register as BaseRegister;

class RegisterPage extends BaseRegister
{

// Override the register method
public function register(): ?RegistrationResponse
{
try {
$this->rateLimit(2);
} catch (TooManyRequestsException $exception) {
Notification::make()
->title(__('filament-panels::pages/auth/register.notifications.throttled.title', [
'seconds' => $exception->secondsUntilAvailable,
'minutes' => ceil($exception->secondsUntilAvailable / 60),
]))
->body(array_key_exists('body', __('filament-panels::pages/auth/register.notifications.throttled') ?: []) ? __('filament-panels::pages/auth/register.notifications.throttled.body', [
'seconds' => $exception->secondsUntilAvailable,
'minutes' => ceil($exception->secondsUntilAvailable / 60),
]) : null)
->danger()
->send();

return null;
}

$data = $this->form->getState();

$user = $this->getUserModel()::create($data);

event(new \Filament\Events\Auth\Registered($user));

$user->assignRole('your role'); // assign role here

$this->sendEmailVerificationNotification($user);

Filament::auth()->login($user);

session()->regenerate();

return app(RegistrationResponse::class);
}
}
...
use Filament\Pages\Auth\Register as BaseRegister;

class RegisterPage extends BaseRegister
{

// Override the register method
public function register(): ?RegistrationResponse
{
try {
$this->rateLimit(2);
} catch (TooManyRequestsException $exception) {
Notification::make()
->title(__('filament-panels::pages/auth/register.notifications.throttled.title', [
'seconds' => $exception->secondsUntilAvailable,
'minutes' => ceil($exception->secondsUntilAvailable / 60),
]))
->body(array_key_exists('body', __('filament-panels::pages/auth/register.notifications.throttled') ?: []) ? __('filament-panels::pages/auth/register.notifications.throttled.body', [
'seconds' => $exception->secondsUntilAvailable,
'minutes' => ceil($exception->secondsUntilAvailable / 60),
]) : null)
->danger()
->send();

return null;
}

$data = $this->form->getState();

$user = $this->getUserModel()::create($data);

event(new \Filament\Events\Auth\Registered($user));

$user->assignRole('your role'); // assign role here

$this->sendEmailVerificationNotification($user);

Filament::auth()->login($user);

session()->regenerate();

return app(RegistrationResponse::class);
}
}
And in your panel
->registration(RegisterPage::class)
->registration(RegisterPage::class)
Let me know if it works for you.
Trasek
Trasek3mo ago
Something like that:
protected static function booted(): void
{
static::creating(function (User $user) {
$user->assignRole('user role');
});
}
protected static function booted(): void
{
static::creating(function (User $user) {
$user->assignRole('user role');
});
}
darrenglanville
darrenglanville3mo ago
For basic logic, this makes sense. Observers can be used for more in depth tasks
Want results from more Discord servers?
Add your server