diff --git a/src/Livewire/Concerns/AuthorizesFieldManagement.php b/src/Livewire/Concerns/AuthorizesFieldManagement.php new file mode 100644 index 000000000..e402c77a1 --- /dev/null +++ b/src/Livewire/Concerns/AuthorizesFieldManagement.php @@ -0,0 +1,27 @@ +authorizeFieldManagement(); + } + + public function hydrateAuthorizesFieldManagement(): void + { + $this->authorizeFieldManagement(); + } + + private function authorizeFieldManagement(): void + { + abort_unless(CustomFieldsPlugin::get()->isAuthorized(), 403); + } +} diff --git a/src/Livewire/ManageCustomField.php b/src/Livewire/ManageCustomField.php index 024a06556..4833b9a1e 100644 --- a/src/Livewire/ManageCustomField.php +++ b/src/Livewire/ManageCustomField.php @@ -18,11 +18,13 @@ use Relaticle\CustomFields\CustomFields; use Relaticle\CustomFields\Filament\Management\Forms\Components\DateConstraintField; use Relaticle\CustomFields\Filament\Management\Schemas\FieldForm; +use Relaticle\CustomFields\Livewire\Concerns\AuthorizesFieldManagement; use Relaticle\CustomFields\Models\CustomField; use Relaticle\CustomFields\Support\SettingsMerger; final class ManageCustomField extends Component implements HasActions, HasForms { + use AuthorizesFieldManagement; use InteractsWithActions; use InteractsWithForms; use InteractsWithRecord; diff --git a/src/Livewire/ManageCustomFieldSection.php b/src/Livewire/ManageCustomFieldSection.php index 02b5985f0..3b59dcce4 100644 --- a/src/Livewire/ManageCustomFieldSection.php +++ b/src/Livewire/ManageCustomFieldSection.php @@ -21,6 +21,7 @@ use Relaticle\CustomFields\CustomFieldsPlugin; use Relaticle\CustomFields\Filament\Management\Schemas\FieldForm; use Relaticle\CustomFields\Filament\Management\Schemas\SectionForm; +use Relaticle\CustomFields\Livewire\Concerns\AuthorizesFieldManagement; use Relaticle\CustomFields\Livewire\Concerns\CreatesCustomFields; use Relaticle\CustomFields\Livewire\Concerns\ManagesFields; use Relaticle\CustomFields\Models\CustomFieldSection; @@ -28,6 +29,7 @@ final class ManageCustomFieldSection extends Component implements HasActions, HasForms { + use AuthorizesFieldManagement; use CreatesCustomFields; use InteractsWithActions; use InteractsWithForms; diff --git a/src/Livewire/ManageCustomFieldWidth.php b/src/Livewire/ManageCustomFieldWidth.php index 48ba5aa60..a1496f5b5 100644 --- a/src/Livewire/ManageCustomFieldWidth.php +++ b/src/Livewire/ManageCustomFieldWidth.php @@ -7,9 +7,12 @@ use Illuminate\Contracts\View\View; use Livewire\Component; use Relaticle\CustomFields\Enums\CustomFieldWidth; +use Relaticle\CustomFields\Livewire\Concerns\AuthorizesFieldManagement; class ManageCustomFieldWidth extends Component { + use AuthorizesFieldManagement; + /** * @var int */ diff --git a/src/Livewire/ManageFieldsTable.php b/src/Livewire/ManageFieldsTable.php index 5a4548925..0de5d8650 100644 --- a/src/Livewire/ManageFieldsTable.php +++ b/src/Livewire/ManageFieldsTable.php @@ -19,6 +19,7 @@ use Livewire\Component; use Relaticle\CustomFields\CustomFields; use Relaticle\CustomFields\Filament\Management\Schemas\FieldForm; +use Relaticle\CustomFields\Livewire\Concerns\AuthorizesFieldManagement; use Relaticle\CustomFields\Livewire\Concerns\CreatesCustomFields; use Relaticle\CustomFields\Models\CustomField; use Relaticle\CustomFields\Support\SettingsMerger; @@ -31,6 +32,7 @@ */ final class ManageFieldsTable extends Component implements HasActions, HasForms { + use AuthorizesFieldManagement; use CreatesCustomFields; use InteractsWithActions; use InteractsWithForms; diff --git a/tests/Feature/Admin/Pages/FieldManagementAuthorizationTest.php b/tests/Feature/Admin/Pages/FieldManagementAuthorizationTest.php new file mode 100644 index 000000000..bc16d042d --- /dev/null +++ b/tests/Feature/Admin/Pages/FieldManagementAuthorizationTest.php @@ -0,0 +1,126 @@ +actingAs(User::factory()->create()); + + $this->allowed = true; + $allowed = &$this->allowed; + CustomFieldsPlugin::get()->authorize(static function () use (&$allowed): bool { + return $allowed; + }); + + $this->section = CustomFieldSection::factory()->forEntityType(User::class)->create(); + $this->field = CustomField::factory()->ofType('text')->create([ + 'custom_field_section_id' => $this->section->getKey(), + 'entity_type' => User::class, + 'name' => 'Original name', + 'sort_order' => 5, + ]); +}); + +afterEach(function (): void { + CustomFieldsPlugin::get()->authorize(true); +}); + +it('refuses to mount :dataset without permission to manage fields', function (string $component): void { + $this->allowed = false; + + $parameters = match ($component) { + ManageFieldsTable::class => ['entityType' => User::class], + ManageCustomField::class => ['field' => $this->field], + ManageCustomFieldSection::class => ['section' => $this->section, 'entityType' => User::class], + ManageCustomFieldWidth::class => ['selectedWidth' => CustomFieldWidth::_100, 'fieldId' => $this->field->getKey()], + }; + + livewire($component, $parameters)->assertForbidden(); +})->with([ + 'the fields table' => ManageFieldsTable::class, + 'a field' => ManageCustomField::class, + 'a section' => ManageCustomFieldSection::class, + 'a field width' => ManageCustomFieldWidth::class, +]); + +it('stops a fields table mounted with permission from deleting once permission is withdrawn', function (): void { + $table = livewire(ManageFieldsTable::class, ['entityType' => User::class]); + $this->allowed = false; + + $table->call('mountAction', 'deleteField', ['fieldId' => $this->field->getKey()]) + ->assertForbidden(); + + expect(CustomField::query()->withDeactivated()->whereKey($this->field->getKey())->exists())->toBeTrue(); +}); + +it('stops a fields table mounted with permission from editing once permission is withdrawn', function (): void { + $table = livewire(ManageFieldsTable::class, ['entityType' => User::class]); + $this->allowed = false; + + $table->call('mountAction', 'editField', ['fieldId' => $this->field->getKey()]) + ->assertForbidden(); + + expect($this->field->fresh()->name)->toBe('Original name'); +}); + +it('stops a fields table mounted with permission from reordering once permission is withdrawn', function (): void { + $table = livewire(ManageFieldsTable::class, ['entityType' => User::class]); + $this->allowed = false; + + $table->call('updateFieldsOrder', [$this->field->getKey()])->assertForbidden(); + + expect($this->field->fresh()->sort_order)->toBe(5); +}); + +it('stops a fields table mounted with permission from resizing once permission is withdrawn', function (): void { + $table = livewire(ManageFieldsTable::class, ['entityType' => User::class]); + $this->allowed = false; + + $table->call('fieldWidthUpdated', $this->field->getKey(), 25)->assertForbidden(); + + expect($this->field->fresh()->width)->not->toBe(CustomFieldWidth::_25); +}); + +it('stops a field mounted with permission from deleting once permission is withdrawn', function (): void { + $component = livewire(ManageCustomField::class, ['field' => $this->field]); + $this->allowed = false; + + $component->call('mountAction', 'delete')->assertForbidden(); + + expect(CustomField::query()->withDeactivated()->whereKey($this->field->getKey())->exists())->toBeTrue(); +}); + +it('stops a section mounted with permission from reordering once permission is withdrawn', function (): void { + $component = livewire(ManageCustomFieldSection::class, ['section' => $this->section, 'entityType' => User::class]); + $this->allowed = false; + + $component->call('updateFieldsOrder', $this->section->getKey(), [$this->field->getKey()])->assertForbidden(); + + expect($this->field->fresh()->sort_order)->toBe(5); +}); + +it('lets a fields table with permission delete and reorder fields', function (): void { + $other = CustomField::factory()->ofType('text')->create([ + 'custom_field_section_id' => $this->section->getKey(), + 'entity_type' => User::class, + 'sort_order' => 9, + ]); + + livewire(ManageFieldsTable::class, ['entityType' => User::class]) + ->call('updateFieldsOrder', [$other->getKey()]) + ->callAction(TestAction::make('deleteField')->arguments(['fieldId' => $this->field->getKey()])) + ->assertSuccessful(); + + expect($other->fresh()->sort_order)->toBe(0) + ->and(CustomField::query()->withDeactivated()->whereKey($this->field->getKey())->exists())->toBeFalse(); +});