Skip to content

Commit 0a672c6

Browse files
committed
✨ new imports methods
1 parent 01708e5 commit 0a672c6

File tree

4 files changed

+70
-22
lines changed

4 files changed

+70
-22
lines changed

content/2.essentials/1.perimeters.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,12 @@ This allows it to surpass other perimeters while still working in coordination w
4545

4646
```php
4747
use Illuminate\Database\Eloquent\Builder;
48+
use App\Models\Task;
4849

4950
class TaskControl extends Control
5051
{
52+
protected string $model = Task::class;
53+
5154
protected function perimeters(): array
5255
{
5356
return [

content/2.essentials/2.controls.md

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@ use Lomkit\Access\Controls\Control;
1616

1717
class PostControl extends Control
1818
{
19-
/**
19+
/**
20+
* The model the control refers to.
21+
*
22+
* @var class-string<Model>
23+
*/
24+
protected string $model;
25+
26+
/**
2027
* Retrieve the list of perimeter definitions for the current control.
2128
*
2229
* @return array<\Lomkit\Access\Perimeters\Perimeter> An array of Perimeter objects.
@@ -30,6 +37,17 @@ class PostControl extends Control
3037
}
3138
```
3239

40+
Don't forget to define the linked model:
41+
42+
```php
43+
/**
44+
* The model the control refers to.
45+
*
46+
* @var class-string<Model>
47+
*/
48+
protected string $model = \App\Models\Task::class;
49+
```
50+
3351
Next you'll need to define your perimeters in the way you want them to be registered:
3452

3553
```php
@@ -82,33 +100,34 @@ class Post extends Model
82100
}
83101
```
84102

85-
### (Optional) Specify which Control to use
103+
### Parent query isolation
86104

87-
You can optionally specify which Control class this model should use:
105+
By default the access parent's query is isolated from your base query, this is to prevent conflict if you manipulate the query after.
88106

89-
```php
90-
use Lomkit\Access\Controls\HasControl;
91-
use App\Access\Controls\PostControl;
107+
If you want to disable this, simply set `queries.isolate_parent_query` in your config file
92108

93-
class Post extends Model
94-
{
95-
use HasControl;
96-
97-
protected static string $control = PostControl::class;
98-
}
109+
```php[access-control.php]
110+
<?php
111+
112+
return [
113+
'queries' => [
114+
'isolate_parent_query' => false, // Set this to false
115+
],
116+
];
99117
```
100118

101-
## Policies
119+
### Perimeter query isolation
102120

103-
In order to be applied to the policy, you need to extend the `ControlledPolicy` class:
121+
By default the access perimeter's query are isolated from your the other perimeters query, this is to prevent conflict if you manipulate overlayed perimeter.
104122

105-
```php
106-
use Lomkit\Access\Policies\ControlledPolicy;
123+
If you want to disable this, simply set `queries.isolate_perimeter_queries` in your config file
107124

108-
class PostPolicy extends ControlledPolicy
109-
{
110-
protected string $model = Post::class;
111-
}
112-
```
125+
```php[access-control.php]
126+
<?php
113127
114-
And you are ready to go, have a look at the [usage section](/essentials/usage) to apply the security wherever you want !
128+
return [
129+
'queries' => [
130+
'isolate_perimeter_queries' => false, // Set this to false
131+
],
132+
];
133+
```

content/2.essentials/3.policies.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
title: Policies
3+
description: Automatically apply Laravel Access Control on your policies !
4+
---
5+
6+
## Policies
7+
8+
In order to be applied to the policy, you need to extend the `ControlledPolicy` class and specify the linked control:
9+
10+
```php
11+
use Lomkit\Access\Policies\ControlledPolicy;
12+
use App\Access\Control\PostControl;
13+
14+
class PostPolicy extends ControlledPolicy
15+
{
16+
protected string $control = PostControl::class;
17+
}
18+
```
19+
20+
And you are ready to go, have a look at the [usage section](/essentials/usage#policies) to apply the security wherever you want !

content/2.essentials/3.usage.md renamed to content/2.essentials/4.usage.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ Post::controlled()
1313
->get();
1414
```
1515

16+
::info
17+
When using the query, Access Control will always use the `view` method in the `should` function. This is because it considers you are trying to view models
18+
since you make a SQL query.
19+
If you are on an index of a controller, the control might trigger twice, once for `viewAny` (Policy) and once for `view` (Query)
20+
::
21+
1622
## Policies
1723

1824
Policies are secured by default for the common method: `viewAny`, `view`, `update`, `create`, `delete`, `restore`, `forceDelete`.

0 commit comments

Comments
 (0)