77 lines
2.5 KiB
Java
77 lines
2.5 KiB
Java
package eu.dnetlib.repo.manager.service.security;
|
|
|
|
import org.apache.log4j.Logger;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
@Service("aaiUserRoleService")
|
|
public class AaiUserRoleServiceImpl implements AaiUserRoleService {
|
|
|
|
private static final Logger logger = Logger.getLogger(AaiUserRoleServiceImpl.class);
|
|
|
|
@Value("${registry.production:true}")
|
|
private boolean production;
|
|
|
|
|
|
private String createRepoRoleName(String prefix, String repoId) {
|
|
return prefix + "." + repoId.replace(":", "$");
|
|
}
|
|
|
|
@Override
|
|
public String getRepoNameWithoutType(String fullName, String prefix) {
|
|
if (fullName != null && prefix != null && fullName.startsWith(prefix)) {
|
|
return fullName.substring(prefix.length());
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public String getRoleIdByRepoId(String repoId) {
|
|
String roleId = "";
|
|
String prefix = production ? null : "beta." + "datasource";
|
|
if (repoId != null) {
|
|
roleId = createRepoRoleName(prefix, repoId);
|
|
return roleId;
|
|
} else {
|
|
return null;
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
public String convertRepoIdToAuthorityId(String repoId) {
|
|
StringBuilder roleBuilder = new StringBuilder();
|
|
String role = "";
|
|
if (repoId != null) {
|
|
roleBuilder.append(production ? "" : "beta.");
|
|
roleBuilder.append("datasource.");
|
|
roleBuilder.append(repoId.replace(":", "$"));
|
|
role = roleBuilder.toString().replace(".", "_").toUpperCase();
|
|
}
|
|
return role;
|
|
}
|
|
|
|
@Override
|
|
public String convertRepoIdToEncodedAuthorityId(String repoId) {
|
|
return URLEncoder.encode(convertRepoIdToAuthorityId(repoId));
|
|
}
|
|
|
|
@Override
|
|
public SimpleGrantedAuthority convertRepoIdToAuthority(String repoId) {
|
|
String role = convertRepoIdToEncodedAuthorityId(repoId);
|
|
return new SimpleGrantedAuthority(role);
|
|
}
|
|
|
|
|
|
@Override
|
|
public boolean isMemberOf(String repoId) {
|
|
String repoRole = convertRepoIdToEncodedAuthorityId(repoId);
|
|
return SecurityContextHolder.getContext().getAuthentication().getAuthorities()
|
|
.parallelStream().anyMatch(authority -> authority.toString().equals(repoRole));
|
|
}
|
|
}
|