Hi, I want to prevent non-admin users from accessing other users' tickets. Each logged-in user should only see their own tickets.
Then do that? Stop trying to shoe-horn this into tickets authorisation. As you’re finding, you can’t then authorise if there aren’t any tickets to authorise against.
The user identifier is in the route, so create some middleware that restricts access to other users:
class RestrictAccessToOwnedResources
{
public function __construct(
#[RouteParameter('user')] protected User $user,
) {}
public function handle(Request $request, Closure $next)
{
abort_unless($this->user->is($request->user()), 403);
return $next($request);
}
}
Middleware checks if the authenticated user is the user specified in the URL, and returns a 403 Forbidden response if not.
If admins should be able to see tickets of any user, then just add to the if statement:
- abort_unless($this->user->is($request->user()), 403);
+ abort_unless($this->user->is($request->user() || $request->user()->isAdmin()), 403);