89 lines
2.9 KiB
Java
89 lines
2.9 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, boolean communityMap) {
|
|
if (type.equals("organization")) {
|
|
type = "institution";
|
|
} else if (type.equals("ri") && communityMap) {
|
|
type = "community";
|
|
}
|
|
return type;
|
|
}
|
|
|
|
/**
|
|
* Type = FUNDER | COMMUNITY | INSTITUTION | PROJECT
|
|
*/
|
|
public String curator(String type) {
|
|
return "CURATOR_" + mapType(type, true).toUpperCase();
|
|
}
|
|
|
|
/**
|
|
* Type = FUNDER | COMMUNITY | INSTITUTION | PROJECT
|
|
* <p>
|
|
* Id = EE, EGI, etc
|
|
*/
|
|
public String manager(String type, String id) {
|
|
return mapType(type, true).toUpperCase() + "_" + id.toUpperCase() + "_MANAGER";
|
|
}
|
|
|
|
/**
|
|
* Type = FUNDER | COMMUNITY | RI | INSTITUTION | PROJECT
|
|
* <p>
|
|
* Id = EE, EGI, etc
|
|
*/
|
|
public String member(String type, String id) {
|
|
return mapType(type, false).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 && (authentication.getPrincipal() instanceof User)) {
|
|
User user = (User) authentication.getPrincipal();
|
|
return user.getPassword();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public String getEmail() {
|
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
if (authentication != null && (authentication.getPrincipal() instanceof User)) {
|
|
User user = (User) authentication.getPrincipal();
|
|
return user.getUsername();
|
|
}
|
|
return null;
|
|
}
|
|
}
|