Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tempura's avatar

[Suggestions] Checking authorization rights by "guard" instead of simple "roles"

I have two guard that shares the same controller.

Long story short: I need to identify rights by guards.

My thoughts currently are:

  • Check if instance of Foo model. (Not sure if this is the best way)
  • To make it dynamic, make use of config (first idea was set a key/value but...)
  • Remembered that auth config has provider which also has a model

Now I could just write that down as a private method on my current controller but I have a feeling that I'll be using this more than once.

Where could I possible assign that method? In my Models by having a contract? But I have this feeling that I am violating SRP here.

I am also thinking if Auth class is the right one. Something like

Auth::method($request->user(), 'guardName') OR Auth::method($request->user()) === config()

Looking for some opinions/suggestions. Anyone?

EDIT

Currently using while not yet decided

$user = $request->user();
$class_name = config('auth.providers.someProvider.model');
return $user instanceof $class_name;
0 likes
0 replies

Please or to participate in this conversation.