Skip to content

scopePermission returns all models when no role/permission assigned #2831

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
iamlasse opened this issue Apr 10, 2025 · 2 comments
Open

scopePermission returns all models when no role/permission assigned #2831

iamlasse opened this issue Apr 10, 2025 · 2 comments

Comments

@iamlasse
Copy link

Description

When I use the scope permission, if I have no permissions assigned to a role, it returns all models. Expected would be no models. This is in use along with TightenCo Parental.

Steps To Reproduce

Reporter::query()->permission(Permission::IMPERSONATE)->toRawSql()

"select * from "users" where (exists (select * from "permissions" inner join "model_has_permissions" on "permissions"."id" = "model_has_permissions"."permission_id" where "users"."id" = "model_has_permissions"."model_id" and "model_has_permissions"."model_type" = 'user' and "permissions"."id" in (32)) or exists (select * from "roles" inner join "model_has_roles" on "roles"."id" = "model_has_roles"."role_id" where "users"."id" = "model_has_roles"."model_id" and "model_has_roles"."model_type" = 'user' and "roles"."id" in (2, 3, 18, 16, 6, 17, 19, 1, 7, 8, 4, 5, 9, 10, 11, 12, 13, 14, 15, 20, 21, 22, 23, 24))) and "users"."deleted_at" is null and "users"."type" = 'reporter' and not "type" = 'system'"

Image
Permission 32 is not assigned to any role.

Example Application

No response

Version of spatie/laravel-permission package:

6.16.0

Version of laravel/framework package:

11.44.0

PHP version:

8.3

Database engine and version:

Postgres 16/17

OS: Windows/Mac/Linux version:

Mac os Sequoia 15

@iamlasse
Copy link
Author

With further investigation, Permission::find(32)->roles returns correct result, however when called inside the scope it does not, and returns all roles...

@iamlasse
Copy link
Author

$rolesWithPermissions = is_a($this, Role::class) ? [] : array_unique(
array_reduce($permissions, function ($result, $permission) {
return array_merge($result, $permission->roles()->get()->all());
}, [])
);

This seems to fix the issue of roles

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant