Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/Livewire/Concerns/AuthorizesFieldManagement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Relaticle\CustomFields\Livewire\Concerns;

use Relaticle\CustomFields\CustomFieldsPlugin;

// The management page checks the plugin's authorize callback only when it loads,
// so every component it renders re-checks it on each request of its own.
trait AuthorizesFieldManagement
{
public function mountAuthorizesFieldManagement(): void
{
$this->authorizeFieldManagement();
}

public function hydrateAuthorizesFieldManagement(): void
{
$this->authorizeFieldManagement();
}

private function authorizeFieldManagement(): void
{
abort_unless(CustomFieldsPlugin::get()->isAuthorized(), 403);
}
}
2 changes: 2 additions & 0 deletions src/Livewire/ManageCustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/Livewire/ManageCustomFieldSection.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
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;
use Relaticle\CustomFields\Support\SettingsMerger;

final class ManageCustomFieldSection extends Component implements HasActions, HasForms
{
use AuthorizesFieldManagement;
use CreatesCustomFields;
use InteractsWithActions;
use InteractsWithForms;
Expand Down
3 changes: 3 additions & 0 deletions src/Livewire/ManageCustomFieldWidth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Livewire/ManageFieldsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +32,7 @@
*/
final class ManageFieldsTable extends Component implements HasActions, HasForms
{
use AuthorizesFieldManagement;
use CreatesCustomFields;
use InteractsWithActions;
use InteractsWithForms;
Expand Down
126 changes: 126 additions & 0 deletions tests/Feature/Admin/Pages/FieldManagementAuthorizationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

declare(strict_types=1);

use Filament\Actions\Testing\TestAction;
use Relaticle\CustomFields\CustomFieldsPlugin;
use Relaticle\CustomFields\Enums\CustomFieldWidth;
use Relaticle\CustomFields\Livewire\ManageCustomField;
use Relaticle\CustomFields\Livewire\ManageCustomFieldSection;
use Relaticle\CustomFields\Livewire\ManageCustomFieldWidth;
use Relaticle\CustomFields\Livewire\ManageFieldsTable;
use Relaticle\CustomFields\Models\CustomField;
use Relaticle\CustomFields\Models\CustomFieldSection;
use Relaticle\CustomFields\Tests\Fixtures\Models\User;

beforeEach(function (): void {
$this->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();
});
Loading