@@ -29,10 +29,11 @@ export async function interpretResource(
29
29
meta : any ,
30
30
source : ActionCheckSource ,
31
31
adminforth : IAdminForth
32
- ) : Promise < { allowedActions : AllowedActionsResolved } > {
32
+ ) : Promise < { allowedActions : AllowedActionsResolved ; visibleColumns : Record < string , boolean > } > {
33
33
if ( process . env . HEAVY_DEBUG ) {
34
34
console . log ( '🪲Interpreting resource' , resource . resourceId , source , 'adminUser' , adminUser ) ;
35
35
}
36
+
36
37
const allowedActions = { } as AllowedActionsResolved ;
37
38
38
39
// we need to compute only allowed actions for this source:
@@ -61,8 +62,6 @@ export async function interpretResource(
61
62
allowedActions [ key ] = false ;
62
63
return ;
63
64
}
64
-
65
- // if callable then call
66
65
if ( typeof value === 'function' ) {
67
66
allowedActions [ key ] = await value ( { adminUser, resource, meta, source, adminforth } ) ;
68
67
} else {
@@ -71,7 +70,47 @@ export async function interpretResource(
71
70
} )
72
71
) ;
73
72
74
- return { allowedActions } ;
73
+ const resolveVisible = async ( val : any ) : Promise < boolean > => {
74
+ if ( typeof val === 'boolean' ) return val ;
75
+ if ( typeof val === 'function' ) {
76
+ const r = val ( { adminUser, resource, meta, source, adminforth } ) ;
77
+ return r instanceof Promise ? await r : ! ! r ;
78
+ }
79
+ return true ;
80
+ } ;
81
+
82
+ const pageMap = {
83
+ [ ActionCheckSource . ListRequest ] : 'list' ,
84
+ [ ActionCheckSource . ShowRequest ] : 'show' ,
85
+ [ ActionCheckSource . EditLoadRequest ] : 'edit' ,
86
+ } as const ;
87
+
88
+ const page = pageMap [ source as keyof typeof pageMap ] as ( 'list' | 'show' | 'edit' ) | undefined ;
89
+
90
+ if ( ! page ) {
91
+ return { allowedActions, visibleColumns : { } } ;
92
+ }
93
+
94
+ const isColumnVisible = async ( col : any ) : Promise < boolean > => {
95
+ const si = col . showIn ;
96
+ if ( ! si ) return true ;
97
+
98
+ if ( Array . isArray ( si ) ) {
99
+ return si . includes ( 'all' ) || si . includes ( page ) ;
100
+ }
101
+
102
+ if ( si [ page ] !== undefined ) return await resolveVisible ( si [ page ] ) ;
103
+ if ( si . all !== undefined ) return await resolveVisible ( si . all ) ;
104
+ return true ;
105
+ } ;
106
+
107
+ const visibleColumnsEntries = await Promise . all (
108
+ resource . columns . map ( async ( col ) => [ col . name , await isColumnVisible ( col ) ] as const )
109
+ ) ;
110
+
111
+ const visibleColumns = Object . fromEntries ( visibleColumnsEntries ) as Record < string , boolean > ;
112
+
113
+ return { allowedActions, visibleColumns } ;
75
114
}
76
115
77
116
export default class AdminForthRestAPI implements IAdminForthRestAPI {
@@ -602,7 +641,7 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
602
641
meta . pk = body . filters . find ( ( f ) => f . field === resource . columns . find ( ( col ) => col . primaryKey ) . name ) ?. value ;
603
642
}
604
643
605
- const { allowedActions } = await interpretResource (
644
+ const { allowedActions, visibleColumns } = await interpretResource (
606
645
adminUser ,
607
646
resource ,
608
647
meta ,
@@ -804,7 +843,8 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
804
843
// remove all columns which are not defined in resources, or defined but backendOnly
805
844
data . data . forEach ( ( item ) => {
806
845
Object . keys ( item ) . forEach ( ( key ) => {
807
- if ( ! resource . columns . find ( ( col ) => col . name === key ) || resource . columns . find ( ( col ) => col . name === key && col . backendOnly ) ) {
846
+ console . log ( visibleColumns ?. [ key ] , key ) ;
847
+ if ( ! resource . columns . find ( ( col ) => col . name === key ) || resource . columns . find ( ( col ) => col . name === key && col . backendOnly ) || visibleColumns ?. [ key ] === false ) {
808
848
delete item [ key ] ;
809
849
}
810
850
} )
0 commit comments