Skip to content

[Bug] Avoid checking permissions-via-roles on Role model (ref Model::preventAccessingMissingAttributes()) #2227

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

Merged

Conversation

juliomotol
Copy link
Contributor

This PR aims to fix an issue when trying to check for Role's hasPermissionTo() when Model::preventAccessingMissingAttributes() is enabled.

The implemented fix overrides the HasPermission::getAllPermissions() to not check for $this->roles as the Role model will never have a relationship to itself.

Signed-off-by: Julio Motol <julio.motol89@gmail.com>
@erikn69
Copy link
Contributor

erikn69 commented Oct 21, 2022

Why don't change getAllPermissions() on src/Traits/HasPermissions.php#L308-L318

Try this:

- if ($this->roles) {
// Try this way
+ if (method_exists($this, 'roles')) {
// Or maybe, i am not sure
+ if (isset($this->roles)) {
public function getAllPermissions(): Collection
{
    /** @var Collection $permissions */
    $permissions = $this->permissions;

    if (method_exists($this, 'roles')) {
        $permissions = $permissions->merge($this->getPermissionsViaRoles());
    }

    return $permissions->sort()->values();
}

public function getAllPermissions(): Collection
{
/** @var Collection $permissions */
$permissions = $this->permissions;
if ($this->roles) {
$permissions = $permissions->merge($this->getPermissionsViaRoles());
}
return $permissions->sort()->values();
}

@juliomotol
Copy link
Contributor Author

I'm down to do that. But what about this:

// HasPermissions.php
public function getAllPermissions(): Collection
{
    /** @var Collection $permissions */
    $permissions = $this->permissions;

    if (in_array(HasRoles::class, class_uses($this))) { // Check for the `HasRoles` trait
        $permissions = $permissions->merge($this->getPermissionsViaRoles());
    }

    return $permissions->sort()->values();
}

Let me know your thoughts.

@parallels999
Copy link
Contributor

parallels999 commented Oct 25, 2022

if (method_exists($this, 'roles')) {

Seems to be working

also maybe better

if (method_exists($this, 'getPermissionsViaRoles')) {
    $permissions = $permissions->merge($this->getPermissionsViaRoles());
}

@drbyte
Copy link
Collaborator

drbyte commented Oct 25, 2022

I'm inclined to think that if (method_exists($this, 'getPermissionsViaRoles')) { is the most semantically relevant. Certainly quite readable.
I imagine it's equally performant.

@erikn69
Copy link
Contributor

erikn69 commented Oct 25, 2022

I'm inclined to think that if (method_exists($this, 'getPermissionsViaRoles')) { is the most semantically relevant.

I think the same, it looks better, but do it works the same?

@juliomotol
Copy link
Contributor Author

That would always result to true as getPermissionViaRoles() is also defined within HasPermissions

public function getPermissionsViaRoles(): Collection
{
return $this->loadMissing('roles', 'roles.permissions')
->roles->flatMap(function ($role) {
return $role->permissions;
})->sort()->values();
}

@parallels999
Copy link
Contributor

@juliomotol how about if (method_exists($this, 'roles')) {??

@juliomotol
Copy link
Contributor Author

PR has been updated

@lloricode
Copy link
Contributor

while not approved this, this is temporary fix

class Role extends \Spatie\Permission\Models\Role 
{

    protected bool $roles = false;

@juliomotol
Copy link
Contributor Author

@drbyte Any plans on releasing this?

@drbyte drbyte changed the title 🐛 Override getAllPermissions() in Role [Bug] Avoid checking permissions-via-roles on Role model (ref Model::preventAccessingMissingAttributes()) Nov 23, 2022
@drbyte
Copy link
Collaborator

drbyte commented Nov 23, 2022

Thanks!

@drbyte drbyte merged commit 3a9bc00 into spatie:main Nov 23, 2022
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

Successfully merging this pull request may close these issues.

5 participants