Skip to content
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

Issue in Policies #1746

Open
greatsami opened this issue Feb 21, 2024 · 1 comment
Open

Issue in Policies #1746

greatsami opened this issue Feb 21, 2024 · 1 comment

Comments

@greatsami
Copy link

Screenshot from 2024-02-21 11-44-02

Hi all,
I have an issue with the policies, I created 4 roles (owner, admin, supervisor, and user)
as you see in the image top left App/Providers/AuthServiceProvider.php
I defined Gate::before to allow the owner and admin to access all admin pages.

in the Backup module I created Module/Backup/App/Providers/AuthServiceProvider.php as bottom left page and defined Backup policy, and add it to register it as it is appears in the top right page in BackupServiceProvider.php

and defined the policies as bottom right page BackupPolicy.php

The problem in this case supervisor cant access backup page as per his permissions, but if I comment Gate::before the supervisor can access his permissions but the owner and admin can not.

Note Before I create the Backup module the permissions and policies was worked fine.

How to solve this issue?

@stale stale bot added the stale label Mar 7, 2024
Repository owner deleted a comment from stale bot Mar 12, 2024
@stale stale bot removed the stale label Mar 12, 2024
@hnasiry
Copy link

hnasiry commented Nov 27, 2024

The issue occurs because your Gate::before() method returns a boolean value instead of null. When Gate::before() returns a boolean, Laravel stops checking other policies.

To ensure Laravel continues checking policies when the user does not have the specified roles, you should return null. Here’s the corrected code:

Gate::before(function ($user, $ability) {
    return $user->hasRole(['owner', 'admin']) ? true : null;
});

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

2 participants