1+ import { AdminForthPlugin , AdminForthDataTypes , AdminForthResourcePages } from "adminforth" ;
2+ import type { IAdminForth , IHttpServer , AdminForthResourceColumn , AdminForthResource , IAdminForthHttpResponse , AdminUser , AdminForthComponentDeclaration } from "adminforth" ;
3+ import type { PluginOptions } from './types.js' ;
4+
5+ export default class UserSoftDelete extends AdminForthPlugin {
6+ options : PluginOptions ;
7+
8+ constructor ( options : PluginOptions ) {
9+ super ( options , import . meta. url ) ;
10+ this . options = options ;
11+ }
12+
13+ async modifyResourceConfig ( adminforth : IAdminForth , resourceConfig : AdminForthResource ) {
14+ super . modifyResourceConfig ( adminforth , resourceConfig ) ;
15+ let allowDisableFunc ;
16+ if ( this . options . canDeactivate ) {
17+ //console.log('Using canDeactivate from plugin options');
18+ allowDisableFunc = this . options . canDeactivate ;
19+ } else if ( resourceConfig . options . allowedActions . delete && typeof resourceConfig . options . allowedActions . delete === 'function' ) {
20+ //console.log('Using existing delete permission function from resource config');
21+ allowDisableFunc = resourceConfig . options . allowedActions . delete ;
22+ } else if ( resourceConfig . options . allowedActions . delete && typeof resourceConfig . options . allowedActions . delete === 'boolean' ) {
23+ //console.log('Using existing delete permission boolean from resource config:', resourceConfig.options.allowedActions.delete);
24+ allowDisableFunc = async ( ) => resourceConfig . options . allowedActions . delete ;
25+ } else {
26+ //console.log('No delete permission function found, defaulting to allow all');
27+ allowDisableFunc = async ( ) => true ;
28+ }
29+
30+ resourceConfig . options . allowedActions . delete = false ;
31+
32+ resourceConfig . columns . push ( {
33+ name : this . options . activeFieldName ,
34+ label : 'Is Active' ,
35+ type : AdminForthDataTypes . BOOLEAN ,
36+ showIn : {
37+ [ AdminForthResourcePages . list ] : true ,
38+ [ AdminForthResourcePages . edit ] : true ,
39+ [ AdminForthResourcePages . create ] : false ,
40+ [ AdminForthResourcePages . filter ] : true ,
41+ [ AdminForthResourcePages . show ] : true ,
42+ } ,
43+ filterOptions : {
44+ multiselect : false ,
45+ }
46+ } ) ;
47+
48+ const beforeLoginConfirmation = this . adminforth . config . auth . beforeLoginConfirmation ;
49+ const beforeLoginConfirmationArray = Array . isArray ( beforeLoginConfirmation ) ? beforeLoginConfirmation : [ beforeLoginConfirmation ] ;
50+ beforeLoginConfirmationArray . unshift (
51+ async ( { extra, adminUser } : { adminUser : AdminUser , response : IAdminForthHttpResponse , extra ?: any } ) => {
52+ const rejectResult = {
53+ body :{
54+ allowedLogin : false ,
55+ redirectTo : '/login' ,
56+ } ,
57+ ok : true
58+ } ;
59+ if ( adminUser . dbUser [ this . options . activeFieldName ] === false ) {
60+ return rejectResult ;
61+ }
62+ }
63+ ) ;
64+
65+ if ( ! resourceConfig . options . pageInjections ) {
66+ resourceConfig . options . pageInjections = { } ;
67+ }
68+ if ( ! resourceConfig . options . pageInjections . list ) {
69+ resourceConfig . options . pageInjections . list = { } ;
70+ }
71+ if ( ! resourceConfig . options . pageInjections . list . threeDotsDropdownItems ) {
72+ resourceConfig . options . pageInjections . list . threeDotsDropdownItems = [ ] ;
73+ }
74+ ( resourceConfig . options . pageInjections . list . threeDotsDropdownItems as AdminForthComponentDeclaration [ ] ) . push (
75+ { file : this . componentPath ( 'DisableButton.vue' ) }
76+ ) ;
77+
78+ // simply modify resourceConfig or adminforth.config. You can get access to plugin options via this.options;
79+ }
80+
81+ validateConfigAfterDiscover ( adminforth : IAdminForth , resourceConfig : AdminForthResource ) {
82+ // optional method where you can safely check field types after database discovery was performed
83+ }
84+
85+ instanceUniqueRepresentation ( pluginOptions : any ) : string {
86+ // optional method to return unique string representation of plugin instance.
87+ // Needed if plugin can have multiple instances on one resource
88+ return `single` ;
89+ }
90+
91+ setupEndpoints ( server : IHttpServer ) {
92+ server . endpoint ( {
93+ method : 'POST' ,
94+ path : `/plugin/${ this . pluginInstanceId } /example` ,
95+ handler : async ( { body } ) => {
96+ const { name } = body ;
97+ return { hey : `Hello ${ name } ` } ;
98+ }
99+ } ) ;
100+ }
101+
102+ }
0 commit comments