Skip to content

Commit c660d01

Browse files
fix: add canCreate() policy checks for GedcomResource and DnaResource with Filament Shield/Spatie Permissions
Co-authored-by: delicatacurtis <247246500+delicatacurtis@users.noreply.github.com>
1 parent c42ed35 commit c660d01

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

app/Filament/App/Resources/DnaResource.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ public static function shouldRegisterNavigation(): bool
4646

4747
public static function canCreate(): bool
4848
{
49-
return auth()->user()->canUploadDna();
49+
$user = auth()->user();
50+
if (!$user) {
51+
return false;
52+
}
53+
54+
return $user->can('create_dna') && $user->canUploadDna();
5055
}
5156

5257
#[Override]

app/Filament/App/Resources/GedcomResource.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ class GedcomResource extends AppResource
4747

4848
protected static string | \UnitEnum | null $navigationGroup = "🛠️ Data Management";
4949

50+
public static function canCreate(): bool
51+
{
52+
return auth()->user()?->can('create_gedcom') ?? false;
53+
}
54+
5055
public static function getPages(): array
5156
{
5257
return [

tests/Feature/Filament/Resources/GedcomResourceTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Foundation\Testing\RefreshDatabase;
1010
use Illuminate\Support\Facades\Auth;
1111
use Illuminate\Support\Facades\Queue;
12+
use Spatie\Permission\Models\Permission;
1213
use Tests\TestCase;
1314

1415
class GedcomResourceTest extends TestCase
@@ -43,6 +44,29 @@ public function test_resource_has_pages_defined(): void
4344
$this->assertArrayHasKey('create', $pages);
4445
}
4546

47+
public function test_can_create_returns_true_for_user_with_permission(): void
48+
{
49+
Permission::findOrCreate('create_gedcom', 'web');
50+
$this->user->givePermissionTo('create_gedcom');
51+
Auth::login($this->user);
52+
53+
$this->assertTrue(GedcomResource::canCreate());
54+
}
55+
56+
public function test_can_create_returns_false_for_user_without_permission(): void
57+
{
58+
Auth::login($this->user);
59+
60+
$this->assertFalse(GedcomResource::canCreate());
61+
}
62+
63+
public function test_can_create_returns_false_when_unauthenticated(): void
64+
{
65+
Auth::logout();
66+
67+
$this->assertFalse(GedcomResource::canCreate());
68+
}
69+
4670
public function test_export_gedcom_dispatches_job_with_authenticated_user(): void
4771
{
4872
Auth::login($this->user);

tests/Filament/Resources/DnaResourceTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
use App\Filament\App\Resources\DnaResource;
66
use App\Models\Dna;
7+
use App\Models\User;
78
use Illuminate\Foundation\Testing\RefreshDatabase;
9+
use Illuminate\Support\Facades\Auth;
10+
use Spatie\Permission\Models\Permission;
811
use Tests\TestCase;
912

1013
class DnaResourceTest extends TestCase
@@ -35,4 +38,39 @@ public function test_model_class_is_dna(): void
3538
{
3639
$this->assertEquals(\App\Models\Dna::class, DnaResource::getModel());
3740
}
41+
42+
public function test_can_create_returns_true_for_user_with_permission_and_upload_allowed(): void
43+
{
44+
Permission::findOrCreate('create_dna', 'web');
45+
$user = User::factory()->create(['dna_uploads_count' => 0]);
46+
$user->givePermissionTo('create_dna');
47+
Auth::login($user);
48+
49+
$this->assertTrue(DnaResource::canCreate());
50+
}
51+
52+
public function test_can_create_returns_false_for_user_without_permission(): void
53+
{
54+
$user = User::factory()->create(['dna_uploads_count' => 0]);
55+
Auth::login($user);
56+
57+
$this->assertFalse(DnaResource::canCreate());
58+
}
59+
60+
public function test_can_create_returns_false_when_upload_limit_reached(): void
61+
{
62+
Permission::findOrCreate('create_dna', 'web');
63+
$user = User::factory()->create(['dna_uploads_count' => 1, 'is_premium' => false]);
64+
$user->givePermissionTo('create_dna');
65+
Auth::login($user);
66+
67+
$this->assertFalse(DnaResource::canCreate());
68+
}
69+
70+
public function test_can_create_returns_false_when_unauthenticated(): void
71+
{
72+
Auth::logout();
73+
74+
$this->assertFalse(DnaResource::canCreate());
75+
}
3876
}

0 commit comments

Comments
 (0)