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

Filter options for role select field #159

Open daniyal-pl opened 3 years ago

daniyal-pl commented 3 years ago

How can I filter the options coming in the role select field? For example, I have two roles created but I only want to show a single role option based on a condition in the select field?

RoleSelect::make('Role', 'roles'),

This is the code that is getting all the roles. I've tried many things but none has done the work.

The same goes for the permissions. how can I show only some selected permission based on a condition?

code for permissions:

PermissionBooleanGroup::make('Permissions'),

Any help would be highly appreciated. Thanks

bzarzuela commented 3 years ago

Something like this will help.

https://github.com/bzarzuela/nova-permission/commit/15b2a4f825fd40056faf0e57489c0de0ae89aa09

The way I use it on my project is like this. On my seeder, I have 3 roles including the Manager but I do not want the UI to show the Manager.

RoleBooleanGroup::make('Roles')
                ->withOptions([
                    'Agent' => 'Agent',
                    'Supervisor' => 'Supervisor',
                ]),

Technically this still is subject for manipulation by the user just by altering the request payload, maybe I'll add more protection in a future PR, but for something like 5 minutes of effort, this is worth it.

Easiest way is to just extend the RoleBooleanGroup class to inject a new public withOptions() method.

<?php

namespace App\Nova\Fields;

class RoleBooleanGroup extends \Vyuldashev\NovaPermission\RoleBooleanGroup
{
    public function withOptions(array $options): self
    {
        $this->options($options);

        return $this;
    }
}

This could be an alternative to #137