Skip to content

Commit a43852d

Browse files
committed
code rabbit
1 parent e7267b5 commit a43852d

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

src/Concerns/Authorizable.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,15 @@ public function authorizedTo($ability, $model)
8686
return $resolver();
8787
}
8888

89-
return true;
89+
return Response::allow();
9090
}
9191

9292
/**
9393
* Determine if the current user has a given ability.
9494
*
9595
* @param string $ability
96-
* * @param Model $model
97-
* * @param string $toActionModel
96+
* @param Model $model
97+
* @param string $toActionModel
9898
*
9999
* @throws \Illuminate\Auth\Access\AuthorizationException
100100
*

src/Http/Response.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,23 @@ public function resource(Resource $resource)
3232

3333
protected function buildGatesForModel(Model $model, Resource $resource, array $gates)
3434
{
35-
$authorizedToView = $resource->authorizedTo('view', $model);
36-
$authorizedToUpdate = $resource->authorizedTo('update', $model);
37-
$authorizedToDelete = $resource->authorizedTo('delete', $model);
38-
$authorizedToRestore = $resource->authorizedTo('restore', $model);
39-
$authorizedToForceDelete = $resource->authorizedTo('forceDelete', $model);
35+
$nameMap = [
36+
'view' => config('rest.gates.names.authorized_to_view'),
37+
'update' => config('rest.gates.names.authorized_to_update'),
38+
'delete' => config('rest.gates.names.authorized_to_delete'),
39+
'restore' => config('rest.gates.names.authorized_to_restore'),
40+
'forceDelete' => config('rest.gates.names.authorized_to_force_delete'),
41+
];
4042

41-
return array_merge(
42-
in_array('view', $gates) ? [config('rest.gates.names.authorized_to_view') => $authorizedToView->message() ?? $authorizedToView->allowed()] : [],
43-
in_array('update', $gates) ? [config('rest.gates.names.authorized_to_update') => $authorizedToUpdate->message() ?? $authorizedToUpdate->allowed()] : [],
44-
in_array('delete', $gates) ? [config('rest.gates.names.authorized_to_delete') => $authorizedToDelete->message() ?? $authorizedToDelete->allowed()] : [],
45-
in_array('restore', $gates) ? [config('rest.gates.names.authorized_to_restore') => $authorizedToRestore->message() ?? $authorizedToRestore->allowed()] : [],
46-
in_array('forceDelete', $gates) ? [config('rest.gates.names.authorized_to_force_delete') => $authorizedToForceDelete->message() ?? $authorizedToForceDelete->allowed()] : [],
47-
);
43+
$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();
48+
}
49+
}
50+
51+
return $result;
4852
}
4953

5054
/**

tests/Feature/Controllers/AutomaticGatingTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,9 @@ public function test_searching_automatic_gated_resource_and_custom_message(): vo
9696
],
9797
]
9898
);
99-
$response->assertJson(
100-
['meta' => ['gates' => ['authorized_to_create' => true]]]
99+
$response->assertJsonPath(
100+
'meta.gates.authorized_to_create',
101+
'You don\'t have permission to create user'
101102
);
102103
}
103104

0 commit comments

Comments
 (0)