Filament::getCurrentPanel() always returns true

I am writing a policy to prevent the challenger from editing the duel once he has created it. Here is the controller code:
/**
* Show the form for editing the specified resource.
*/
public function edit(duel $duel)
{
// get the current player's characters count
$count = $this->countActiveCharacters(auth()->user()->id);

// if the player doesn't have any characters
if ($count === 0) {

// display an error instead
return inertia('Duels/NoActiveCharacters');
}

// if the duel was already completed
if($duel->status === STATUS::COMPLETED->value) {
// display a message letting them know
return inertia('Duels/Completed');
}

//lazy load the challenger relationship
$duel->loadMissing('challenger:id,name');

// get offensive moves
$offensive_moves = Move::where('move_type', MoveType::OFFENSIVE)->get(['id', 'name']);

// get defensive moves
$defensive_moves = Move::where('move_type', MoveType::DEFENSIVE)->get(['id', 'name']);

return inertia('Duels/Edit', [
'duel' => fn() => new DuelResource($duel),
'offensive_moves' => fn() => MoveResource::collection($offensive_moves),
'defensive_moves' => fn() => MoveResource::collection($defensive_moves),
]);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(duel $duel)
{
// get the current player's characters count
$count = $this->countActiveCharacters(auth()->user()->id);

// if the player doesn't have any characters
if ($count === 0) {

// display an error instead
return inertia('Duels/NoActiveCharacters');
}

// if the duel was already completed
if($duel->status === STATUS::COMPLETED->value) {
// display a message letting them know
return inertia('Duels/Completed');
}

//lazy load the challenger relationship
$duel->loadMissing('challenger:id,name');

// get offensive moves
$offensive_moves = Move::where('move_type', MoveType::OFFENSIVE)->get(['id', 'name']);

// get defensive moves
$defensive_moves = Move::where('move_type', MoveType::DEFENSIVE)->get(['id', 'name']);

return inertia('Duels/Edit', [
'duel' => fn() => new DuelResource($duel),
'offensive_moves' => fn() => MoveResource::collection($offensive_moves),
'defensive_moves' => fn() => MoveResource::collection($defensive_moves),
]);
}
Solution:
Since you’re not checking against the panelId anyway maybe Filament::isServing() works for you.
Jump to solution
7 Replies
Ookma-Kyi
Ookma-KyiOP2mo ago
However it seems the user is able to access the route no matter what. Here is the policy code:
/**
* Determine whether the user can update the model.
*/
public function update(User $user, Duel $duel): bool
{
if (Filament::getCurrentPanel() && $user->hasRole('admin')) {
Log::info('Panel accessed!');
Log::info('Panel: ' . Filament::getCurrentPanel()->getId());
return true;
}

Log::info('user id: ' . $user->id);

Log::info('opponent id: ' . $duel->opponent->user->id);

return $user->id === $duel->opponent->user->id;
}
/**
* Determine whether the user can update the model.
*/
public function update(User $user, Duel $duel): bool
{
if (Filament::getCurrentPanel() && $user->hasRole('admin')) {
Log::info('Panel accessed!');
Log::info('Panel: ' . Filament::getCurrentPanel()->getId());
return true;
}

Log::info('user id: ' . $user->id);

Log::info('opponent id: ' . $duel->opponent->user->id);

return $user->id === $duel->opponent->user->id;
}
I managed to track it down to this bit of code:
if (Filament::getCurrentPanel() && $user->hasRole('admin')) {
if (Filament::getCurrentPanel() && $user->hasRole('admin')) {
Adding a bit of logging it seems even if I access the route outside the admin panel Filament::getCurrentPanel() always returns true. To be more specific the line Log::info('Panel: ' . Filament::getCurrentPanel()->getId()); returns admin even if I am accessing the route itself and not in the admin panel. I can't tell if this is a bug with Filament so I am posting it here. Thanks
toeknee
toeknee2mo ago
Is admin panel the default? If so it could be falling back to the first panel in list if no panel is currently selected. I would need to check the getCurrentPanel code to be sure.
Ookma-Kyi
Ookma-KyiOP2mo ago
I found this bug report https://github.com/filamentphp/filament/issues/14840 that shows others have the same issue as I do. Here a link to my repository with the Filament resources currently implemented https://bitbucket.org/ookma-kyi/ookma-kyi-core/src/main/app/Filament/Resources/. The question now is what is the workaround for the moment?
GitHub
filament()->getCurrentPanel() always returns a panel, even outsid...
Package filament/filament Package Version v3 Laravel Version v11 Livewire Version v3 PHP Version PHP 8.3 Problem description When serving a response outside of Filament, filament()->getCurrentPa...
toeknee
toeknee2mo ago
There isn’t one, you just have to condition it correctly. Dans changed it in V4 if you want to back-port the code.
Ookma-Kyi
Ookma-KyiOP2mo ago
So what would be the correct condition or is backporting it the only way for now?
Solution
awcodes
awcodes2mo ago
Since you’re not checking against the panelId anyway maybe Filament::isServing() works for you.
Ookma-Kyi
Ookma-KyiOP2mo ago
That seems to work for the moment and gives the desired behavior. 👍

Did you find this page helpful?