Files
animethemes-server/app/Filament/HeaderActions/Models/Auth/User/RevokeRoleHeaderAction.php
T
2024-05-23 17:22:01 -03:00

73 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Filament\HeaderActions\Models\Auth\User;
use App\Filament\Components\Fields\Select;
use App\Models\Auth\Role;
use App\Models\Auth\User;
use Filament\Forms\Form;
use Filament\Actions\Action;
use Illuminate\Support\Arr;
/**
* Class RevokeRoleHeaderAction.
*/
class RevokeRoleHeaderAction extends Action
{
final public const FIELD_ROLE = 'role';
/**
* Initial setup for the action.
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();
$this->action(fn (User $record, array $data) => $this->handle($record, $data));
}
/**
* Perform the action on the given models.
*
* @param User $user
* @param array $data
* @return void
*/
public function handle(User $user, array $data): void
{
$role = Role::findById(intval(Arr::get($data, self::FIELD_ROLE)));
$user->removeRole($role);
}
/**
* Get the fields available on the action.
*
* @param Form $form
* @return Form
*
* @noinspection PhpMissingParentCallCommonInspection
*/
public function getForm(Form $form): Form
{
$roles = Role::all([Role::ATTRIBUTE_ID, Role::ATTRIBUTE_NAME])
->keyBy(Role::ATTRIBUTE_ID)
->map(fn (Role $role) => $role->name)
->toArray();
return $form
->schema([
Select::make(self::FIELD_ROLE)
->label(__('filament.resources.singularLabel.role'))
->searchable()
->required()
->options($roles)
->rules('required'),
]);
}
}