vyuldashev / nova-permission

A Laravel Nova tool for Spatie's laravel-permission library
https://novapackages.com/packages/vyuldashev/nova-permission
419 stars 217 forks source link

How to get permissions attached to a role to be shown as selected on User Details/Edit User screens? #110

Closed ITango closed 4 years ago

ITango commented 4 years ago

Question

How can I get the permissions attached to a role to be shown as selected on the User Details and Edit User screens?

In the first screenshot below an Admin role has 3 permissions attached (view accounts, invoices, and subscriptions).

Screen Shot 2019-12-13 at 4 35 49 pm

The other two screenshots show User Details and Edit User screens for Test Account that has an Admin role attached. However, the Admin role permissions (view accounts, invoices, and subscriptions) are not shown as selected.

Screen Shot 2019-12-13 at 4 36 18 pm Screen Shot 2019-12-13 at 4 36 33 pm
SDCCardsDeveloper commented 4 years ago

Same here :)

bilfeldt commented 4 years ago

One way to achieve this would be to make a viewOnly field that uses the getPermissionsViaRoles() methods from spatie/laravel-permission:

// Direct permissions
$user->getDirectPermissions() // Or $user->permissions;

// Permissions inherited from the user's roles
$user->getPermissionsViaRoles();

// All permissions which apply on the user (inherited and direct)
$user->getAllPermissions();
ITango commented 4 years ago

Another way to do it is as below:

RoleBooleanGroup::make('Roles'), PermissionBooleanGroup::make('Permissions')->resolveUsing(function ($permissions, $user) { $values = []; foreach ($user->getAllPermissions() as $item) { $values[$item->name] = true; }

return $values;

}),

olipayne commented 3 years ago

This does not remove those permissions however, once the role is removed from the user


            RoleBooleanGroup::make('Roles'),
            PermissionBooleanGroup::make('Permissions')->resolveUsing(function ($permissions, $user) {
                $values = [];
                foreach ($user->getAllPermissions() as $item) {
                    $values[$item->name] = true;
                }
                return $values;
            }),