You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: adminforth/documentation/docs/tutorial/03-Customization/04-hooks.md
+71-13Lines changed: 71 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,8 +9,43 @@ Hooks are used to:
9
9
- modify the fetched data before it is displayed in the list and show
10
10
- prevent the request to db depending on some condition (Better use [allowedActions](./05-limitingAccess.md) for this)
11
11
12
+
Every hook is executed when AdminForth frontend makes some internal API HTTP request to the backend.
12
13
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
+

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
+

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
+

47
+
48
+
### Example: modify the created object before it is saved to the database
14
49
15
50
Let's add reference to `adminUser` when user creates a new apartment:
16
51
@@ -38,9 +73,9 @@ import type { AdminUser } from 'adminforth';
@@ -52,11 +87,15 @@ import type { AdminUser } from 'adminforth';
52
87
}
53
88
```
54
89
90
+
In this way user who creates the apartment will be assigned as a realtor.
55
91
56
-
## Limiting access to user-related data
92
+
## List page flow
57
93
94
+
When user opens the list page, AdminForth makes a request to the backend to get the list of items.
58
95
59
-
### List of entities
96
+

97
+
98
+
### Example: limit access in list to user-related records
60
99
61
100
For example we can prevent the user to see Apartments created by other users. Superadmin user still can see all:
62
101
@@ -90,13 +129,17 @@ For example we can prevent the user to see Apartments created by other users. Su
90
129
}
91
130
```
92
131
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.
95
134
96
135
### Dropdown list of foreignResource
97
136
98
137
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:
100
143
101
144
```ts title='./resources/apartments.ts'
102
145
{
@@ -118,7 +161,22 @@ select dropdown with list of all Realtors. This might be a leak to get id's of o
118
161
119
162
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.
120
163
121
-
### Show entity
164
+
Flow diagram for dropdown list:
165
+
166
+

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
+

177
+
178
+
179
+
### Example show limiting:
122
180
123
181
```ts title='./resources/apartments.ts'
124
182
{
@@ -143,14 +201,14 @@ In our case we limit the dropdown list to show only the current user, however yo
143
201
}
144
202
```
145
203
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
147
205
> Otherwise you would use just `response[0].realtor_id`
148
206
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:
150
208
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)
152
210
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
154
212
always performed before any hooks and any database requests.
Copy file name to clipboardExpand all lines: adminforth/documentation/docs/tutorial/03-Customization/05-limitingAccess.md
+7Lines changed: 7 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,13 @@
1
1
2
2
# Limiting actions access
3
3
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
+

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.
0 commit comments