Project

General

Profile

1
package eu.dnetlib.uoaauthorizationlibrary.security;
2

    
3
import org.springframework.security.core.Authentication;
4
import org.springframework.security.core.GrantedAuthority;
5
import org.springframework.security.core.context.SecurityContextHolder;
6
import org.springframework.stereotype.Component;
7

    
8
import java.util.ArrayList;
9
import java.util.List;
10

    
11
@Component(value = "AuthorizationService")
12
public class AuthorizationService {
13

    
14
    public final String SUPER_ADMIN = "SUPER_ADMINISTRATOR";
15
    public final String PORTAL_ADMIN = "PORTAL_ADMINISTRATOR";
16
    public final String USER_ADMIN = "USER_MANAGER";
17

    
18
    private String mapType(String type) {
19
        if(type.equals("organization")) {
20
            type = "institution";
21
        }
22
        if(type.equals("ri")) {
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).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).toUpperCase() + "_" + id.toUpperCase() + "_MANAGER";
43
    }
44

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

    
54
    public List<String> getRoles() {
55
        List<String> roles = new ArrayList<>();
56
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
57
        if(authentication != null) {
58
            List<GrantedAuthority> authorities = (List<GrantedAuthority>) authentication.getAuthorities();
59
            if(authorities != null) {
60
                authorities.forEach((authority) -> roles.add(authority.getAuthority()));
61
            }
62
        }
63
        return roles;
64
    }
65
}
(4-4/7)