Skip to content

Commit a4b373d

Browse files
committed
feat: add "adminUserAuthorize" hook
1 parent 44ff2b6 commit a4b373d

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ export default class UserSoftDelete extends AdminForthPlugin {
4444
}
4545
);
4646

47+
const adminUserAuthorize = this.adminforth.config.auth.adminUserAuthorize;
48+
const adminUserAuthorizeArray = Array.isArray(adminUserAuthorize) ? adminUserAuthorize : [adminUserAuthorize];
49+
adminUserAuthorizeArray.unshift(
50+
async({ extra, adminUser }: { adminUser: AdminUser, response: IAdminForthHttpResponse, extra?: any} )=> {
51+
const rejectResult = {
52+
error: 'Your account is deactivated',
53+
allowed: false,
54+
};
55+
if (adminUser.dbUser[this.options.activeFieldName] === false) {
56+
return rejectResult;
57+
}
58+
}
59+
);
60+
4761
if ( !resourceConfig.options.pageInjections ) {
4862
resourceConfig.options.pageInjections = {};
4963
}
@@ -88,8 +102,6 @@ export default class UserSoftDelete extends AdminForthPlugin {
88102
let isAllowedToDeactivate = false;
89103
if ( typeof this.allowDisableFunc === "function" ) {
90104
isAllowedToDeactivate = await this.allowDisableFunc(adminUser);
91-
} else if (typeof this.allowDisableFunc === "boolean") {
92-
isAllowedToDeactivate = this.allowDisableFunc;
93105
}
94106
if ( isAllowedToDeactivate === false ) {
95107
return {ok: false, error: "Not allowed to deactivate user"}

package-lock.json

Lines changed: 11 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
"description": "",
1717
"devDependencies": {
1818
"@types/node": "latest",
19-
"typescript": "^5.7.3",
2019
"semantic-release": "^24.2.1",
21-
"semantic-release-slack-bot": "^4.0.2"
20+
"semantic-release-slack-bot": "^4.0.2",
21+
"typescript": "^5.7.3"
2222
},
2323
"dependencies": {
24-
"adminforth": "^2.4.0-next.191"
24+
"adminforth": "^2.4.0-next.227"
2525
},
2626
"release": {
2727
"plugins": [

types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ import type { AdminUser } from "adminforth";
22

33
export interface PluginOptions {
44
activeFieldName: string;
5-
canDeactivate?: (adminUser: AdminUser) => Promise<boolean>;
5+
canDeactivate: (adminUser: AdminUser) => Promise<boolean>;
66
}

0 commit comments

Comments
 (0)