@@ -15,20 +15,15 @@ import { Chalk, type ChalkInstance } from 'chalk';
15
15
import Table from 'cli-table3' ;
16
16
import { Command , Option } from 'clipanion' ;
17
17
import { startCase } from 'lodash' ;
18
- import { DateTime } from 'luxon' ;
19
18
import fs from 'node:fs/promises' ;
20
19
import { type LiteralUnion } from 'type-fest' ;
21
20
import { inspect } from 'util' ;
22
21
import xlsx from 'xlsx' ;
23
- import {
24
- type EnhancedResource ,
25
- firstOr ,
26
- type ID ,
27
- Role ,
28
- type Session ,
29
- } from '~/common' ;
22
+ import { type EnhancedResource , firstOr , Role } from '~/common' ;
30
23
import { searchCamelCase } from '~/common/search-camel-case' ;
31
- import { InjectableCommand , type ResourceLike , ResourcesHost } from '~/core' ;
24
+ import { Identity } from '~/core/authentication' ;
25
+ import { InjectableCommand } from '~/core/cli' ;
26
+ import { type ResourceLike , ResourcesHost } from '~/core/resources' ;
32
27
import {
33
28
ChildListAction ,
34
29
ChildSingleAction ,
@@ -44,6 +39,7 @@ type AnyResource = EnhancedResource<any>;
44
39
@Injectable ( )
45
40
export class PolicyDumper {
46
41
constructor (
42
+ private readonly identity : Identity ,
47
43
private readonly resources : ResourcesHost ,
48
44
private readonly executor : PolicyExecutor ,
49
45
) { }
@@ -172,51 +168,45 @@ export class PolicyDumper {
172
168
resource : AnyResource ,
173
169
options : { props : boolean | ReadonlySet < string > } ,
174
170
) : DumpedRow [ ] {
175
- const session : Session = {
176
- token : 'system' ,
177
- issuedAt : DateTime . now ( ) ,
178
- userId : 'anonymous' as ID ,
179
- anonymous : false ,
180
- roles : [ `global:${ role } ` ] ,
181
- } ;
182
- const resolve = ( action : string , prop ?: string ) =>
183
- this . executor . resolve ( {
184
- session,
185
- resource,
186
- calculatedAsCondition : true ,
187
- optimizeConditions : true ,
188
- action,
189
- prop,
190
- } ) ;
191
- return [
192
- {
193
- role,
194
- resource,
195
- edge : undefined ,
196
- ...mapValues . fromList ( ResourceAction , ( action ) => resolve ( action ) )
197
- . asRecord ,
198
- } ,
199
- ...( options . props !== false
200
- ? ( [
201
- [ resource . securedPropsPlusExtra , PropAction ] ,
202
- [ resource . childSingleKeys , ChildSingleAction ] ,
203
- [ resource . childListKeys , ChildListAction ] ,
204
- ] as const )
205
- : [ ]
206
- ) . flatMap ( ( [ set , actions ] ) =>
207
- [ ...set ]
208
- . filter (
209
- ( p ) => typeof options . props === 'boolean' || options . props . has ( p ) ,
210
- )
211
- . map ( ( prop ) => ( {
212
- role,
213
- resource,
214
- edge : prop ,
215
- ...mapValues . fromList ( actions , ( action ) => resolve ( action , prop ) )
216
- . asRecord ,
217
- } ) ) ,
218
- ) ,
219
- ] ;
171
+ return this . identity . asRole ( role , ( ) => {
172
+ const resolve = ( action : string , prop ?: string ) =>
173
+ this . executor . resolve ( {
174
+ resource,
175
+ calculatedAsCondition : true ,
176
+ optimizeConditions : true ,
177
+ action,
178
+ prop,
179
+ } ) ;
180
+ return [
181
+ {
182
+ role,
183
+ resource,
184
+ edge : undefined ,
185
+ ...mapValues . fromList ( ResourceAction , ( action ) => resolve ( action ) )
186
+ . asRecord ,
187
+ } ,
188
+ ...( options . props !== false
189
+ ? ( [
190
+ [ resource . securedPropsPlusExtra , PropAction ] ,
191
+ [ resource . childSingleKeys , ChildSingleAction ] ,
192
+ [ resource . childListKeys , ChildListAction ] ,
193
+ ] as const )
194
+ : [ ]
195
+ ) . flatMap ( ( [ set , actions ] ) =>
196
+ [ ...set ]
197
+ . filter (
198
+ ( p ) => typeof options . props === 'boolean' || options . props . has ( p ) ,
199
+ )
200
+ . map ( ( prop ) => ( {
201
+ role,
202
+ resource,
203
+ edge : prop ,
204
+ ...mapValues . fromList ( actions , ( action ) => resolve ( action , prop ) )
205
+ . asRecord ,
206
+ } ) ) ,
207
+ ) ,
208
+ ] ;
209
+ } ) ;
220
210
}
221
211
}
222
212
0 commit comments