Laravel-Backpack / PermissionManager

Admin interface for managing users, roles, permissions, using Backpack CRUD
http://backpackforlaravel.com
Other
527 stars 168 forks source link

[FEATURE] Filtering role and permissions in user CRUD #344

Closed mariovillani closed 11 months ago

mariovillani commented 11 months ago

Hello, Is there a way to filter out selectable roles/permissions in the CRUD view of the UserCrudController? In my application, permissions and roles that are available to choose when an user is created/updated via Backpack change by checking current user's authorizations.

I expect to add in the //SOME OPTIONS comment section:

CRUD::field([   // two interconnected entities
    'label'             => 'User Role Permissions',
    'field_unique_name' => 'user_role_permission',
    'type'              => 'checklist_dependency',
    'name'              => 'roles,permissions', // the methods that define the relationship in your Models
    'subfields'         => [
        'primary' => [
            'label'            => 'Roles',
            'name'             => 'roles', // the method that defines the relationship in your Model
            'entity'           => 'roles', // the method that defines the relationship in your Model
            'entity_secondary' => 'permissions', // the method that defines the relationship in your Model
            'attribute'        => 'name', // foreign key attribute that is shown to user
            'model'            => "Backpack\PermissionManager\app\Models\Role", // foreign key model
            'pivot'            => true, // on create&update, do you need to add/delete pivot table entries?]
            'number_columns'   => 3, //can be 1,2,3,4,6,
            // SOME OPTIONS
        ],
        'secondary' => [
            'label'          => 'Permission',
            'name'           => 'permissions', // the method that defines the relationship in your Model
            'entity'         => 'permissions', // the method that defines the relationship in your Model
            'entity_primary' => 'roles', // the method that defines the relationship in your Model
            'attribute'      => 'name', // foreign key attribute that is shown to user
            'model'          => "Backpack\PermissionManager\app\Models\Permission", // foreign key model
            'pivot'          => true, // on create&update, do you need to add/delete pivot table entries?]
            'number_columns' => 3, //can be 1,2,3,4,6
             // SOME OPTIONS
        ],
    ],
]);

Something like the "options" field for the type "select" CRUD::field to filter out results:

CRUD::field([  // Select
   'label'     => "Category",
   'type'      => 'select',
   ...
   'options'   => (function ($query) {  //Something like that to filter out the available roles
        return $query->orderBy('name', 'ASC')->where('depth', 1)->get();
    }), //  you can use this to filter the results show in the select
]);

But seems like nothing like that is implemented. Any ideas on how to do that? Thanks in advance.

welcome[bot] commented 11 months ago

Hello there! Thanks for opening your first issue on this repo!

Just a heads-up: Here at Backpack we use Github Issues only for tracking bugs. Talk about new features is also acceptable. This helps a lot in keeping our focus on improving Backpack. If you issue is not a bug/feature, please help us out by closing the issue yourself and posting in the appropriate medium (see below). If you're not sure where it fits, it's ok, a community member will probably reply to help you with that.

Backpack communication mediums:

Please keep in mind Backpack offers no official / paid support. Whatever help you receive here, on Gitter, Slack or Stackoverflow is thanks to our awesome awesome community members, who give up some of their time to help their peers. If you want to join our community, just start pitching in. We take pride in being a welcoming bunch.

Thank you!

-- Justin Case The Backpack Robot

pxpm commented 11 months ago

Hey @MarioVillani I think I got your request, thanks for it šŸ™

Unfortunately checklist_dependency is not as flexible as a regular select, it does its own query and parsing of the results in the field itself to build the dependency list. https://github.com/Laravel-Backpack/CRUD/blob/4c7030524540d34ca32db367a48ae03d34c14505/src/resources/views/crud/fields/checklist_dependency.blade.php#L21

I'd recommend you overwrite this field and do your customization. We don't plan to add new features in this field, actually if something we may have ahead in the road plan is to rewrite this field into a more general component.

If you want to keep both fields (the core and the custom) you can just create a field with a different name starting from the checklist dependency, with: php artisan backpack:field checklist_dependency_custom --from=checklist_dependency.

https://backpackforlaravel.com/docs/6.x/crud-how-to#how-to-publish-a-column-field-filter-button-and-modify-it

Closing this, please feel free to continue the conversation or re-open if you think I am wrong.

Hope that helps šŸ™