Project

General

Profile

1
package eu.dnetlib.repo.manager.service.security;
2

    
3
import org.apache.log4j.Logger;
4
import org.springframework.beans.factory.annotation.Value;
5
import org.springframework.security.core.GrantedAuthority;
6
import org.springframework.security.core.authority.SimpleGrantedAuthority;
7
import org.springframework.stereotype.Service;
8

    
9
import java.net.URLEncoder;
10
import java.util.Collection;
11
import java.util.stream.Collectors;
12

    
13
@Service("roleMappingService")
14
public class AaiRoleMappingService implements RoleMappingService {
15

    
16
    private static final Logger logger = Logger.getLogger(AaiRoleMappingService.class);
17

    
18
    @Value("${registry.production:true}")
19
    private boolean production;
20

    
21

    
22
    private String createRepoRoleName(String prefix, String repoId) {
23
        return prefix + "." + repoId.replace(":", "$");
24
    }
25

    
26
    @Override
27
    public String getRepoNameWithoutType(String fullName, String prefix) {
28
        if (fullName != null && prefix != null && fullName.startsWith(prefix)) {
29
            return fullName.substring(prefix.length());
30
        }
31
        return null;
32
    }
33

    
34
    @Override
35
    public String getRepoIdByRoleId(String roleId) {
36
        return roleId.replaceFirst(".*datasource\\.", "").replace("$", ":");
37
    }
38

    
39
    @Override
40
    public Collection<String> getRepoIdsByRoleIds(Collection<String> roleIds) {
41
        return roleIds
42
                .stream()
43
                .map(this::getRepoIdByRoleId)
44
                .collect(Collectors.toList());
45
    }
46

    
47
    @Override
48
    public String getRoleIdByRepoId(String repoId) {
49
        String roleId = "";
50
        String prefix = production ? null : "beta." + "datasource";
51
        if (repoId != null) {
52
            roleId = createRepoRoleName(prefix, repoId);
53
            return roleId;
54
        } else {
55
            return null;
56
        }
57

    
58
    }
59

    
60
    @Override
61
    public Collection<String> getRoleIdsByRepoIds(Collection<String> repoIds) {
62
        return repoIds
63
                .stream()
64
                .map(this::getRoleIdByRepoId)
65
                .collect(Collectors.toList());
66
    }
67

    
68
    @Override
69
    public String convertAuthorityIdToRepoId(String authorityId) {
70
        String repo = "";
71
        if (authorityId != null) {
72
            repo = authorityId
73
                    .replaceFirst(".*datasource\\.", "")
74
                    .replace("$", ":")
75
                    .toLowerCase();
76
        }
77
        return repo;
78
    }
79

    
80
    @Override
81
    public String convertAuthorityToRepoId(GrantedAuthority authority) {
82
        return convertAuthorityIdToRepoId(authority.toString());
83
    }
84

    
85
    @Override
86
    public String convertRepoIdToAuthorityId(String repoId) {
87
        StringBuilder roleBuilder = new StringBuilder();
88
        String role = "";
89
        if (repoId != null) {
90
            roleBuilder.append(production ? "" : "beta.");
91
            roleBuilder.append("datasource.");
92
            roleBuilder.append(repoId.replace(":", "$"));
93
            role = roleBuilder.toString().replace(".", "_").toUpperCase();
94
        }
95
        return role;
96
    }
97

    
98
    @Override
99
    public String convertRepoIdToEncodedAuthorityId(String repoId) {
100
        return URLEncoder.encode(convertRepoIdToAuthorityId(repoId));
101
    }
102

    
103
    @Override
104
    public SimpleGrantedAuthority convertRepoIdToAuthority(String repoId) {
105
        String role = convertRepoIdToEncodedAuthorityId(repoId);
106
        return new SimpleGrantedAuthority(role);
107
    }
108
}
(1-1/6)