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

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

Closed
mariovillani opened this issue Nov 20, 2023 · 2 comments
Closed

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

mariovillani opened this issue Nov 20, 2023 · 2 comments

Comments

@mariovillani
Copy link

mariovillani commented Nov 20, 2023

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.

Copy link

welcome bot commented Nov 20, 2023

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:

  • Bug Reports, Feature Requests - Github Issues (here);
  • Quick help (How do I do X) - Gitter Chatroom;
  • Long questions (I have done X and Y and it won't do Z wtf) - Stackoverflow, using the backpack-for-laravel tag;

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
Copy link
Contributor

pxpm commented Nov 21, 2023

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 🙏

@pxpm pxpm closed this as completed Nov 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

No branches or pull requests

2 participants