1
|
package eu.dnetlib.openaire.usermanagement.utils;
|
2
|
|
3
|
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
|
4
|
import org.springframework.security.core.Authentication;
|
5
|
import org.springframework.security.core.GrantedAuthority;
|
6
|
import org.springframework.security.core.context.SecurityContextHolder;
|
7
|
import org.springframework.stereotype.Component;
|
8
|
|
9
|
import java.util.ArrayList;
|
10
|
import java.util.List;
|
11
|
import java.util.stream.Collectors;
|
12
|
|
13
|
@Component("AuthorizationService")
|
14
|
public class AuthorizationService {
|
15
|
|
16
|
public final String PORTAL_ADMIN = "PORTAL_ADMINISTRATOR";
|
17
|
public final String ANONYMOUS_USER = "ROLE_ANONYMOUS";
|
18
|
public final String REGISTERED_USER = "REGISTERED_USER";
|
19
|
|
20
|
private String mapType(String type, boolean communityMap) {
|
21
|
if(type.equals("organization")) {
|
22
|
type = "institution";
|
23
|
} else if(type.equals("ri") && communityMap) {
|
24
|
type = "community";
|
25
|
}
|
26
|
return type;
|
27
|
}
|
28
|
|
29
|
/**
|
30
|
* Type = FUNDER | COMMUNITY | INSTITUTION | PROJECT
|
31
|
*
|
32
|
* */
|
33
|
public String curator(String type) {
|
34
|
return "CURATOR_" + mapType(type, true).toUpperCase();
|
35
|
}
|
36
|
|
37
|
/**
|
38
|
* Type = FUNDER | COMMUNITY | INSTITUTION | PROJECT
|
39
|
*
|
40
|
* Id = EE, EGI, etc
|
41
|
* */
|
42
|
public String manager(String type, String id) {
|
43
|
return mapType(type, true).toUpperCase() + "_" + id.toUpperCase() + "_MANAGER";
|
44
|
}
|
45
|
|
46
|
/**
|
47
|
* Type = FUNDER | COMMUNITY | RI | INSTITUTION | PROJECT
|
48
|
*
|
49
|
* Id = EE, EGI, etc
|
50
|
* */
|
51
|
public String member(String type, String id) {
|
52
|
return mapType(type, false).toUpperCase() + "_" + id.toUpperCase();
|
53
|
}
|
54
|
|
55
|
public boolean isCommunity(String type) {
|
56
|
return mapType(type, false).equals("community");
|
57
|
}
|
58
|
|
59
|
public boolean isPortalAdmin() {
|
60
|
return getRoles().stream().anyMatch(authority -> authority.equalsIgnoreCase(PORTAL_ADMIN));
|
61
|
}
|
62
|
|
63
|
public boolean isCurator(String type) {
|
64
|
return getRoles().stream().anyMatch(authority -> authority.equalsIgnoreCase(curator(type)));
|
65
|
}
|
66
|
|
67
|
public boolean isManager(String type, String id) {
|
68
|
return getRoles().stream().anyMatch(authority -> authority.equalsIgnoreCase(manager(type, id)));
|
69
|
}
|
70
|
|
71
|
public boolean isMember(String type, String id) {
|
72
|
return getRoles().stream().anyMatch(authority -> authority.equalsIgnoreCase(member(type, id)));
|
73
|
}
|
74
|
|
75
|
public List<String> getRoles() {
|
76
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
77
|
if (authentication instanceof OIDCAuthenticationToken) {
|
78
|
return authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());
|
79
|
}
|
80
|
return new ArrayList<>();
|
81
|
}
|
82
|
|
83
|
public String getAaiId() {
|
84
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
85
|
return authentication instanceof OIDCAuthenticationToken ? ((OIDCAuthenticationToken)authentication).getSub() : null;
|
86
|
}
|
87
|
|
88
|
public String getEmail() {
|
89
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
90
|
return authentication instanceof OIDCAuthenticationToken ? ((OIDCAuthenticationToken)authentication).getUserInfo().getEmail() : null;
|
91
|
}
|
92
|
}
|