Understanding roles and permissions

I'm trying to use modifyQueryUsing in the resource before the table what i want to achive is - show all the records to user with the Super Admin or Manager roles (NOT WORKS) - show records to user only if he is the author (WORKS) - show records to supervisor (responsible in my case) (WORKS) now i have this code

$userId = auth()->id(); //recupero l'id dell'utente
$userRoles = auth()->user()->roles->pluck('name'); //recupero i ruoli dell'utente
return $table
->modifyQueryUsing(
fn (Builder $query) => $query->where(function ($query) use ($userId, $userRoles) {
$query->where('user_id', $userId) // if the user is the author of the request WORKS
->orWhereHas('project.responsible', function ($query) use ($userId) {
$query->where('user_id', $userId); // if the user is the project responsible WORKS
})->orWhereIn('Super Admin', $userRoles); // if the user is a super admin DOESN'T WORKS
})
)

$userId = auth()->id(); //recupero l'id dell'utente
$userRoles = auth()->user()->roles->pluck('name'); //recupero i ruoli dell'utente
return $table
->modifyQueryUsing(
fn (Builder $query) => $query->where(function ($query) use ($userId, $userRoles) {
$query->where('user_id', $userId) // if the user is the author of the request WORKS
->orWhereHas('project.responsible', function ($query) use ($userId) {
$query->where('user_id', $userId); // if the user is the project responsible WORKS
})->orWhereIn('Super Admin', $userRoles); // if the user is a super admin DOESN'T WORKS
})
)
the last orWhereIn doesn't works any help is appreciated
1 Reply
Soundmit
SoundmitOP11mo ago
this solution works, is the best solution possible?

->modifyQueryUsing(
fn (Builder $query) => $query->when(auth()->user()->hasRole('super_admin') === false, function ($query) use ($userId) {
$query->where('user_id', $userId) // if the user is the author of the request
->orWhereHas('project.responsible', function ($query) use ($userId) {
$query->where('user_id', $userId); // if the user is the project responsible
});
})
)

->modifyQueryUsing(
fn (Builder $query) => $query->when(auth()->user()->hasRole('super_admin') === false, function ($query) use ($userId) {
$query->where('user_id', $userId) // if the user is the author of the request
->orWhereHas('project.responsible', function ($query) use ($userId) {
$query->where('user_id', $userId); // if the user is the project responsible
});
})
)
Want results from more Discord servers?
Add your server