Skip to content

Commit b152f94

Browse files
committed
Merge branch 'next' of github.com:devforth/adminforth into next
2 parents 595dbd3 + 899a08d commit b152f94

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed

adminforth/documentation/docs/tutorial/01-helloWorld.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ Create `index.ts` file in root directory with following content:
131131

132132
```ts title="./index.ts"
133133
import express from 'express';
134-
import AdminForth, { AdminForthDataTypes, AdminUser, Filters } from 'adminforth';
134+
import AdminForth, { AdminForthDataTypes, Filters } from 'adminforth';
135+
import type { AdminForthResourceInput, AdminForthResource, AdminUser } from 'adminforth';
135136

136137
export const admin = new AdminForth({
137138
baseUrl: '',
@@ -208,6 +209,26 @@ export const admin = new AdminForth({
208209
},
209210
{ name: 'passwordHash', backendOnly: true, showIn: { all: false } }
210211
],
212+
hooks: {
213+
create: {
214+
beforeSave: async ({ record, adminUser, resource }: { record: any, adminUser: AdminUser, resource: AdminForthResource }) => {
215+
record.password_hash = await AdminForth.Utils.generatePasswordHash(record.password);
216+
return { ok: true };
217+
}
218+
},
219+
edit: {
220+
beforeSave: async ({ oldRecord, updates, adminUser, resource }: { oldRecord: any, updates: any, adminUser: AdminUser, resource: AdminForthResource }) => {
221+
console.log('Updating user', updates);
222+
if (oldRecord.id === adminUser.dbUser.id && updates.role) {
223+
return { ok: false, error: 'You cannot change your own role' };
224+
}
225+
if (updates.password) {
226+
updates.password_hash = await AdminForth.Utils.generatePasswordHash(updates.password);
227+
}
228+
return { ok: true }
229+
},
230+
},
231+
}
211232
},
212233
{
213234
table: 'post',

adminforth/documentation/docs/tutorial/03-Customization/06-customPages.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,10 +457,18 @@ You can navigate user to this page using any router link, e.g.:
457457
</template>
458458
```
459459
460+
Add to your `<script setup>` section:
461+
460462
```ts
461463
import { Link } from '@/afcl';
462464
```
463465
466+
If you set `customLayout: true` in the `meta` object, it will not include default layout like sidebar and header, so you can create your own layout for this page.
467+
468+
> **Redirects to login page**: Any route which has sidebar and header (e.g. default CRUD pages or menu item with `component`) uses internal AdminForth REST API to fetch menu items and user information, so it passes authentication check and if authentication cookie is not provided or has expired JWT user gets redirected to the login page.
469+
> In case if you set `customLayout: true`, it will not call these APIs so user will not be automatically redirected to the login page in case of expired or not-provided authentication cookie. That feature allows you to implement public pages without authentication, e.g. Terms of Service, Privacy Policy and many others. In case if you need to check if user is logged in just call any custom API which has `admin.express.authorize` middleware. Obviously for public pages you should create API endpoint WITHOUT `admin.express.authorize` middleware.
470+
471+
464472
### Passing meta attributes to the page
465473
466474
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
Allows to add an inline show of connected foreign resource records on a adminforth show page.
3+
4+
Foreign inline show plugin allows to display a show of items from a foreign table in the show view.
5+
6+
## Usage
7+
8+
9+
Import plugin:
10+
11+
```bash
12+
npm i @adminforth/foreign-inline-show --save
13+
```
14+
15+
```ts title="./resources/adminuser.ts"
16+
import ForeignInlineShowPlugin from '@adminforth/foreign-inline-show';
17+
import { AdminForthResource, AdminForthResourceColumn } from 'adminforth';
18+
```
19+
20+
21+
In [Getting Started](<../001-gettingStarted.md>) we created a `'aparts'` resource which has a field `'realtor_id'`.
22+
This field refers to record from `'adminuser'` resource. To remind you, we configured this relation using `foreignResource` setting in the column configuration:
23+
24+
```typescript title="./resources/apartments.ts"
25+
//
26+
export default {
27+
resourceId: 'aparts',
28+
...
29+
columns: [
30+
...
31+
{
32+
name: 'realtor_id',
33+
foreignResource: {
34+
resourceId: 'adminuser', // this means that aparts.realtor_id refers to primary key of 'adminuser' resource
35+
// this is Many-To-One relatin: many aparts can refer to one user
36+
}
37+
}
38+
],
39+
}
40+
```
41+
42+
This means that we can display a show of user in the apartments show view.
43+
44+
Add to your `'apartments'` resource configuration the plugin instance:
45+
46+
```ts title="./resources/apartments.ts"
47+
{
48+
...
49+
resourceId: 'aparts',
50+
...
51+
//diff-add
52+
plugins: [
53+
//diff-add
54+
new ForeignInlineShowPlugin({
55+
//diff-add
56+
foreignResourceId: 'users',
57+
//diff-add
58+
}),
59+
//diff-add
60+
],
61+
}
62+
```
63+
64+
![alt text](ForeignInlineShow.png)
65+
66+
> 👆 To make plugin work, the specified resource (defined with `foreignResourceId`) should have one (and only one) column that refers to the current resource on which you add a plugin.
67+
> In our case we add plugin to `adminuser` resource, so the `aparts` resource should have one column with `foreignResource.resourceId` equal to `adminuser` resourceId.
160 KB
Loading

0 commit comments

Comments
 (0)