Project

General

Profile

1
package eu.dnetlib.uoaauthorizationlibrary.security;
2

    
3
import org.apache.log4j.Logger;
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.security.core.userdetails.User;
8
import org.springframework.stereotype.Component;
9

    
10
import java.util.ArrayList;
11
import java.util.List;
12

    
13
@Component(value = "AuthorizationService")
14
public class AuthorizationService {
15
    private final Logger log = Logger.getLogger(this.getClass());
16

    
17
    public final String PORTAL_ADMIN = "PORTAL_ADMINISTRATOR";
18
    public final String ANONYMOUS_USER = "ROLE_ANONYMOUS";
19
    public final String REGISTERED_USER = "REGISTERED_USER";
20

    
21

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

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

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

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

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

    
71
    public String getAaiId() {
72
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
73
        if (authentication != null) {
74
            User user = (User) authentication.getPrincipal();
75
            return user.getPassword();
76
        }
77
        return null;
78
    }
79
}
(4-4/8)