Laravel-Backpack / PermissionManager

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

Error when using select2 to limit user to one role - is this even possible? #308

Closed emogeekface closed 2 years ago

emogeekface commented 2 years ago

Bug report

There doesn't seem to be a way to limit users to one role each.

What I did:

I have created my own UserCrudController.php and Roles Model and have changed the checklist_dependency fields for roles and permissions within the addUserFields function to:

[  // Select2
  'label'     => trans('backpack::permissionmanager.roles'),
  'type'      => 'select2',
  'name'      => 'roles', // the db column for the foreign key
  'entity'    => 'roles', // the method that defines the relationship in your Model
  'model'     => \App\Models\Role::class, // foreign key model
  'attribute' => 'name', // foreign key attribute that is shown to user
  'default'   => 1, // set the default value of the select2
],
[
  'label'          => mb_ucfirst(trans('backpack::permissionmanager.permission_plural')),
  'type'           => 'checklist',
  'name'           => 'permissions', // the method that defines the relationship in your Model
  'entity'         => 'permissions', // the method that defines the relationship in your Model
  'attribute'      => 'name', // foreign key attribute that is shown to user
  'model'          => config('permission.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
]

What I expected to happen:

I expect to be able to select one role for a user using the select2 dropdown, and successfully create and save the user.

What happened:

I receive the error: array_values(): Argument #1 ($array) must be of type array, int given from the line return $this->traitStore(); in the store() function.

What I've already tried to fix it:

This appears to be because roles is being passed across as an integer, and not an array.

I have tried to find out whether this is a backpack or spatie issue and have a feeling it might be due to spatie permissions stating that user has a BelongsToMany relationship with roles, however, I can't figure out how to override that.

Backpack, Laravel, PHP, DB version:

Backpack v5 Laravel v9

welcome[bot] commented 2 years 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

tabacitu commented 2 years ago

Hey @emogeekface ,

Indeed, this package assumes one user can have multiple roles. If that's not what you want, feel free to customize the UserCrudController to match your needs. But keep in mind users & permissions are both set using ONE field, checklist_dependency, which connects two sets of checkboxes.

If you want a select and a set of checkboxes, you can do that, but it won't be plug-and-play, you have to code it yourself. There are Backpack components that can help, though:

I'm going to close this issue, as it's not really a bug - it's a custom feature that we don't plan on supporting.

Hope it helps. Cheers!