Skip to content

Commit 1706a61

Browse files
committed
set this as a config
1 parent a43852d commit 1706a61

File tree

3 files changed

+53
-23
lines changed

3 files changed

+53
-23
lines changed

config/rest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
'gates' => [
1515
'enabled' => true,
1616
'key' => 'gates',
17+
'message' => [
18+
'enabled' => false
19+
],
1720
// Here you can customize the keys for each gate
1821
'names' => [
1922
'authorized_to_view' => 'authorized_to_view',

src/Http/Response.php

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ public function resource(Resource $resource)
3030
});
3131
}
3232

33-
protected function buildGatesForModel(Model $model, Resource $resource, array $gates)
33+
protected function buildGatesForModel(Model|string $model, Resource $resource, array $gates)
3434
{
3535
$nameMap = [
36+
'create' => config('rest.gates.names.authorized_to_create'),
3637
'view' => config('rest.gates.names.authorized_to_view'),
3738
'update' => config('rest.gates.names.authorized_to_update'),
3839
'delete' => config('rest.gates.names.authorized_to_delete'),
@@ -41,10 +42,23 @@ protected function buildGatesForModel(Model $model, Resource $resource, array $g
4142
];
4243

4344
$result = [];
44-
foreach ($gates as $gate) {
45-
if (isset($nameMap[$gate])) {
46-
$auth = $resource->authorizedTo($gate, $model);
47-
$result[$nameMap[$gate]] = $auth->message() ?? $auth->allowed();
45+
46+
if (config('rest.gates.message.enabled', false)) {
47+
foreach ($gates as $gate) {
48+
if (isset($nameMap[$gate])) {
49+
$authorizedTo = $resource->authorizedTo($gate, $model);
50+
$result[$nameMap[$gate]]['allowed'] = $authorizedTo->allowed();
51+
$result[$nameMap[$gate]]['message'] = $authorizedTo->message();
52+
}
53+
}
54+
} else {
55+
// TODO: put the good anchor to link to the new method (link in the string)
56+
trigger_deprecation('lomkit/laravel-rest-api', '2.17.0', 'In Laravel Rest Api 3 it won\'t be possible to use the old gate schema, please upgrade as quickly as possible. See: https://laravel-rest-api.lomkit.com/digging-deeper/gates#policy-message-in-gates');
57+
foreach ($gates as $gate) {
58+
if (isset($nameMap[$gate])) {
59+
$authorizedTo = $resource->authorizedTo($gate, $model);
60+
$result[$nameMap[$gate]] = $authorizedTo->allowed();
61+
}
4862
}
4963
}
5064

@@ -87,9 +101,10 @@ public function modelToResponse(Model $model, Resource $resource, array $request
87101
)
88102
)
89103
->when($resource->isGatingEnabled() && isset($currentRequestArray['gates']), function ($attributes) use ($currentRequestArray, $resource, $model) {
104+
$currentRequestArrayWithoutCreate = collect($currentRequestArray['gates'])->reject(fn ($value) => $value === 'create')->toArray();
90105
return $attributes->put(
91106
config('rest.gates.key'),
92-
$this->buildGatesForModel($model, $resource, $currentRequestArray['gates'])
107+
$this->buildGatesForModel($model, $resource, $currentRequestArrayWithoutCreate)
93108
);
94109
})
95110
->toArray(),
@@ -136,20 +151,14 @@ public function modelToResponse(Model $model, Resource $resource, array $request
136151
public function toResponse($request)
137152
{
138153
if ($this->responsable instanceof LengthAwarePaginator) {
139-
if ($this->resource->isGatingEnabled() && in_array('create', $request->input('search.gates', []))) {
140-
$authorizedToCreate = $this->resource->authorizedTo('create', $this->resource::newModel()::class);
141-
}
142-
143154
$restLengthAwarePaginator = new \Lomkit\Rest\Pagination\LengthAwarePaginator(
144155
$this->responsable->items(),
145156
$this->responsable->total(),
146157
$this->responsable->perPage(),
147158
$this->responsable->currentPage(),
148159
$this->responsable->getOptions(),
149-
isset($authorizedToCreate) ? [
150-
config('rest.gates.key') => [
151-
config('rest.gates.names.authorized_to_create') => $authorizedToCreate->message() ?? $authorizedToCreate->allowed(),
152-
],
160+
$this->resource->isGatingEnabled() && in_array('create', $request->input('search.gates', [])) ? [
161+
config('rest.gates.key') => $this->buildGatesForModel($this->resource::newModel()::class, $this->resource, ['create']),
153162
] : []
154163
);
155164

@@ -168,9 +177,7 @@ public function toResponse($request)
168177
'data' => $data ?? $this->map($this->responsable, $this->modelToResponse($this->responsable, $this->resource, $request->input('search', []))),
169178
'meta' => array_merge(
170179
$this->resource->isGatingEnabled() && in_array('create', $request->input('search.gates', [])) ? [
171-
config('rest.gates.key') => [
172-
config('rest.gates.names.authorized_to_create') => $this->resource->authorizedTo('create', $this->resource::newModel()::class),
173-
],
180+
config('rest.gates.key') => $this->buildGatesForModel($this->resource::newModel()::class, $this->resource, ['create']),
174181
] : []
175182
),
176183
];

tests/Feature/Controllers/AutomaticGatingTest.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ public function test_searching_automatic_gated_resource_and_custom_message(): vo
7070

7171
Gate::policy(Model::class, RedPolicyWithMessage::class);
7272

73+
config(['rest.gates.message.enabled' => true]);
74+
7375
$response = $this->post(
7476
'/api/automatic-gating/search',
7577
[
@@ -87,18 +89,36 @@ public function test_searching_automatic_gated_resource_and_custom_message(): vo
8789
[
8890
[
8991
'gates' => [
90-
'authorized_to_view' => 'You don\'t have permission to view user',
91-
'authorized_to_update' => 'You don\'t have permission to update user',
92-
'authorized_to_delete' => 'You don\'t have permission to delete user',
93-
'authorized_to_restore' => 'You don\'t have permission to restore user',
94-
'authorized_to_force_delete' => 'You don\'t have permission to force delete user',
92+
'authorized_to_view' => [
93+
'allowed' => false,
94+
'message' => 'You don\'t have permission to view user'
95+
],
96+
'authorized_to_update' => [
97+
'allowed' => false,
98+
'message' => 'You don\'t have permission to update user'
99+
],
100+
'authorized_to_delete' => [
101+
'allowed' => false,
102+
'message' => 'You don\'t have permission to delete user'
103+
],
104+
'authorized_to_restore' => [
105+
'allowed' => false,
106+
'message' => 'You don\'t have permission to restore user'
107+
],
108+
'authorized_to_force_delete' => [
109+
'allowed' => false,
110+
'message' => 'You don\'t have permission to force delete user'
111+
],
95112
],
96113
],
97114
]
98115
);
99116
$response->assertJsonPath(
100117
'meta.gates.authorized_to_create',
101-
'You don\'t have permission to create user'
118+
[
119+
'allowed' => false,
120+
'message' => 'You don\'t have permission to create user'
121+
]
102122
);
103123
}
104124

0 commit comments

Comments
 (0)