View and ViewAny Permissions

I have a custom menu item on my backend which has this logic for visibility:
->visible(function () {
$location = Location::find(auth()->user()->location_id);
$canView = auth()->user()->can('view', $location);
$canViewAll = auth()->user()->can('viewAny', Location::class);
return $canView && !$canViewAll;
})
->visible(function () {
$location = Location::find(auth()->user()->location_id);
$canView = auth()->user()->can('view', $location);
$canViewAll = auth()->user()->can('viewAny', Location::class);
return $canView && !$canViewAll;
})
I did this so it doesn't show for super admins, who are able to see the actual page with all of the locations. This seems to work, but when I'm in as a role who has view permissions but not viewAny and I click on the menu item, I get a 403 page. I can't seem to figure out why I can't view the location page. This is my location policy:
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user): bool
{
return $user->can('view_any_location');
}

/**
* Determine whether the user can view the model.
*/
public function view(User $user, Location $location): bool
{
return $user->can('view_location');
}
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user): bool
{
return $user->can('view_any_location');
}

/**
* Determine whether the user can view the model.
*/
public function view(User $user, Location $location): bool
{
return $user->can('view_location');
}
Eventually I'd want to change this so it restricts it to only being able to view their location, but right now it's just not letting me access the page or any of the locations. Any help here?
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?