Skip to content

Commit 3e5de3d

Browse files
committed
hooks diagrams
1 parent 2617c7a commit 3e5de3d

File tree

21 files changed

+85
-19
lines changed

21 files changed

+85
-19
lines changed

adminforth/documentation/docs/tutorial/03-Customization/04-hooks.md

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,43 @@ Hooks are used to:
99
- modify the fetched data before it is displayed in the list and show
1010
- prevent the request to db depending on some condition (Better use [allowedActions](./05-limitingAccess.md) for this)
1111

12+
Every hook is executed when AdminForth frontend makes some internal API HTTP request to the backend.
1213

13-
## Modify the data before it is saved to the database
14+
Every hook must return one of two objects:
15+
1) If everything is fine and request flow should be continued hook should return `{ ok: true }`
16+
2) If for some reason you need to interrupt request flow in hook you should return `{ ok: false, error: 'some error message for user' }`. This is
17+
handy for access-related tasks, though most of such tasks should be solved with [allowedActions](./05-limitingAccess.md) and not hooks.
18+
19+
Every hook is array of async functions, so you can have multiple hooks for one event.
20+
For simplicity of course you can specify hook as scalar async function and not as array, but internally it will be anyway converted to array with single element just after app start. Plugins can push new own hooks in front of yours (using `unshift`) or after yours (using `push`). For example audit log plugin adds hooks for registration of all changes in the database.
21+
22+
Here we will consider possible flows one by one
23+
24+
## Initial data for edit page flow
25+
26+
When user opens edit page, AdminForth makes a request to the backend to get the initial data for the form.
27+
28+
![Initial data for edit page flow](image-28.png)
29+
30+
Practically you can use this hook to modify or add some data before it is displayed on the edit page.
31+
32+
For example [upload plugin](/docs/tutorial/Plugins/upload/) uses this hook to generate signed preview URL so user can see existing uploaded file preview in form, and at the same time database stores only original file path which might be not accessible without presigned URL.
33+
34+
## Saving data on edit page
35+
36+
When user clicks the save button, AdminForth makes a request to the backend to save the data.
37+
38+
![Saving data on edit page](image-27.png)
39+
40+
Practically you can use this hook to modify the data or populate new fields before it is saved to the database.
41+
42+
## Saving data on create page
43+
44+
When user clicks the save button from create page, AdminForth makes a request to the backend to save the data.
45+
46+
![Saving data on create page](image-26.png)
47+
48+
### Example: modify the created object before it is saved to the database
1449

1550
Let's add reference to `adminUser` when user creates a new apartment:
1651

@@ -38,9 +73,9 @@ import type { AdminUser } from 'adminforth';
3873
//diff-add
3974
create: {
4075
//diff-add
41-
beforeSave: async ({ adminUser, record }: { adminUser: AdminUser, record: any }) => {
76+
beforeSave: async ({ adminUser, record }: { adminUser: AdminUser, updates: any }) => {
4277
//diff-add
43-
record.realtor_id = adminUser.dbUser.id;
78+
updates.realtor_id = adminUser.dbUser.id;
4479
//diff-add
4580
return { ok: true, record };
4681
//diff-add
@@ -52,11 +87,15 @@ import type { AdminUser } from 'adminforth';
5287
}
5388
```
5489

90+
In this way user who creates the apartment will be assigned as a realtor.
5591

56-
## Limiting access to user-related data
92+
## List page flow
5793

94+
When user opens the list page, AdminForth makes a request to the backend to get the list of items.
5895

59-
### List of entities
96+
![List page flow](image-22.png)
97+
98+
### Example: limit access in list to user-related records
6099

61100
For example we can prevent the user to see Apartments created by other users. Superadmin user still can see all:
62101

@@ -90,13 +129,17 @@ For example we can prevent the user to see Apartments created by other users. Su
90129
}
91130
```
92131

93-
This hook will prevent the user to see Apartments created by other users in list, however user if will be able to discover
94-
the apartment id, will be able to use show page to see the apartment details. Let's limit it as well:
132+
This hook will prevent the user to see Apartments created by other users in list, however if user will be able to discover
133+
the apartment id, he will be able to use show page to see the apartment details, that is why separate limiting for show page is required as well. Below we will discover how to limit access to show page.
95134

96135
### Dropdown list of foreignResource
97136

98137
By default if there is `foreignResource` like we use for demo on `realtor_id` column, the filter will suggest a
99-
select dropdown with list of all Realtors. This might be a leak to get id's of other users. Let's limit it:
138+
select dropdown with list of all Realtors.
139+
140+
This might bring us a leak where explorer will get id's of other users in the system which might be not desired
141+
142+
Let's limit it:
100143

101144
```ts title='./resources/apartments.ts'
102145
{
@@ -118,7 +161,22 @@ select dropdown with list of all Realtors. This might be a leak to get id's of o
118161

119162
In our case we limit the dropdown list to show only the current user, however you can use same sample to list only objects who are related to the current user in case if you will have relation configurations which require to show related objects which belongs to the current user.
120163

121-
### Show entity
164+
Flow diagram for dropdown list:
165+
166+
![Flow diagram for dropdown list](image-30.png)
167+
168+
## Show page flow
169+
170+
When user opens the show page, AdminForth makes a request to the backend to get the item. This request ia absolutely the same as one for edit initial data, because naturally for most of cases data for show page are the same as initial data for edit page.
171+
172+
However if you still need to distinguish between these two cases you can use `query.source` parameter in hook (we do not mentioned it in diagram for simplicity and rare demand).
173+
174+
Here is show request flow:
175+
176+
![Here is show request](image-29.png)
177+
178+
179+
### Example show limiting:
122180

123181
```ts title='./resources/apartments.ts'
124182
{
@@ -143,14 +201,14 @@ In our case we limit the dropdown list to show only the current user, however yo
143201
}
144202
```
145203

146-
> 👆 Please note that we use `response[0].realtor_id.pk` because this fiel has `foreignResource` in column option is set
204+
> 👆 Please note that we use `response[0].realtor_id.pk` because this field has `foreignResource` in column option is set
147205
> Otherwise you would use just `response[0].realtor_id`
148206
149-
Important notice: when using hook to filter out list of items for list page or list of items on dropdown makes a lot of sense (because gives ability to change filter of database request), using hook for show page is not reasonable:
207+
Important notice: Using hook to filter out list of items for list page or list of items for dropdown makes a lot of sense because gives ability to change filter of database request. However using hook for show page is not reasonable:
150208

151-
First of all it sematicaly better aligns with using allowedActions interface. For this particular case you must use [allowedActions.show](./05-limitingAccess.md#disable-showing-the-resource-based-on-owner)
209+
First of all it semantically better aligns with using `allowedActions` interface. For this particular case you must use [allowedActions.show](./05-limitingAccess.md#disable-showing-the-resource-based-on-owner)
152210

153-
Secondly limiting access from this hook will not prevent executing other hooks (e.g. beforeDatasourceRequest), when allowedActions check
211+
Secondly limiting access from this hook will not prevent executing other hooks (e.g. `beforeDatasourceRequest`), when allowedActions check
154212
always performed before any hooks and any database requests.
155213

156214
## All hooks

adminforth/documentation/docs/tutorial/03-Customization/05-limitingAccess.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11

22
# Limiting actions access
33

4+
As you might have noticed in diagrams from [adminforth hooks](./04-hooks.md) section of this tutorial, AdminForth checks `options.allowedActions` before executing any action. In this section we will show real-code examples of how to limit access to actions based on user role or record values.
5+
6+
Before we start it is worth to mention that callbacks or scalars defined in `allowedActions` are called/parsed not only before actual request but also before displaying buttons in the UI. So first time, when frontend loads any page of resource, it "calls" `allowedActions` to understand whether user has access to each function, and e.g. if it says that user can't delete record, AdminForth will not show delete icon in the UI:
7+
8+
![Resource any page request](image-21.png)
9+
10+
As you can see allowedAction callbacks are called in parallel in async manner. However it is important to keep them fast and not to make any slow operations in them, to keep UI responsive.
411

512
## Statically disable some action
613

314 KB
Loading
307 KB
Loading
331 KB
Loading
346 KB
Loading
387 KB
Loading
344 KB
Loading
337 KB
Loading
337 KB
Loading

0 commit comments

Comments
 (0)