Skip to content

Commit c8e3dd1

Browse files
authored
Feature/small adjustments (#33)
1 parent 9c40752 commit c8e3dd1

File tree

7 files changed

+38
-10
lines changed

7 files changed

+38
-10
lines changed

config/access-control.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,20 @@
1212
'isolate_parent_query' => true, // Isolate the control's logic by applying a parent where on the query
1313
'isolate_perimeter_queries' => true, // Isolate every perimeter query by applying a default "orWhere" to prevent Overlayed Perimeters collapsing
1414
],
15+
16+
/*
17+
|--------------------------------------------------------------------------
18+
| Access Control Methods
19+
|--------------------------------------------------------------------------
20+
|
21+
*/
22+
'methods' => [
23+
'viewAny' => 'view',
24+
'view' => 'view',
25+
'create' => 'create',
26+
'update' => 'update',
27+
'delete' => 'delete',
28+
'restore' => 'restore',
29+
'forceDelete' => 'forceDelete',
30+
],
1531
];

src/Console/PerimeterMakeCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ protected function buildClass($name)
104104
protected function getOptions()
105105
{
106106
return [
107-
['overlay', 'o', InputOption::VALUE_OPTIONAL, 'Indicates if the perimeter overlays'],
107+
['overlay', 'o', InputOption::VALUE_NONE, 'Indicates if the perimeter overlays'],
108108
];
109109
}
110110

src/Console/stubs/control.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class {{ class }} extends Control
1313
* The model the control refers to.
1414
* @var class-string<Model>
1515
*/
16-
protected string $model = {{ namespacedModel }}::class;
16+
protected string $model = \{{ namespacedModel }}::class;
1717

1818
/**
1919
* Retrieve the list of perimeter definitions for the current control.

src/Controls/Control.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,12 @@ protected function perimeters(): array
6161
*/
6262
public function applies(Model $user, string $method, Model $model): bool
6363
{
64+
// If the method is viewAny, we check instead if he has any 'view' right on different perimeters
65+
$appliesMethod = config(sprintf('access-control.methods.%s', $method)) ?? $method;
66+
6467
foreach ($this->perimeters() as $perimeter) {
65-
if ($perimeter->applyAllowedCallback($user, $method)) {
66-
// If the model doesn't exists, it means the method is not related to a model
68+
if ($perimeter->applyAllowedCallback($user, $appliesMethod)) {
69+
// If the model doesn't exist, it means the method is not related to a model
6770
// so we don't need to activate the should result since we can't compare an existing model
6871
if (!$model->exists) {
6972
return true;
@@ -131,7 +134,7 @@ protected function applyQueryControl(Builder $query, Model $user): Builder
131134
};
132135

133136
foreach ($this->perimeters() as $perimeter) {
134-
if ($perimeter->applyAllowedCallback($user, 'view')) {
137+
if ($perimeter->applyAllowedCallback($user, config('access-control.methods.view'))) {
135138
if (config('access-control.queries.isolate_perimeter_queries')) {
136139
$query = $query->orWhere(function (Builder $query) use ($user, $perimeter) {
137140
$perimeter->applyQueryCallback($query, $user);
@@ -166,7 +169,7 @@ protected function applyScoutQueryControl(\Laravel\Scout\Builder $query, Model $
166169
};
167170

168171
foreach ($this->perimeters() as $perimeter) {
169-
if ($perimeter->applyAllowedCallback($user, 'view')) {
172+
if ($perimeter->applyAllowedCallback($user, config('access-control.methods.view'))) {
170173
$query = $perimeter->applyScoutQueryCallback($query, $user);
171174

172175
$noResultCallback = function ($query) {return $query; };

tests/Feature/ControlsShouldTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,18 @@ public function test_control_with_no_perimeter_passing(): void
1414
$this->assertFalse((new \Lomkit\Access\Tests\Support\Access\Controls\ModelControl())->applies(Auth::user(), 'create', new Model()));
1515
}
1616

17+
public function test_control_should_view_any_using_client_perimeter_with_changed_view_any_method_configuration(): void
18+
{
19+
Gate::define('view client models', function (User $user) {
20+
return true;
21+
});
22+
23+
$this->assertTrue((new \Lomkit\Access\Tests\Support\Access\Controls\ModelControl())->applies(Auth::user(), 'viewAny', new Model()));
24+
}
25+
1726
public function test_control_should_view_any_using_client_perimeter(): void
1827
{
19-
Gate::define('viewAny client models', function (User $user) {
28+
Gate::define('view client models', function (User $user) {
2029
return true;
2130
});
2231

@@ -85,7 +94,7 @@ public function test_control_should_delete_using_client_perimeter(): void
8594

8695
public function test_control_should_view_any_using_global_perimeter(): void
8796
{
88-
Gate::define('viewAny global models', function (User $user) {
97+
Gate::define('view global models', function (User $user) {
8998
return true;
9099
});
91100

tests/Feature/PoliciesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function test_policies_calls_view_method_properly(): void
2424

2525
public function test_policies_calls_view_any_method_properly(): void
2626
{
27-
Gate::define('viewAny global models', function (User $user) {
27+
Gate::define('view global models', function (User $user) {
2828
return true;
2929
});
3030
$user = \Illuminate\Support\Facades\Auth::user();

tests/Unit/Console/MakeCommandsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function test_make_control_with_model_command()
109109

110110
$this->assertFileExists(app_path('Access/Controls/TestControl.php'));
111111
$this->assertStringContainsString('class TestControl', file_get_contents(app_path('Access/Controls/TestControl.php')));
112-
$this->assertStringContainsString('protected string $model = App\Models\User::class;', file_get_contents(app_path('Access/Controls/TestControl.php')));
112+
$this->assertStringContainsString('protected string $model = \App\Models\User::class;', file_get_contents(app_path('Access/Controls/TestControl.php')));
113113

114114
unlink(app_path('Models/User.php'));
115115
unlink(app_path('Models/Post.php'));

0 commit comments

Comments
 (0)