Checking for Auth0 Permissions from Laravel 11

While I’ve been able to check the permissions for a user via the Management API, it feels pretty hacked together. I’m fairly new to Laravel so it wouldn’t surprise me if I’m missing something obvious.

We’re using a local store for users so I can save associations with other content via a pivot table. I’m looping through the list of associated users and want to check if they have the payments:view permission assigned in Auth0 to the role they have.

This is what I have currently.

// get the user object
$profile = app('auth0')->getSdk()->management()->users()->getPermissions($user->auth0)
$profile = Auth0\Laravel\Facade\Auth0::json($profile);
// flatten the multidimensional array so I can check it
$profile = Illuminate\Support\Arr::flatten($profile);
// check for my permission in the array
$permission = in_array( 'payments:view', $profile);

// change UI
@if ( $permission )
show UI element
@else
show different element
@endif

I can of course move this off into my model as a wrapper function but I feel like I must be missing something, like I should be able to use the @can blade directive to check access without all the other work. Currently that only works for the logged in user and I have been unable to find a way to apply it to the Collection of users I’m looping through.

Hi @ProudCity,

Welcome to the Auth0 Community!

I have reviewed your code, and it looks good with using the Management API to get the user’s permissions from Auth0.

I found that the @can blade directive only works for the currently authenticated user. Therefore, it won’t be possible to use it to check the collection of user permissions.

You could consider adapting your current script to use the Management API to get the permissions for each user in a collection.

Let me know what your thoughts are on this approach.

Thanks,
Rueben

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.