package org.endeavourhealth.datavalidation.helpers;

import org.endeavourhealth.common.security.SecurityUtils;
import org.keycloak.representations.AccessToken;

import javax.ws.rs.core.SecurityContext;
import java.util.*;

public class Security {
    public Set<String> getUserAllowedOrganisationIdsFromSecurityContext(SecurityContext securityContext) {
        Set<String> orgs = new HashSet<>();

        AccessToken accessToken = SecurityUtils.getToken(securityContext);
        List<Map<String, Object>> orgGroups = (List)accessToken.getOtherClaims().getOrDefault("orgGroups", null);

        for (Object orgGroup1 : orgGroups) {
            Map<String, Object> orgGroup = (Map) orgGroup1;
            String orgGroupOrganisationId = (String) orgGroup.getOrDefault("organisationId", null);
            if (orgGroupOrganisationId != null)
                orgs.add(orgGroupOrganisationId);
        }

        return orgs;
    }
}

@@ -578,24 +578,61 @@
 s%22, null);%0A%0A
+        if (orgGroups != null) %7B%0A    
         for 
@@ -660,24 +660,28 @@
 rgGroups) %7B%0A
+    
             
@@ -740,16 +740,20 @@
         
+  
+  
 String o
@@ -836,32 +836,36 @@
 l);%0A            
+    
 if (orgGroupOrga
@@ -900,16 +900,20 @@
         
+  
+  
 orgs.add
@@ -938,16 +938,30 @@
 ionId);%0A
+            %7D%0A
         

Handle missing "orgGroups" claim