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
|
|
11
|
@Service("roleMappingService")
|
12
|
public class AaiRoleMappingService implements RoleMappingService {
|
13
|
|
14
|
private static final Logger logger = Logger.getLogger(AaiRoleMappingService.class);
|
15
|
|
16
|
@Value("${registry.production:true}")
|
17
|
private boolean production;
|
18
|
|
19
|
|
20
|
private String createRepoRoleName(String prefix, String repoId) {
|
21
|
return prefix + "." + repoId.replace(":", "$");
|
22
|
}
|
23
|
|
24
|
@Override
|
25
|
public String getRepoNameWithoutType(String fullName, String prefix) {
|
26
|
if (fullName != null && prefix != null && fullName.startsWith(prefix)) {
|
27
|
return fullName.substring(prefix.length());
|
28
|
}
|
29
|
return null;
|
30
|
}
|
31
|
|
32
|
@Override
|
33
|
public String getRepoIdByRoleId(String roleId) {
|
34
|
return roleId.replaceFirst(".*datasource\\.", "").replace("$", ":");
|
35
|
}
|
36
|
|
37
|
@Override
|
38
|
public String getRoleIdByRepoId(String repoId) {
|
39
|
String roleId = "";
|
40
|
String prefix = production ? null : "beta." + "datasource";
|
41
|
if (repoId != null) {
|
42
|
roleId = createRepoRoleName(prefix, repoId);
|
43
|
return roleId;
|
44
|
} else {
|
45
|
return null;
|
46
|
}
|
47
|
|
48
|
}
|
49
|
|
50
|
@Override
|
51
|
public String convertAuthorityIdToRepoId(String authorityId) {
|
52
|
String repo = "";
|
53
|
if (authorityId != null) {
|
54
|
repo = authorityId
|
55
|
.replaceFirst(".*datasource\\.", "")
|
56
|
.replace("$", ":")
|
57
|
.toLowerCase();
|
58
|
}
|
59
|
return repo;
|
60
|
}
|
61
|
|
62
|
@Override
|
63
|
public String convertAuthorityToRepoId(GrantedAuthority authority) {
|
64
|
return convertAuthorityIdToRepoId(authority.toString());
|
65
|
}
|
66
|
|
67
|
@Override
|
68
|
public String convertRepoIdToAuthorityId(String repoId) {
|
69
|
StringBuilder roleBuilder = new StringBuilder();
|
70
|
String role = "";
|
71
|
if (repoId != null) {
|
72
|
roleBuilder.append(production ? "" : "beta.");
|
73
|
roleBuilder.append("datasource.");
|
74
|
roleBuilder.append(repoId.replace(":", "$"));
|
75
|
role = roleBuilder.toString().replace(".", "_").toUpperCase();
|
76
|
}
|
77
|
return role;
|
78
|
}
|
79
|
|
80
|
@Override
|
81
|
public String convertRepoIdToEncodedAuthorityId(String repoId) {
|
82
|
return URLEncoder.encode(convertRepoIdToAuthorityId(repoId));
|
83
|
}
|
84
|
|
85
|
@Override
|
86
|
public SimpleGrantedAuthority convertRepoIdToAuthority(String repoId) {
|
87
|
String role = convertRepoIdToEncodedAuthorityId(repoId);
|
88
|
return new SimpleGrantedAuthority(role);
|
89
|
}
|
90
|
}
|