[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.
This commit is contained in:
parent
a42f2db850
commit
a299ccbf6b
|
@ -1,7 +1,13 @@
|
||||||
package eu.dnetlib.uoaauthorizationlibrary.security;
|
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 org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Component(value = "AuthorizationService")
|
@Component(value = "AuthorizationService")
|
||||||
public class AuthorizationService {
|
public class AuthorizationService {
|
||||||
|
|
||||||
|
@ -9,12 +15,22 @@ public class AuthorizationService {
|
||||||
public final String PORTAL_ADMIN = "PORTAL_ADMINISTRATOR";
|
public final String PORTAL_ADMIN = "PORTAL_ADMINISTRATOR";
|
||||||
public final String USER_ADMIN = "USER_MANAGER";
|
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
|
* Type = FUNDER | COMMUNITY | INSTITUTION | PROJECT
|
||||||
*
|
*
|
||||||
* */
|
* */
|
||||||
public String curator(String type) {
|
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
|
* Id = EE, EGI, etc
|
||||||
* */
|
* */
|
||||||
public String manager(String type, String id) {
|
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
|
* Id = EE, EGI, etc
|
||||||
* */
|
* */
|
||||||
public String member(String type, String id) {
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue