11import { AdminForthPlugin , AdminForthDataTypes , AdminForthResourcePages , Filters } from "adminforth" ;
22import type { IAdminForth , IHttpServer , AdminForthResourceColumn , AdminForthResource , IAdminForthHttpResponse , AdminUser , AdminForthComponentDeclaration } from "adminforth" ;
33import type { PluginOptions } from './types.js' ;
4+ import { error } from "console" ;
45
56export 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