@@ -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,41 @@ export async function interpretResource(
71
70
} )
72
71
) ;
73
72
74
- return { allowedActions } ;
73
+ const resolveAllowed = 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 page : 'list' | 'show' | 'edit' = ( {
83
+ [ ActionCheckSource . ListRequest ] : 'list' ,
84
+ [ ActionCheckSource . ShowRequest ] : 'show' ,
85
+ [ ActionCheckSource . EditLoadRequest ] : 'edit' ,
86
+ } as const ) [ source ] ?? 'show' ;
87
+
88
+ const isColumnVisible = async ( col : any ) : Promise < boolean > => {
89
+ const si = col . showIn ;
90
+ if ( ! si ) return true ;
91
+
92
+ if ( Array . isArray ( si ) ) {
93
+ return si . includes ( 'all' ) || si . includes ( page ) ;
94
+ }
95
+
96
+ if ( si [ page ] !== undefined ) return await resolveAllowed ( si [ page ] ) ;
97
+ if ( si . all !== undefined ) return await resolveAllowed ( si . all ) ;
98
+ return true ;
99
+ } ;
100
+
101
+ const visibleColumnsEntries = await Promise . all (
102
+ resource . columns . map ( async ( col ) => [ col . name , await isColumnVisible ( col ) ] as const )
103
+ ) ;
104
+
105
+ const visibleColumns = Object . fromEntries ( visibleColumnsEntries ) as Record < string , boolean > ;
106
+
107
+ return { allowedActions, visibleColumns } ;
75
108
}
76
109
77
110
export default class AdminForthRestAPI implements IAdminForthRestAPI {
@@ -602,7 +635,7 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
602
635
meta . pk = body . filters . find ( ( f ) => f . field === resource . columns . find ( ( col ) => col . primaryKey ) . name ) ?. value ;
603
636
}
604
637
605
- const { allowedActions } = await interpretResource (
638
+ const { allowedActions, visibleColumns } = await interpretResource (
606
639
adminUser ,
607
640
resource ,
608
641
meta ,
@@ -804,7 +837,8 @@ export default class AdminForthRestAPI implements IAdminForthRestAPI {
804
837
// remove all columns which are not defined in resources, or defined but backendOnly
805
838
data . data . forEach ( ( item ) => {
806
839
Object . keys ( item ) . forEach ( ( key ) => {
807
- if ( ! resource . columns . find ( ( col ) => col . name === key ) || resource . columns . find ( ( col ) => col . name === key && col . backendOnly ) ) {
840
+ console . log ( visibleColumns ?. [ key ] , key ) ;
841
+ if ( ! resource . columns . find ( ( col ) => col . name === key ) || resource . columns . find ( ( col ) => col . name === key && col . backendOnly ) || visibleColumns ?. [ key ] === false ) {
808
842
delete item [ key ] ;
809
843
}
810
844
} )
0 commit comments