roles icon indicating copy to clipboard operation
roles copied to clipboard

Role Heredity

Open Isfirs opened this issue 8 years ago • 2 comments

May there be a feature to make inheritance on roles like:

user
|- admin
   |-superadmin

and giving a role will automaticly assign missing permissions? And even a way to RESTRICT permissions in that kind of model?

My application in the first instance uses a Microsoft Windows © like permission model. I am porting my app to Laravel 5.2 and I am using bican/roles for a strong and nice permission concept.

I want to keep my old kind of permission concept where my views appropriate a keyword (the permission entry) and check if the group (or role) has this keyword(aka permission). And since it is the administrator, who sets up the groups and assignes the permissions, I need a way to decline the assignment of a permission somehow.

If needed I can make a more indeepth post containing more of the project and what I am doing. Maybe ther eis another approach I don't know.

Isfirs avatar Apr 06 '16 14:04 Isfirs

+1

Would be very helpful to be able to do something like this:

$user->isAtLeast('admin');  // Allows 'superadmin' role as well

Edit

I just made this little functionality by adding the following method in the HasRoleAndPermission trait:

/**
 * Check if the User has the given role or one with a higher level.
 *
 * @param  Role|string  $role
 * @return bool
 */
public function isAtLeast($role)
{
    if ($this->isPretendEnabled())
    {
        return $this->pretend('is');
    }

    if ( ! is_object($role))
    {
        if ( ! $this->hasRole($role))
        {
            $role = Role::where('slug', $role)->firstOrFail();
        }
        else
        {
            $role = $this->getRoles()->where('slug', $role)->first();
        }
    }

    return $this->level() >= $role->level;
}

ghost avatar Apr 08 '16 08:04 ghost

If I understood, you are working with the level field.

I don't like levels much. Maybe i will fork it and open a pull request. But I don't have the time now. Maybe this night.

Isfirs avatar Apr 08 '16 12:04 Isfirs