From a299ccbf6bc2a9dc8a16d360dfb5773ca2357ce4 Mon Sep 17 00:00:00 2001 From: "konstantina.galouni" Date: Tue, 3 Nov 2020 12:25:53 +0000 Subject: [PATCH] [Trunk | Auth-Library]: AuthorizationService.java: 1. Add private method "mapType()", to map "organization" to "institution" and "ri" to "community" for roles. 2. Add public method "getRoles()" to access roles (as Strings) from Granted Authorities. --- .../security/AuthorizationService.java | 34 +++++++++++++++++-- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/src/main/java/eu/dnetlib/uoaauthorizationlibrary/security/AuthorizationService.java b/src/main/java/eu/dnetlib/uoaauthorizationlibrary/security/AuthorizationService.java index 043be41..d4be59a 100644 --- a/src/main/java/eu/dnetlib/uoaauthorizationlibrary/security/AuthorizationService.java +++ b/src/main/java/eu/dnetlib/uoaauthorizationlibrary/security/AuthorizationService.java @@ -1,7 +1,13 @@ package eu.dnetlib.uoaauthorizationlibrary.security; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Component; +import java.util.ArrayList; +import java.util.List; + @Component(value = "AuthorizationService") public class AuthorizationService { @@ -9,12 +15,22 @@ public class AuthorizationService { public final String PORTAL_ADMIN = "PORTAL_ADMINISTRATOR"; public final String USER_ADMIN = "USER_MANAGER"; + private String mapType(String type) { + if(type.equals("organization")) { + type = "institution"; + } + if(type.equals("ri")) { + type = "community"; + } + return type; + } + /** * Type = FUNDER | COMMUNITY | INSTITUTION | PROJECT * * */ public String curator(String type) { - return type.toUpperCase() + "_CURATOR"; + return mapType(type).toUpperCase() + "_CURATOR"; } /** @@ -23,7 +39,7 @@ public class AuthorizationService { * Id = EE, EGI, etc * */ public String manager(String type, String id) { - return type.toUpperCase() + "_" + id.toUpperCase() + "_MANAGER"; + return mapType(type).toUpperCase() + "_" + id.toUpperCase() + "_MANAGER"; } /** @@ -32,6 +48,18 @@ public class AuthorizationService { * Id = EE, EGI, etc * */ public String member(String type, String id) { - return type.toUpperCase() + "_" + id.toUpperCase(); + return mapType(type).toUpperCase() + "_" + id.toUpperCase(); + } + + public List getRoles() { + List roles = new ArrayList<>(); + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + if(authentication != null) { + List authorities = (List) authentication.getAuthorities(); + if(authorities != null) { + authorities.forEach((authority) -> roles.add(authority.getAuthority())); + } + } + return roles; } }