Feb 25, 2024
0
Level 2
Restricted access to multi tenancy features based on registration status
Understanding the Problem:
-
Users with "individual" registration status are redirected to the tenant registration page
RegisterTenantwhich they shouldn't have access to. -
I want to prevent this redirection and redirect them to their appropriate dashboard.
What I did:
- I modify the handleRegistration() to:
- Check the user's registration status before creating a tenant.
- If
individualredirect them to the dashboard usingreturn redirect()->route('filament.app.pages.dashboard');. - If
team_ownerproceed with tenant creation and user attachment.
The code does not work as expected I still get access to filament.app.tenant.registration even my registration status is individual
My Current Codes:
2014_10_12_000000_create_users_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('users', function (Blueprint $table) {
...
$table->string('registration_status')->default('individual');
...
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('users');
}
};
app/Models/User.php
protected $fillable = [
'registration_status',
];
app/Filament/App/Pages/Tenancy/RegisterTenant.php
<?php
namespace App\Filament\App\Pages\Tenancy;
use App\Models\Tenant;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Pages\Tenancy\RegisterTenant as BaseRegisterTenant;
class RegisterTenant extends BaseRegisterTenant
{
protected static string $view = 'filament.app.pages.tenancy.register-tenant';
public static function getLabel(): string
{
return __('Register Team');
}
public function form(Form $form): Form
{
return $form
->schema([
TextInput::make('name')
->required(),
TextInput::make('slug')
->required(),
]);
}
protected function handleRegistration(array $data): Tenant
{
$user = auth()->user();
if ($user && $user->registration_status === 'team_owner') {
$tenant = Tenant::create($data);
$tenant->users()->attach($user);
return $tenant;
} else {
return redirect()->route('filament.app.pages.dashboard');
}
}
}
Please or to participate in this conversation.