1+ import { AuthData } from "@/contexts/AuthContext" ;
2+ import { SystemConfig } from "@/models/systemConfig" ;
13import { type ClassValue , clsx } from "clsx"
24import { twMerge } from "tailwind-merge"
35
@@ -70,6 +72,42 @@ export function isVersionLower(version: string, target: string) {
7072 return false ;
7173}
7274
75+ export function getUserVOs ( authData : AuthData ) : string [ ] {
76+ const vos : string [ ] = [ ] ;
77+ if ( authData . egiSession ?. eduperson_entitlement ) {
78+ authData . egiSession . eduperson_entitlement . forEach ( ( entitlement ) => {
79+ // "urn:mace:egi.eu:group:vo.example.eu:role=member#aai.egi.eu"
80+ const match = entitlement . match ( / ^ u r n : m a c e : e g i \. e u : g r o u p : ( v o \. .+ ?) : r o l e = m e m b e r (?: # | $ ) / ) ;
81+ if ( match && match [ 1 ] ) {
82+ vos . push ( match [ 1 ] ) ;
83+ }
84+ } ) ;
85+ }
86+ if ( authData . egiSession ?. group_membership ) {
87+ authData . egiSession . group_membership . forEach ( ( group ) => {
88+ // "/employees/vo.example.eu"
89+ const match = group . match ( / ^ \/ .* \/ ( v o \. .+ ) $ / ) ;
90+ if ( match && match [ 1 ] ) {
91+ vos . push ( match [ 1 ] ) ;
92+ }
93+ } ) ;
94+ }
95+ return vos ;
96+ }
97+
98+ export function getAllowedVOs ( systemConfig : { config : SystemConfig } | null , authData : AuthData ) : string [ ] {
99+ if ( ! systemConfig || ! systemConfig . config || ! systemConfig . config . oidc_groups || systemConfig . config . oidc_groups . length === 0 ) return [ ] ;
100+ // If user is oscar, return all allowed VOs from system config
101+ if ( authData . user === "oscar" ) return systemConfig . config . oidc_groups ;
102+ // Get user's VOs
103+ const userVOs = getUserVOs ( authData ) ;
104+ // If user has no VOs, return all allowed VOs from system config
105+ if ( userVOs . length === 0 ) return systemConfig . config . oidc_groups ;
106+ // Filter allowed VOs based on user's VOs
107+ const filteredVOs = systemConfig . config . oidc_groups . filter ( ( vo ) => userVOs . includes ( vo ) ) ;
108+ return filteredVOs ;
109+ }
110+
73111function sleep ( ms : number ) {
74112 return new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
75- }
113+ }
0 commit comments