fix cache to use tenant

This commit is contained in:
Efstratios Giannopoulos 2024-04-05 11:34:30 +03:00
parent 139ee2d259
commit 0d3e72028f
3 changed files with 34 additions and 9 deletions

View File

@ -20,14 +20,16 @@ public class AffiliationCacheService extends CacheService<AffiliationCacheServic
public AffiliationCacheValue() {
}
public AffiliationCacheValue(UUID userId, UUID entityId, String entityType, AffiliatedResource affiliatedResource) {
public AffiliationCacheValue(UUID tenantId, UUID userId, UUID entityId, String entityType, AffiliatedResource affiliatedResource) {
this.userId = userId;
this.tenantId = tenantId;
this.entityId = entityId;
this.entityType = entityType;
this.affiliatedResource = affiliatedResource;
}
private UUID userId;
private UUID tenantId;
private UUID entityId;
private String entityType;
private AffiliatedResource affiliatedResource;
@ -40,6 +42,14 @@ public class AffiliationCacheService extends CacheService<AffiliationCacheServic
this.userId = userId;
}
public UUID getTenantId() {
return tenantId;
}
public void setTenantId(UUID tenantId) {
this.tenantId = tenantId;
}
public UUID getEntityId() {
return entityId;
}
@ -79,17 +89,18 @@ public class AffiliationCacheService extends CacheService<AffiliationCacheServic
@Override
public String keyOf(AffiliationCacheValue value) {
return this.buildKey(value.getUserId(), value.getEntityId(), value.getEntityType());
return this.buildKey(value.getTenantId(), value.getUserId(), value.getEntityId(), value.getEntityType());
}
public String buildKey(UUID userId, UUID entityId, String entityType) {
public String buildKey(UUID tenantId, UUID userId, UUID entityId, String entityType) {
if (userId == null) throw new IllegalArgumentException("userId id is required");
if (entityId == null) throw new IllegalArgumentException("entityId id is required");
if (this.conventionService.isNullOrEmpty(entityType)) throw new IllegalArgumentException("entityType id is required");
HashMap<String, String> keyParts = new HashMap<>();
keyParts.put("$user$", userId.toString().replace("-", "").toLowerCase(Locale.ROOT));
keyParts.put("$tenant$", tenantId == null ? "" : userId.toString().replace("-", "").toLowerCase(Locale.ROOT));
keyParts.put("$entity$", entityId.toString().replace("-", "").toLowerCase(Locale.ROOT));
keyParts.put("$type$", entityType);
return this.generateKey(keyParts);

View File

@ -3,6 +3,7 @@ package eu.eudat.authorization.authorizationcontentresolver;
import eu.eudat.authorization.AffiliatedResource;
import eu.eudat.authorization.PermissionNameProvider;
import eu.eudat.commons.enums.IsActive;
import eu.eudat.commons.scope.tenant.TenantScope;
import eu.eudat.commons.scope.user.UserScope;
import eu.eudat.data.DescriptionEntity;
import eu.eudat.data.DmpDescriptionTemplateEntity;
@ -19,6 +20,7 @@ import gr.cite.tools.fieldset.BaseFieldSet;
import org.springframework.stereotype.Service;
import org.springframework.web.context.annotation.RequestScope;
import javax.management.InvalidApplicationException;
import java.util.*;
import java.util.stream.Collectors;
@ -27,11 +29,13 @@ import java.util.stream.Collectors;
public class AuthorizationContentResolverImpl implements AuthorizationContentResolver {
private final QueryFactory queryFactory;
private final UserScope userScope;
private final TenantScope tenantScope;
private final AffiliationCacheService affiliationCacheService;
private final PermissionNameProvider permissionNameProvider;
public AuthorizationContentResolverImpl(QueryFactory queryFactory, UserScope userScope, AffiliationCacheService affiliationCacheService, PermissionNameProvider permissionNameProvider) {
public AuthorizationContentResolverImpl(QueryFactory queryFactory, UserScope userScope, TenantScope tenantScope, AffiliationCacheService affiliationCacheService, PermissionNameProvider permissionNameProvider) {
this.queryFactory = queryFactory;
this.userScope = userScope;
this.tenantScope = tenantScope;
this.affiliationCacheService = affiliationCacheService;
this.permissionNameProvider = permissionNameProvider;
}
@ -138,21 +142,31 @@ public class AuthorizationContentResolverImpl implements AuthorizationContentRes
return affiliatedResources;
}
private List<UUID> getAffiliatedFromCache(List<UUID> ids, UUID userId, Map<UUID, AffiliatedResource> affiliatedResources, String entityType){
private List<UUID> getAffiliatedFromCache(List<UUID> ids, UUID userId, Map<UUID, AffiliatedResource> affiliatedResources, String entityType) {
List<UUID> idsToResolve = new ArrayList<>();
for (UUID id : ids){
AffiliationCacheService.AffiliationCacheValue cacheValue = this.affiliationCacheService.lookup(this.affiliationCacheService.buildKey(userId, id, entityType));
AffiliationCacheService.AffiliationCacheValue cacheValue = null;
try {
cacheValue = this.affiliationCacheService.lookup(this.affiliationCacheService.buildKey(this.tenantScope.isSet() ? this.tenantScope.getTenant(): null, userId, id, entityType));
} catch (InvalidApplicationException e) {
throw new RuntimeException(e);
}
if (cacheValue != null) affiliatedResources.put(id, cacheValue.getAffiliatedResource());
else idsToResolve.add(id);
}
return idsToResolve;
}
private void ensureAffiliatedInCache(List<UUID> idsToResolve, UUID userId, Map<UUID, AffiliatedResource> affiliatedResources, String entityType){
private void ensureAffiliatedInCache(List<UUID> idsToResolve, UUID userId, Map<UUID, AffiliatedResource> affiliatedResources, String entityType) {
for (UUID id : idsToResolve){
AffiliatedResource affiliatedResource = affiliatedResources.getOrDefault(id, null);
if (affiliatedResource != null) {
AffiliationCacheService.AffiliationCacheValue cacheValue = new AffiliationCacheService.AffiliationCacheValue(userId, id, entityType, affiliatedResource);
AffiliationCacheService.AffiliationCacheValue cacheValue = null;
try {
cacheValue = new AffiliationCacheService.AffiliationCacheValue(this.tenantScope.isSet() ? this.tenantScope.getTenant(): null, userId, id, entityType, affiliatedResource);
} catch (InvalidApplicationException e) {
throw new RuntimeException(e);
}
this.affiliationCacheService.put(cacheValue);
}
}

View File

@ -110,4 +110,4 @@ cache:
keyPattern: resolve_$keyhash$:v0
affiliation:
name: affiliation
keyPattern: affiliation_$entity$_$user$_$type$:v0
keyPattern: affiliation_$entity$_$user$_$tenant$_$type$:v0