F
Filament•16mo ago
geisi1909

Is there a way to conditionally hide the Tenant Registration Link in the Tenant Menu?

Do you know if it is possible to hide the Tenant Registration Link in the Tenant Menu conditionally? E.g. Check if User has the Right to Create Tenants
24 Replies
Ben
Ben•16mo ago
In the registration tenant page class, override the parent mount() method. I just did this recently
class RegisterTeam extends RegisterTenant
{
public function mount(): void
{
parent::mount();

if (false) { // your condition checking
$this->redirectRoute('some fallback route');
}

// or abort on certain condition
abort_if(false, 403, 'You don\'t have access to create tenant');
}

public static function getLabel(): string
{
return 'Register team';
}

public function form(Form $form): Form
{
return $form
->schema([
TextInput::make('name')->required(),
...
]);
}
}
class RegisterTeam extends RegisterTenant
{
public function mount(): void
{
parent::mount();

if (false) { // your condition checking
$this->redirectRoute('some fallback route');
}

// or abort on certain condition
abort_if(false, 403, 'You don\'t have access to create tenant');
}

public static function getLabel(): string
{
return 'Register team';
}

public function form(Form $form): Form
{
return $form
->schema([
TextInput::make('name')->required(),
...
]);
}
}
Oh I'm sorry, I was misread your issue.
geisi1909
geisi1909OP•16mo ago
Yes but I think it is kind of a workaround but ideally i will also hide the Tenant Registration Link in the Tenant Menu.
tuto1902
tuto1902•16mo ago
I'm looking for a solution for this as well. So far, I haven't found a way to conditionally show/hide the link. @benny.dev 's workaround is a nice one. I think I may have found a way. It's not pretty but it works. Basically, create a new file in resources\views\vendor\filament-panels\components\ called tenant-menu.blade.php. To preserve all the functionality as-is, copy the contents of vendor\filament\filament\resources\views\components\tenant-menu.blade.php and paste it in the new file you just created. Then add your checks in the @php section. In my case, I wanted to hide the menu for all users except admins. This is what I did
@php
// ...
$registrationItem = $items['register'] ?? null;
$registrationItemUrl = $registrationItem?->getUrl();
$hasTenantRegistration = filament()->hasTenantRegistration() || filled($registrationItemUrl);
// Only show the tenant registration menu item if the currently logged in user role is 'admin'
$hasTenantRegistration = auth()->user()->role->name == 'admin' ? $hasTenantRegistration : false;
// ...
@endphp
@php
// ...
$registrationItem = $items['register'] ?? null;
$registrationItemUrl = $registrationItem?->getUrl();
$hasTenantRegistration = filament()->hasTenantRegistration() || filled($registrationItemUrl);
// Only show the tenant registration menu item if the currently logged in user role is 'admin'
$hasTenantRegistration = auth()->user()->role->name == 'admin' ? $hasTenantRegistration : false;
// ...
@endphp
⚠️ Warning ⚠️ I assume this may introduce breaking changes into your application in future updates. Use at your own discretion. @Dan Harrin may be able to corroborate this. Thoughts?
Dan Harrin
Dan Harrin•16mo ago
if the user doesn't have permission to create a tenant in the Policy, the link shouldnt show
tuto1902
tuto1902•16mo ago
Oh I'm going to try that. Thank you!
Dan Harrin
Dan Harrin•16mo ago
it should also throw a 403
xy
xy•16mo ago
Whats the name of policy function for this?
tuto1902
tuto1902•16mo ago
My teant model is Clinic. So I created a ClinicPolicy with php artisan make:policy ClinicPolicy. Just to be safe, I registered the new policy manually in AuthServiceProvider
protected $policies = [
Clinic::class => ClinicPolicy::class
];
protected $policies = [
Clinic::class => ClinicPolicy::class
];
Then, I added the following policy method
/**
* Determine whether the user can create models.
*/
public function create(User $user): bool
{
return false;
}
/**
* Determine whether the user can create models.
*/
public function create(User $user): bool
{
return false;
}
But the menu item still shows up. Did I not do this correctly?
tuto1902
tuto1902•16mo ago
It didn't throw a 403 either I've already deleted the /resources/views/vendor folder and cleared browser cache.
Dan Harrin
Dan Harrin•16mo ago
which v3 patch version are you on? please make sure you also delete any published views
tuto1902
tuto1902•16mo ago
In the tenant-menu.blade.php I can see how the profile page is observing tenant policies, not the case for the registration page
$registrationItem = $items['register'] ?? null;
$registrationItemUrl = $registrationItem?->getUrl();
$hasTenantRegistration = filament()->hasTenantRegistration() || filled($registrationItemUrl);

$profileItem = $items['profile'] ?? null;
$profileItemUrl = $profileItem?->getUrl();
$hasTenantProfile = (filament()->hasTenantProfile() && filament()->getTenantProfilePage()::canView($currentTenant)) || filled($profileItemUrl);
$registrationItem = $items['register'] ?? null;
$registrationItemUrl = $registrationItem?->getUrl();
$hasTenantRegistration = filament()->hasTenantRegistration() || filled($registrationItemUrl);

$profileItem = $items['profile'] ?? null;
$profileItemUrl = $profileItem?->getUrl();
$hasTenantProfile = (filament()->hasTenantProfile() && filament()->getTenantProfilePage()::canView($currentTenant)) || filled($profileItemUrl);
Is this pull request worthy? Oh, I just noticed the RegisterTenant page class doesn't have a canView() method
Dan Harrin
Dan Harrin•16mo ago
the base class should, right?
tuto1902
tuto1902•16mo ago
you mean the abstract RegisterTenant class? if so, then no. The base RegisterTenant class does not have a ::canView() method
Dan Harrin
Dan Harrin•16mo ago
it does though...
Dan Harrin
Dan Harrin•16mo ago
Dan Harrin
Dan Harrin•16mo ago
please make sure you're on the latest version
tuto1902
tuto1902•16mo ago
makes sense * hides in shame *
Dan Harrin
Dan Harrin•16mo ago
so it works correctly on the latest version, right?
tuto1902
tuto1902•16mo ago
I'm testing that now. I did php artisan filament:upgrade but I can't see the canView method still. Trying to figure out if I'm missing something
Dan Harrin
Dan Harrin•16mo ago
composer update?
tuto1902
tuto1902•16mo ago
read my mind doing that now
tuto1902
tuto1902•16mo ago
That did the trick! Menu item is gone! Woohoo! I gotta make a video about this one 🤔
Want results from more Discord servers?
Add your server