@@ -13,50 +13,41 @@ export const AUTHORITIES = {
1313// handles getting account authorities from the cache or the database
1414// and also appends global authorities to the account authorities
1515// usually will be used when wanting to get the account authorities
16- export async function getAccountAuthorities (
16+ export async function getAccountAuthoritiesForScope (
1717 accountId : string ,
1818 scope : string ,
1919 forceDbQuery = false
2020) : Promise < string [ ] > {
2121 let authorities : string [ ] = [ ] ;
2222
23- // handle scope authorities
24- let currentScopeAuthorities : string [ ] = [ ] ;
25-
2623 if ( ! forceDbQuery ) {
27- currentScopeAuthorities =
28- getAccountAuthoritiesCacheForScope ( accountId , scope ) || [ ] ;
24+ authorities = getAccountAuthoritiesCacheForScope ( accountId , scope ) || [ ] ;
2925 }
3026
31- if ( forceDbQuery || currentScopeAuthorities . length === 0 ) {
27+ if ( forceDbQuery || authorities . length === 0 ) {
3228 // query from db
33- currentScopeAuthorities =
34- ( await queryAccountAuthoritiesById ( accountId , scope ) ) || [ ] ;
35- setAccountAuthoritiesCacheForScope (
36- accountId ,
37- scope ,
38- currentScopeAuthorities
39- ) ;
29+ authorities = ( await queryAccountAuthoritiesById ( accountId , scope ) ) || [ ] ;
30+ setAccountAuthoritiesCacheForScope ( accountId , scope , authorities ) ;
4031 }
4132
42- authorities = currentScopeAuthorities ;
43-
44- // handle global authorities
45- let globalAuthorities : string [ ] = [ ] ;
33+ return authorities ;
34+ }
4635
47- if ( ! forceDbQuery ) {
48- globalAuthorities =
49- getAccountAuthoritiesCacheForScope ( accountId , "global" ) || [ ] ;
50- }
36+ export async function getAccountAuthorities (
37+ accountId : string ,
38+ scopes : string [ ] ,
39+ forceDbQuery = false
40+ ) : Promise < string [ ] > {
41+ let authorities : string [ ] = [ ] ;
5142
52- if ( forceDbQuery || globalAuthorities . length === 0 ) {
53- globalAuthorities =
54- ( await queryAccountAuthoritiesById ( accountId , "global" ) ) || [ ] ;
55- setAccountAuthoritiesCacheForScope ( accountId , "global" , globalAuthorities ) ;
43+ for ( const scope of scopes ) {
44+ const currentScopeAuthorities = await getAccountAuthoritiesForScope (
45+ accountId ,
46+ scope ,
47+ forceDbQuery
48+ ) ;
49+ authorities = [ ...authorities , ...currentScopeAuthorities ] ;
5650 }
5751
58- // final result
59- authorities = [ ...authorities , ...globalAuthorities ] ;
60-
6152 return authorities ;
6253}
0 commit comments