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 { public final String SUPER_ADMIN = "SUPER_ADMINISTRATOR"; 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 mapType(type).toUpperCase() + "_CURATOR"; } /** * Type = FUNDER | COMMUNITY | INSTITUTION | PROJECT * * Id = EE, EGI, etc * */ public String manager(String type, String id) { return mapType(type).toUpperCase() + "_" + id.toUpperCase() + "_MANAGER"; } /** * Type = FUNDER | COMMUNITY | INSTITUTION | PROJECT * * Id = EE, EGI, etc * */ public String member(String type, String id) { 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; } }