Skip to content

Commit d3bfe09

Browse files
committed
dev-demo: add user-soft-delete plugin
AdminForth/1777/image
1 parent d9777b2 commit d3bfe09

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
-- RedefineTables
2+
PRAGMA defer_foreign_keys=ON;
3+
PRAGMA foreign_keys=OFF;
4+
CREATE TABLE "new_adminuser" (
5+
"id" TEXT NOT NULL PRIMARY KEY,
6+
"email" TEXT NOT NULL,
7+
"password_hash" TEXT NOT NULL,
8+
"role" TEXT NOT NULL,
9+
"created_at" DATETIME NOT NULL,
10+
"secret2fa" TEXT,
11+
"responsible_person" TEXT,
12+
"avatar" TEXT,
13+
"is_active" BOOLEAN NOT NULL DEFAULT true
14+
);
15+
INSERT INTO "new_adminuser" ("avatar", "created_at", "email", "id", "password_hash", "responsible_person", "role", "secret2fa") SELECT "avatar", "created_at", "email", "id", "password_hash", "responsible_person", "role", "secret2fa" FROM "adminuser";
16+
DROP TABLE "adminuser";
17+
ALTER TABLE "new_adminuser" RENAME TO "adminuser";
18+
CREATE UNIQUE INDEX "adminuser_email_key" ON "adminuser"("email");
19+
CREATE INDEX "adminuser_is_active_idx" ON "adminuser"("is_active");
20+
PRAGMA foreign_keys=ON;
21+
PRAGMA defer_foreign_keys=OFF;

dev-demo/migrations/prisma/sqlite/schema.prisma

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ model adminuser {
1515
secret2fa String?
1616
responsible_person String?
1717
avatar String?
18+
19+
is_active Boolean @default(true)
20+
@@index([is_active])
1821
}
1922

2023
model cars {

dev-demo/resources/adminuser.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import UploadPlugin from '../../plugins/adminforth-upload/index.js';
77
import AdminForthStorageAdapterLocalFilesystem from "../../adapters/adminforth-storage-adapter-local/index.js";
88
import OpenSignupPlugin from '../../plugins/adminforth-open-signup/index.js';
99
import DashboardPlugin from '../../plugins/adminforth-dashboard/index.js';
10+
import UserSoftDelete from '../../plugins/adminforth-user-soft-delete/index.js';
1011
import KeyValueAdapterRam from '../../adapters/adminforth-key-value-adapter-ram/index.js';
1112
import OAuthPlugin from './configs/oauthPluginConfig.js';
1213

@@ -98,6 +99,22 @@ export default {
9899
type: AdminForthDataTypes.STRING,
99100
showIn: ["show", "edit", "create" ],
100101
},
102+
{
103+
name: "is_active",
104+
type: AdminForthDataTypes.BOOLEAN,
105+
label: "Is Active",
106+
fillOnCreate: () => true,
107+
filterOptions: {
108+
multiselect: false,
109+
},
110+
showIn: {
111+
list: true,
112+
filter: true,
113+
show: true,
114+
create: false,
115+
edit: true,
116+
},
117+
},
101118
],
102119
plugins: [
103120
new TwoFactorsAuthPlugin (
@@ -176,6 +193,16 @@ export default {
176193
new DashboardPlugin({
177194
dashboardConfigsResourceId: 'dashboard_configs',
178195
}),
196+
new UserSoftDelete({
197+
activeFieldName: "is_active",
198+
//in canDeactivate we pass a function, that specify adminusers roles, which can seactivate other adminusers
199+
canDeactivate: async (adminUser: AdminUser) => {
200+
if (adminUser.dbUser.role === "superadmin") {
201+
return true;
202+
}
203+
return false;
204+
}
205+
}),
179206
],
180207
hooks: {
181208
create: {

0 commit comments

Comments
 (0)