82 lines
2.5 KiB
Java
82 lines
2.5 KiB
Java
package eu.dnetlib.uoaauthorizationlibrary.security;
|
|
|
|
import org.apache.log4j.Logger;
|
|
import org.springframework.security.core.Authentication;
|
|
import org.springframework.security.core.GrantedAuthority;
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
import org.springframework.security.core.userdetails.User;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
@Component(value = "AuthorizationService")
|
|
public class AuthorizationService {
|
|
private final Logger log = Logger.getLogger(this.getClass());
|
|
|
|
public final String PORTAL_ADMIN = "PORTAL_ADMINISTRATOR";
|
|
public final String ANONYMOUS_USER = "ROLE_ANONYMOUS";
|
|
public final String REGISTERED_USER = "REGISTERED_USER";
|
|
|
|
|
|
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 "CURATOR_"+mapType(type).toUpperCase();
|
|
}
|
|
|
|
/**
|
|
* 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<String> getRoles() {
|
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
if(authentication != null) {
|
|
List<GrantedAuthority> authorities = (List<GrantedAuthority>) authentication.getAuthorities();
|
|
if(authorities != null) {
|
|
List<String> roles = new ArrayList<>();
|
|
authorities.forEach((authority) -> {
|
|
roles.add(authority.getAuthority());
|
|
});
|
|
return roles;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public String getAaiId() {
|
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
if(authentication != null) {
|
|
User user = (User) authentication.getPrincipal();
|
|
return user.getPassword();
|
|
}
|
|
return null;
|
|
}
|
|
}
|