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.authority.SimpleGrantedAuthority;
|
6
|
import org.springframework.security.core.context.SecurityContextHolder;
|
7
|
import org.springframework.stereotype.Service;
|
8
|
|
9
|
import java.net.URLEncoder;
|
10
|
|
11
|
@Service("aaiUserRoleService")
|
12
|
public class AaiUserRoleServiceImpl implements AaiUserRoleService {
|
13
|
|
14
|
private static final Logger logger = Logger.getLogger(AaiUserRoleServiceImpl.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 getRoleIdByRepoId(String repoId, String prefix) {
|
34
|
String roleId = "";
|
35
|
if (repoId != null && prefix != null) {
|
36
|
roleId = createRepoRoleName(prefix, repoId);
|
37
|
return roleId;
|
38
|
} else {
|
39
|
return null;
|
40
|
}
|
41
|
|
42
|
}
|
43
|
|
44
|
@Override
|
45
|
public String convertRepoIdToAuthorityId(String repoId) {
|
46
|
StringBuilder roleBuilder = new StringBuilder();
|
47
|
String role = "";
|
48
|
if (repoId != null) {
|
49
|
roleBuilder.append(production ? "" : "beta.");
|
50
|
roleBuilder.append("datasource.");
|
51
|
roleBuilder.append(repoId.replace(":", "$"));
|
52
|
role = roleBuilder.toString().replace(".", "_").toUpperCase();
|
53
|
}
|
54
|
return role;
|
55
|
}
|
56
|
|
57
|
@Override
|
58
|
public String convertRepoIdToEncodedAuthorityId(String repoId) {
|
59
|
return URLEncoder.encode(convertRepoIdToAuthorityId(repoId));
|
60
|
}
|
61
|
|
62
|
@Override
|
63
|
public SimpleGrantedAuthority convertRepoIdToAuthority(String repoId) {
|
64
|
String role = convertRepoIdToEncodedAuthorityId(repoId);
|
65
|
if (role != null) {
|
66
|
role = URLEncoder.encode(role);
|
67
|
}
|
68
|
return new SimpleGrantedAuthority(role);
|
69
|
}
|
70
|
|
71
|
|
72
|
@Override
|
73
|
public boolean isMemberOf(String repoId) {
|
74
|
String repoRole = convertRepoIdToEncodedAuthorityId(repoId);
|
75
|
return SecurityContextHolder.getContext().getAuthentication().getAuthorities()
|
76
|
.parallelStream().anyMatch(authority -> authority.toString().equals(repoRole));
|
77
|
}
|
78
|
}
|