Skip to content

Commit 0702da5

Browse files
committed
feat: Enhance user deactivation logic and improve validation in UserSoftDelete plugin
1 parent 6da9520 commit 0702da5

File tree

2 files changed

+31
-20
lines changed

2 files changed

+31
-20
lines changed

custom/DisableButton.vue

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,30 @@
1919

2020
<script lang="ts" setup>
2121
import { Dialog, Tooltip } from '@/afcl';
22-
import { ref } from 'vue';
22+
import { ref, onMounted } from 'vue';
2323
import { AdminUser, type AdminForthResourceCommon } from '@/types';
2424
import adminforth from "@/adminforth"
2525
import { callAdminForthApi } from '@/utils';
26+
import { AdminForthFilterOperators } from '@/types/Common';
2627
2728
const confirmDialog = ref(null);
2829
30+
onMounted(async () => {
31+
await new Promise((resolve) => setTimeout(resolve, 50));
32+
33+
adminforth?.list?.updateFilter?.({
34+
field: props.meta.field,
35+
operator: AdminForthFilterOperators.EQ,
36+
value: true,
37+
});
38+
});
39+
2940
function openDialog() {
3041
if ( props.checkboxes.length !== 1 ) {
31-
if (props.checkboxes.lenght > 1) {
42+
if (props.checkboxes.length > 1) {
3243
adminforth.alert({message: "Select only one account to deactivate", variant: "warning"})
3344
} else {
34-
adminforth.alert({message: "Select at least to deactivate", variant: "warning"})
45+
adminforth.alert({message: "Select at least one account to deactivate", variant: "warning"})
3546
}
3647
} else {
3748
confirmDialog.value.open()

index.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { AdminForthPlugin, AdminForthDataTypes, AdminForthResourcePages, Filters} from "adminforth";
22
import type { IAdminForth, IHttpServer, AdminForthResourceColumn, AdminForthResource, IAdminForthHttpResponse, AdminUser, AdminForthComponentDeclaration } from "adminforth";
33
import type { PluginOptions } from './types.js';
4+
import { error } from "console";
45

56
export default class UserSoftDelete extends AdminForthPlugin {
67
options: PluginOptions;
@@ -25,27 +26,12 @@ export default class UserSoftDelete extends AdminForthPlugin {
2526

2627
resourceConfig.options.allowedActions.delete = false;
2728

28-
resourceConfig.columns.push({
29-
name: this.options.activeFieldName,
30-
label: 'Is Active',
31-
type: AdminForthDataTypes.BOOLEAN,
32-
showIn: {
33-
[AdminForthResourcePages.list]: true,
34-
[AdminForthResourcePages.edit]: true,
35-
[AdminForthResourcePages.create]: false,
36-
[AdminForthResourcePages.filter]: true,
37-
[AdminForthResourcePages.show]: true,
38-
},
39-
filterOptions: {
40-
multiselect: false,
41-
}
42-
});
43-
4429
const beforeLoginConfirmation = this.adminforth.config.auth.beforeLoginConfirmation;
4530
const beforeLoginConfirmationArray = Array.isArray(beforeLoginConfirmation) ? beforeLoginConfirmation : [beforeLoginConfirmation];
4631
beforeLoginConfirmationArray.unshift(
4732
async({ extra, adminUser }: { adminUser: AdminUser, response: IAdminForthHttpResponse, extra?: any} )=> {
4833
const rejectResult = {
34+
error: 'Your account is deactivated',
4935
body:{
5036
allowedLogin: false,
5137
redirectTo: '/login',
@@ -68,14 +54,24 @@ export default class UserSoftDelete extends AdminForthPlugin {
6854
resourceConfig.options.pageInjections.list.threeDotsDropdownItems = [];
6955
}
7056
(resourceConfig.options.pageInjections.list.threeDotsDropdownItems as AdminForthComponentDeclaration[]).push(
71-
{ file: this.componentPath('DisableButton.vue'), meta: { pluginInstanceId: this.pluginInstanceId } }
57+
{ file: this.componentPath('DisableButton.vue'), meta: { pluginInstanceId: this.pluginInstanceId, field: this.options.activeFieldName } }
7258
);
7359

7460
// simply modify resourceConfig or adminforth.config. You can get access to plugin options via this.options;
7561
}
7662

7763
validateConfigAfterDiscover(adminforth: IAdminForth, resourceConfig: AdminForthResource) {
7864
// optional method where you can safely check field types after database discovery was performed
65+
if (!this.options.activeFieldName) {
66+
throw new Error(`Option activeFieldName is required for UserSoftDelete plugin on resource ${this.resourceConfig.resourceId}`);
67+
}
68+
const column = this.resourceConfig.columns.find(f => f.name === this.options.activeFieldName);
69+
if (!column) {
70+
throw new Error(`Field ${this.options.activeFieldName} not found in resource ${this.resourceConfig.resourceId}`);
71+
}
72+
if (![AdminForthDataTypes.BOOLEAN].includes(column!.type!)) {
73+
throw new Error(`Field ${this.options.activeFieldName} should be boolean, but it is ${column!.type}`);
74+
}
7975
}
8076

8177
instanceUniqueRepresentation(pluginOptions: any) : string {
@@ -109,6 +105,10 @@ export default class UserSoftDelete extends AdminForthPlugin {
109105
throw new Error(`User with id ${id} not found`);
110106
}
111107

108+
if (oldUser[this.options.activeFieldName] === false) {
109+
return {ok: false, error: "User is already deactivated"}
110+
}
111+
112112
const newUser = { ...oldUser, [this.options.activeFieldName]: false };
113113

114114
await this.adminforth.updateResourceRecord({

0 commit comments

Comments
 (0)