Project

General

Profile

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

    
12
@Component("AuthorizationService")
13
public class AuthorizationService {
14

    
15
    public final String PORTAL_ADMIN = "PORTAL_ADMINISTRATOR";
16
    public final String ANONYMOUS_USER = "ROLE_ANONYMOUS";
17
    public final String REGISTERED_USER = "REGISTERED_USER";
18

    
19
    private String mapType(String type, boolean communityMap) {
20
        if(type.equals("organization")) {
21
            type = "institution";
22
        } else if(type.equals("ri") && communityMap) {
23
            type = "community";
24
        }
25
        return type;
26
    }
27

    
28
    /**
29
     * Type = FUNDER | COMMUNITY | INSTITUTION | PROJECT
30
     *
31
     * */
32
    public String curator(String type) {
33
        return "CURATOR_" + mapType(type, true).toUpperCase();
34
    }
35

    
36
    /**
37
     * Type = FUNDER | COMMUNITY | INSTITUTION | PROJECT
38
     *
39
     * Id = EE, EGI, etc
40
     * */
41
    public String manager(String type, String id) {
42
        return mapType(type, true).toUpperCase() + "_" + id.toUpperCase() + "_MANAGER";
43
    }
44

    
45
    /**
46
     * Type = FUNDER | COMMUNITY | RI | INSTITUTION | PROJECT
47
     *
48
     * Id = EE, EGI, etc
49
     * */
50
    public String member(String type, String id) {
51
        return mapType(type, false).toUpperCase() + "_" + id.toUpperCase();
52
    }
53

    
54
    public boolean isCommunity(String type) {
55
        return mapType(type, false).equals("community");
56
    }
57

    
58

    
59
    public List<String> getRoles() {
60
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
61
        if (authentication != null) {
62
            List<GrantedAuthority> authorities = (List<GrantedAuthority>) authentication.getAuthorities();
63
            if (authorities != null) {
64
                List<String> roles = new ArrayList<>();
65
                authorities.forEach((authority) -> {
66
                    roles.add(authority.getAuthority());
67
                });
68
                return roles;
69
            }
70
        }
71
        return null;
72
    }
73

    
74
    public String getAaiId() {
75
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
76
        return authentication instanceof OIDCAuthenticationToken ? ((OIDCAuthenticationToken)authentication).getSub() : null;
77
    }
78

    
79
    public String getEmail() {
80
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
81
        return authentication instanceof OIDCAuthenticationToken ? ((OIDCAuthenticationToken)authentication).getUserInfo().getEmail() : null;
82
    }
83
}
(2-2/9)