Project

General

Profile

1 59336 k.triantaf
package eu.dnetlib.uoaauthorizationlibrary.security;
2
3 60035 konstantin
import org.apache.log4j.Logger;
4 59754 konstantin
import org.springframework.security.core.Authentication;
5
import org.springframework.security.core.GrantedAuthority;
6
import org.springframework.security.core.context.SecurityContextHolder;
7 60035 konstantin
import org.springframework.security.core.userdetails.User;
8 59336 k.triantaf
import org.springframework.stereotype.Component;
9
10 59754 konstantin
import java.util.ArrayList;
11
import java.util.List;
12
13 59336 k.triantaf
@Component(value = "AuthorizationService")
14
public class AuthorizationService {
15 60035 konstantin
    private final Logger log = Logger.getLogger(this.getClass());
16 59336 k.triantaf
17
    public final String PORTAL_ADMIN = "PORTAL_ADMINISTRATOR";
18 60035 konstantin
    public final String ANONYMOUS_USER = "ROLE_ANONYMOUS";
19 60198 k.triantaf
    public final String REGISTERED_USER = "REGISTERED_USER";
20 59336 k.triantaf
21 60198 k.triantaf
22 60300 k.triantaf
    private String mapType(String type, boolean communityMap) {
23
        if (type.equals("organization")) {
24 59754 konstantin
            type = "institution";
25 60300 k.triantaf
        } else if (type.equals("ri") && communityMap) {
26 59754 konstantin
            type = "community";
27
        }
28
        return type;
29
    }
30
31 59336 k.triantaf
    /**
32
     * Type = FUNDER | COMMUNITY | INSTITUTION | PROJECT
33 60300 k.triantaf
     */
34 59336 k.triantaf
    public String curator(String type) {
35 60300 k.triantaf
        return "CURATOR_" + mapType(type, true).toUpperCase();
36 59336 k.triantaf
    }
37
38
    /**
39
     * Type = FUNDER | COMMUNITY | INSTITUTION | PROJECT
40 60300 k.triantaf
     * <p>
41 59336 k.triantaf
     * Id = EE, EGI, etc
42 60300 k.triantaf
     */
43 59336 k.triantaf
    public String manager(String type, String id) {
44 60300 k.triantaf
        return mapType(type, true).toUpperCase() + "_" + id.toUpperCase() + "_MANAGER";
45 59336 k.triantaf
    }
46
47
    /**
48 60300 k.triantaf
     * Type = FUNDER | COMMUNITY | RI | INSTITUTION | PROJECT
49
     * <p>
50 59336 k.triantaf
     * Id = EE, EGI, etc
51 60300 k.triantaf
     */
52 59346 k.triantaf
    public String member(String type, String id) {
53 60300 k.triantaf
        return mapType(type, false).toUpperCase() + "_" + id.toUpperCase();
54 59336 k.triantaf
    }
55 59754 konstantin
56
    public List<String> getRoles() {
57
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
58 60300 k.triantaf
        if (authentication != null) {
59 59754 konstantin
            List<GrantedAuthority> authorities = (List<GrantedAuthority>) authentication.getAuthorities();
60 60300 k.triantaf
            if (authorities != null) {
61 60035 konstantin
                List<String> roles = new ArrayList<>();
62
                authorities.forEach((authority) -> {
63
                    roles.add(authority.getAuthority());
64
                });
65
                return roles;
66 59754 konstantin
            }
67
        }
68 60035 konstantin
        return null;
69 59754 konstantin
    }
70 60035 konstantin
71
    public String getAaiId() {
72
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
73 60419 konstantin
        if (authentication != null && (authentication.getPrincipal() instanceof User)) {
74 60035 konstantin
            User user = (User) authentication.getPrincipal();
75
            return user.getPassword();
76
        }
77
        return null;
78
    }
79 59336 k.triantaf
}