Skip to content

Commit 21eedea

Browse files
committed
✨ disable authorizations globaly
1 parent b27ef84 commit 21eedea

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

config/rest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,18 @@
2525
'authorized_to_force_delete' => 'authorized_to_force_delete',
2626
]
2727
],
28+
29+
/*
30+
|--------------------------------------------------------------------------
31+
| Rest Authorizations
32+
|--------------------------------------------------------------------------
33+
|
34+
| This is the feature that automatically binds to policies to validate incoming requests.
35+
| Laravel Rest Api will validate each models searched / mutated / deleted to avoid leaks in your API.
36+
|
37+
*/
38+
39+
'authorizations' => [
40+
'enabled' => true
41+
],
2842
];

src/Http/Resource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@ public function isAutomaticGatingEnabled() : bool {
7373
}
7474

7575
public function isAuthorizingEnabled() : bool {
76-
return true;
76+
return config('rest.authorizations.enabled');
7777
}
7878
}

tests/Feature/Controllers/AutomaticGatingTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,30 @@ public function test_searching_automatic_gated_resource(): void
7171
);
7272
}
7373

74+
public function test_searching_automatic_gated_resource_with_global_config_disabled(): void
75+
{
76+
$model = ModelFactory::new()
77+
->create();
78+
79+
Gate::policy(Model::class, GreenPolicy::class);
80+
81+
config(['rest.automatic_gates.enabled' => false]);
82+
83+
$response = $this->post(
84+
'/api/automatic-gating/search',
85+
[
86+
87+
],
88+
['Accept' => 'application/json']
89+
);
90+
91+
$this->assertResourcePaginated(
92+
$response,
93+
[$model],
94+
new AutomaticGatingResource
95+
);
96+
}
97+
7498
public function test_searching_automatic_gated_resource_with_create_policy(): void
7599
{
76100
$model = ModelFactory::new()

tests/Feature/Controllers/NoAuthorizationTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,30 @@
3636

3737
class NoAuthorizationTest extends TestCase
3838
{
39+
public function test_searching_with_global_authorization_disabled(): void
40+
{
41+
$model = ModelFactory::new()
42+
->create();
43+
44+
Gate::policy(Model::class, RedPolicy::class);
45+
46+
config(['rest.authorizations.enabled' => false]);
47+
48+
$response = $this->post(
49+
'/api/models/search',
50+
[
51+
52+
],
53+
['Accept' => 'application/json']
54+
);
55+
56+
$this->assertResourcePaginated(
57+
$response,
58+
[$model],
59+
new ModelResource
60+
);
61+
}
62+
3963
public function test_searching_with_no_authorizations(): void
4064
{
4165
$model = ModelFactory::new()

0 commit comments

Comments
 (0)