@ -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 < String > getRoles ( ) {
List < String > roles = new ArrayList < > ( ) ;
Authentication authentication = SecurityContextHolder . getContext ( ) . getAuthentication ( ) ;
if ( authentication ! = null ) {
List < GrantedAuthority > authorities = ( List < GrantedAuthority > ) authentication . getAuthorities ( ) ;
if ( authorities ! = null ) {
authorities . forEach ( ( authority ) - > roles . add ( authority . getAuthority ( ) ) ) ;
}
}
return roles ;
}
}