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) {
23
        if(type.equals("organization")) {
24
            type = "institution";
25
        }
26
        if(type.equals("ri")) {
27
            type = "community";
28
        }
29
        return type;
30
    }
31

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

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

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

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

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