Skip to content

Commit

Permalink
VIC-1918: Tenant data in authorised context
Browse files Browse the repository at this point in the history
  • Loading branch information
adnanalicic committed Nov 1, 2022
1 parent 59a75ac commit cc354d8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ public RestrictedTenantDTO toRestrictedTenantDTO(TenantEntity tenant) {
.settings(getSettings(tenant));
}

public RestrictedTenantDTO toRestrictedTenantDTOinAuthorisedContext(TenantEntity mainTenant, TenantEntity tenantFromAuthorisedContext) {
RestrictedTenantDTO restrictedTenantDTO = toRestrictedTenantDTO(mainTenant);
restrictedTenantDTO.getContent().setPrivacy(tenantFromAuthorisedContext.getContentPrivacy());
return restrictedTenantDTO;
}

public BasicTenantLicensingDTO toBasicLicensingTenantDTO(TenantEntity tenant) {
var basicTenantLicensingDTO = new BasicTenantLicensingDTO()
.id(tenant.getId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
import com.vi.tenantservice.api.model.TenantEntity;
import com.vi.tenantservice.api.service.TenantService;
import com.vi.tenantservice.api.validation.TenantInputSanitizer;
import com.vi.tenantservice.config.security.AuthorisationService;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.ws.rs.BadRequestException;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
Expand All @@ -29,6 +32,10 @@ public class TenantServiceFacade {
private final @NonNull TenantConverter tenantConverter;
private final @NonNull TenantInputSanitizer tenantInputSanitizer;
private final @NonNull TenantFacadeAuthorisationService tenantFacadeAuthorisationService;
private final @NonNull AuthorisationService authorisationService;

@Value("${feature.multitenancy.with.single.domain.enabled}")
private boolean multitenancyWithSingleDomain;

public TenantDTO createTenant(TenantDTO tenantDTO) {
log.info("Creating new tenant");
Expand Down Expand Up @@ -74,7 +81,7 @@ public Optional<RestrictedTenantDTO> findRestrictedTenantById(Long id) {
return tenantById.isEmpty() ? Optional.empty()
: Optional.of(tenantConverter.toRestrictedTenantDTO(tenantById.get()));
}

public List<BasicTenantLicensingDTO> getAllTenants() {
var tenantEntities = tenantService.getAllTenants();
return tenantEntities.stream().map(tenantConverter::toBasicLicensingTenantDTO).collect(
Expand All @@ -83,10 +90,32 @@ public List<BasicTenantLicensingDTO> getAllTenants() {

public Optional<RestrictedTenantDTO> findTenantBySubdomain(String subdomain) {
var tenantById = tenantService.findTenantBySubdomain(subdomain);

if (multitenancyWithSingleDomain) {
return getSingleDomainSpecificTenantData(tenantById);
}

return tenantById.isEmpty() ? Optional.empty()
: Optional.of(tenantConverter.toRestrictedTenantDTO(tenantById.get()));
}

public Optional<RestrictedTenantDTO> getSingleDomainSpecificTenantData(
Optional<TenantEntity> tenant) {
Optional<Long> accessTokenTenantId = authorisationService.findTenantIdInAccessToken();
if(accessTokenTenantId.isEmpty()){
if (accessTokenTenantId.isEmpty()) {
throw new BadRequestException("tenantId not found in access token");
}
}
Optional<TenantEntity> tenantFromAuthorisedContext = tenantService.findTenantById(accessTokenTenantId.get());
if (tenantFromAuthorisedContext.isEmpty()) {
throw new BadRequestException("Tenant not found for id " + accessTokenTenantId);
}
return Optional.of(tenantConverter
.toRestrictedTenantDTOinAuthorisedContext(tenant.get(),
tenantFromAuthorisedContext.get()));
}

public Optional<RestrictedTenantDTO> getSingleTenant() {
var tenantEntities = tenantService.getAllTenants();
if (tenantEntities != null && tenantEntities.size() == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.vi.tenantservice.api.model.TenantEntity;
import com.vi.tenantservice.api.service.TenantService;
import com.vi.tenantservice.api.validation.TenantInputSanitizer;
import com.vi.tenantservice.config.security.AuthorisationService;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.Test;
Expand All @@ -23,6 +24,7 @@
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.test.util.ReflectionTestUtils;

@ExtendWith(MockitoExtension.class)
class TenantServiceFacadeTest {
Expand All @@ -45,6 +47,9 @@ class TenantServiceFacadeTest {
@Mock
private TenantFacadeAuthorisationService tenantFacadeAuthorisationService;

@Mock
private AuthorisationService authorisationService;

@InjectMocks
private TenantServiceFacade tenantServiceFacade;

Expand Down Expand Up @@ -193,4 +198,24 @@ void getSingleTenant_Should_shouldThrowIllegalStateException_When_moreTenantsAre
verify(tenantService).getAllTenants();
verifyNoInteractions(converter);
}

@Test
void findTenantBySubdomain_Should_tenantDataBasedOnTenantFromAccessToken_When_tenantIdPresentInToken(){
String subdomain = "app";
ReflectionTestUtils.setField(tenantServiceFacade,"multitenancyWithSingleDomain",true);
ReflectionTestUtils.setField(tenantServiceFacade,"tenantConverter",new TenantConverter());
TenantEntity defaultTenantEntity = new TenantEntity();
defaultTenantEntity.setContentPrivacy("content1");
Optional<TenantEntity> defaultTenant = Optional.of(defaultTenantEntity);
when(tenantService.findTenantBySubdomain(subdomain)).thenReturn(defaultTenant);
when(authorisationService.findTenantIdInAccessToken()).thenReturn(Optional.of(2L));
TenantEntity accessTokenTenant = new TenantEntity();
accessTokenTenant.setContentPrivacy("content2");
Optional<TenantEntity> accessTokenTenantData = Optional.of(accessTokenTenant);
when(tenantService.findTenantById(2L)).thenReturn(accessTokenTenantData);

Optional<RestrictedTenantDTO> tenantDTO = tenantServiceFacade.findTenantBySubdomain(subdomain);
assertThat(tenantDTO.get().getContent().getPrivacy()).contains("content2");

}
}

0 comments on commit cc354d8

Please sign in to comment.