2020-09-03 21:40:48 +02:00
|
|
|
package eu.dnetlib.uoaauthorizationlibrary.security;
|
|
|
|
|
2020-12-04 11:00:10 +01:00
|
|
|
import org.apache.log4j.Logger;
|
2020-11-03 13:25:53 +01:00
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
|
import org.springframework.security.core.GrantedAuthority;
|
|
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
2020-12-04 11:00:10 +01:00
|
|
|
import org.springframework.security.core.userdetails.User;
|
2020-09-03 21:40:48 +02:00
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
2020-11-03 13:25:53 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2020-09-03 21:40:48 +02:00
|
|
|
@Component(value = "AuthorizationService")
|
|
|
|
public class AuthorizationService {
|
2020-12-04 11:00:10 +01:00
|
|
|
private final Logger log = Logger.getLogger(this.getClass());
|
|
|
|
|
2020-09-03 21:40:48 +02:00
|
|
|
public final String PORTAL_ADMIN = "PORTAL_ADMINISTRATOR";
|
2020-12-04 11:00:10 +01:00
|
|
|
public final String ANONYMOUS_USER = "ROLE_ANONYMOUS";
|
2021-01-08 15:51:03 +01:00
|
|
|
public final String REGISTERED_USER = "REGISTERED_USER";
|
|
|
|
|
2020-09-03 21:40:48 +02:00
|
|
|
|
2021-01-27 14:47:55 +01:00
|
|
|
private String mapType(String type, boolean communityMap) {
|
|
|
|
if (type.equals("organization")) {
|
2020-11-03 13:25:53 +01:00
|
|
|
type = "institution";
|
2021-01-27 14:47:55 +01:00
|
|
|
} else if (type.equals("ri") && communityMap) {
|
2020-11-03 13:25:53 +01:00
|
|
|
type = "community";
|
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2020-09-03 21:40:48 +02:00
|
|
|
/**
|
|
|
|
* Type = FUNDER | COMMUNITY | INSTITUTION | PROJECT
|
2021-01-27 14:47:55 +01:00
|
|
|
*/
|
2020-09-03 21:40:48 +02:00
|
|
|
public String curator(String type) {
|
2021-01-27 14:47:55 +01:00
|
|
|
return "CURATOR_" + mapType(type, true).toUpperCase();
|
2020-09-03 21:40:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Type = FUNDER | COMMUNITY | INSTITUTION | PROJECT
|
2021-01-27 14:47:55 +01:00
|
|
|
* <p>
|
2020-09-03 21:40:48 +02:00
|
|
|
* Id = EE, EGI, etc
|
2021-01-27 14:47:55 +01:00
|
|
|
*/
|
2020-09-03 21:40:48 +02:00
|
|
|
public String manager(String type, String id) {
|
2021-01-27 14:47:55 +01:00
|
|
|
return mapType(type, true).toUpperCase() + "_" + id.toUpperCase() + "_MANAGER";
|
2020-09-03 21:40:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-27 14:47:55 +01:00
|
|
|
* Type = FUNDER | COMMUNITY | RI | INSTITUTION | PROJECT
|
|
|
|
* <p>
|
2020-09-03 21:40:48 +02:00
|
|
|
* Id = EE, EGI, etc
|
2021-01-27 14:47:55 +01:00
|
|
|
*/
|
2020-09-08 15:08:44 +02:00
|
|
|
public String member(String type, String id) {
|
2021-01-27 14:47:55 +01:00
|
|
|
return mapType(type, false).toUpperCase() + "_" + id.toUpperCase();
|
2020-11-03 13:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public List<String> getRoles() {
|
|
|
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
2021-01-27 14:47:55 +01:00
|
|
|
if (authentication != null) {
|
2020-11-03 13:25:53 +01:00
|
|
|
List<GrantedAuthority> authorities = (List<GrantedAuthority>) authentication.getAuthorities();
|
2021-01-27 14:47:55 +01:00
|
|
|
if (authorities != null) {
|
2020-12-04 11:00:10 +01:00
|
|
|
List<String> roles = new ArrayList<>();
|
|
|
|
authorities.forEach((authority) -> {
|
|
|
|
roles.add(authority.getAuthority());
|
|
|
|
});
|
|
|
|
return roles;
|
2020-11-03 13:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
2020-12-04 11:00:10 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getAaiId() {
|
|
|
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
2021-02-14 23:55:31 +01:00
|
|
|
if (authentication != null && (authentication.getPrincipal() instanceof User)) {
|
2020-12-04 11:00:10 +01:00
|
|
|
User user = (User) authentication.getPrincipal();
|
|
|
|
return user.getPassword();
|
|
|
|
}
|
|
|
|
return null;
|
2020-09-03 21:40:48 +02:00
|
|
|
}
|
|
|
|
}
|