Show Icon tenant If user Is_admin

using If condtion in AdminPanelProvider
No description
25 Replies
Dennis Koch
Dennis Koch7mo ago
Use closures in the config methods: ->tenant(fn () => auth()->user() ...)
Alnuaimi
Alnuaimi7mo ago
ok write on this code if (fn (): bool => auth()->user()->IS_ADMIN) { // Include the tenant section $panel = $panel ->tenant(Team::class) ->tenantRegistration(RegisterTeam::class) ->tenantProfile(EditTeamProfile::class); }
toeknee
toeknee7mo ago
please read the #✅┊rules on formatting code.
Dennis Koch
Dennis Koch7mo ago
I don't know what you want
joaopaulolndev
joaopaulolndev7mo ago
Hey @Dennis Koch I think he would like to show tenant only in one condition. But the problem is is not possible to catch auth()->user() inside AdminPanelProvider
Dennis Koch
Dennis Koch7mo ago
Well, I shared some code to try.
joaopaulolndev
joaopaulolndev7mo ago
I was trying to help, but always retorn false public function panel(Panel $panel): Panel { $isManager = fn() => auth()->check() && auth()->user()->isManager(); dd($isManager());
Dennis Koch
Dennis Koch7mo ago
It’s a service provider. There is no authenticated user. That’s why I said you need a closure
joaopaulolndev
joaopaulolndev7mo ago
Sure, I see
Alnuaimi
Alnuaimi7mo ago
fn (): bool => auth()->user()->is_admin this not work why
Dennis Koch
Dennis Koch7mo ago
Can you show the full code? Are there any errors? "not work" is not enough info to help
Alnuaimi
Alnuaimi7mo ago
I think ,It nos possible to get this on AdminPanelProvider because it’s loaded before login
No description
Dennis Koch
Dennis Koch7mo ago
Please read my answer again. You cannot simply put a Closure inside an if statement and that's not what I proposed
Alnuaimi
Alnuaimi7mo ago
ok . how do it if ((fn (): bool => auth()->user()->is_admin)) { // Include the tenant section $panel = $panel ->tenant(Team::class) ->tenantRegistration(RegisterTeam::class) ->tenantProfile(EditTeamProfile::class); } can you help me?
Dennis Koch
Dennis Koch7mo ago
Yes, I can. If you read my messages: That's the code I sent you:
->tenant(fn () => auth()->user() ...)
->tenant(fn () => auth()->user() ...)
You need to pass the Closure to the config methods on the Panel object.
Alnuaimi
Alnuaimi7mo ago
error @Dennis Koch
No description
No description
Dennis Koch
Dennis Koch7mo ago
Okay, sorry. Then it's not possible to use Closures for tenant registration. Usually we support it in most places. Maybe it doesn't make sense here, because Tenancy must be enabled before HTTP middlewares.
Alnuaimi
Alnuaimi7mo ago
ok how to show this ok how to show this or hidde if user is admin
No description
Dennis Koch
Dennis Koch7mo ago
I think it's not possible then.
LeandroFerreira
LeandroFerreira7mo ago
1 - create a middleware 2 - add the middleware in the admin provider
$panel
->middleware([
...
MyMiddleware::class,
])
$panel
->middleware([
...
MyMiddleware::class,
])
3 - in the middleware
public function handle(Request $request, Closure $next): Response
{
$panel = filament()->getCurrentPanel();

if (auth()->user()->is_admin) {
$panel
->tenant(Team::class)
->tenantRegistration(...);
...
}

return $next($request);
}
public function handle(Request $request, Closure $next): Response
{
$panel = filament()->getCurrentPanel();

if (auth()->user()->is_admin) {
$panel
->tenant(Team::class)
->tenantRegistration(...);
...
}

return $next($request);
}
Dennis Koch
Dennis Koch7mo ago
Leandro to the resuce. Yeah, makes sense 😅
Alnuaimi
Alnuaimi7mo ago
error
No description
No description
No description
LeandroFerreira
LeandroFerreira7mo ago
Not sure why it is null.. Could you share the project on the github?
Soundmit
Soundmit7mo ago
i've setup this middleware but doesn't works if i comment

// ->tenant(Team::class)
// ->tenantRegistration(RegisterTeam::class)
// ->tenantProfile(EditTeamProfile::class)
// ->tenant(Team::class, slugAttribute: 'slug')

// ->tenant(Team::class)
// ->tenantRegistration(RegisterTeam::class)
// ->tenantProfile(EditTeamProfile::class)
// ->tenant(Team::class, slugAttribute: 'slug')
in the panel i get Route [filament.admin.tenant.registration] not defined. in the panel config

->middleware([
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
AuthenticateSession::class,
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
DisableBladeIconComponents::class,
DispatchServingFilamentEvent::class,
IsTenantAdmin::class

->middleware([
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
AuthenticateSession::class,
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
DisableBladeIconComponents::class,
DispatchServingFilamentEvent::class,
IsTenantAdmin::class
and the middleware


namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use App\Filament\Pages\Tenancy\EditTeamProfile;
use App\Filament\Pages\Tenancy\RegisterTeam;
use App\Models\Team;

class IsTenantAdmin
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
$panel = filament()->getCurrentPanel();
// $tenantAdmin = auth()->user()->is_tenant_owner;
$tenantAdmin = true;
if ($tenantAdmin == true) {
$panel
->tenant(Team::class, ownershipRelationship: 'team')
->tenantRegistration(RegisterTeam::class)
->tenantProfile(EditTeamProfile::class)
->tenant(Team::class, slugAttribute: 'slug');
}

return $next($request);
}
}


namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use App\Filament\Pages\Tenancy\EditTeamProfile;
use App\Filament\Pages\Tenancy\RegisterTeam;
use App\Models\Team;

class IsTenantAdmin
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
$panel = filament()->getCurrentPanel();
// $tenantAdmin = auth()->user()->is_tenant_owner;
$tenantAdmin = true;
if ($tenantAdmin == true) {
$panel
->tenant(Team::class, ownershipRelationship: 'team')
->tenantRegistration(RegisterTeam::class)
->tenantProfile(EditTeamProfile::class)
->tenant(Team::class, slugAttribute: 'slug');
}

return $next($request);
}
}
What i want is - tenantAdmin == true can create new team - tenantAdmin == false can't create/edit manage team i can hide the menu voices with
'profile' => MenuItem::make()->label('Edit workshop profile')
->visible(fn (): bool => auth()->user()->can('manage-team')),
'profile' => MenuItem::make()->label('Edit workshop profile')
->visible(fn (): bool => auth()->user()->can('manage-team')),
but i still can access to edit page (for example)
Alnuaimi
Alnuaimi7mo ago
👍