Skip to content

Commit

Permalink
Revert "Set tenantId to TenantContextHolder via CurrentTenantIntercep…
Browse files Browse the repository at this point in the history
…tor"

This reverts commit 4e826b1.
  • Loading branch information
derTobsch committed Nov 19, 2023
1 parent d09e6da commit ae7bb8c
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 258 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,26 @@

import de.focusshift.zeiterfassung.tenancy.tenant.TenantContextHolder;
import de.focusshift.zeiterfassung.tenancy.tenant.TenantId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.stereotype.Component;

import java.util.Optional;

import static de.focusshift.zeiterfassung.tenancy.TenantConfigurationProperties.MULTI;
import static java.lang.invoke.MethodHandles.lookup;

@Component
@ConditionalOnProperty(value = "zeiterfassung.tenant.mode", havingValue = MULTI)
class TenantContextHolderMultiTenant implements TenantContextHolder {

private static final Logger LOG = LoggerFactory.getLogger(lookup().lookupClass());

private static final InheritableThreadLocal<TenantId> currentTenantId = new InheritableThreadLocal<>();

@Override
public Optional<TenantId> getCurrentTenantId() {
return Optional.ofNullable(currentTenantId.get());
}

@Override
public void setTenantId(TenantId tenantId) {
LOG.debug("Setting tenantId to {}", tenantId);
currentTenantId.set(tenantId);
}

@Override
public void clear() {
LOG.debug("Clearing tenantId");
currentTenantId.remove();
if (SecurityContextHolder.getContext().getAuthentication() instanceof final OAuth2AuthenticationToken oauthToken) {
final TenantId tenantId = new TenantId(oauthToken.getAuthorizedClientRegistrationId());
if (tenantId.valid()) {
return Optional.of(tenantId);
}
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,7 @@ class TenantContextHolderSingleTenant implements TenantContextHolder {
this.defaultTenantId = singleTenantConfigurationProperties.getDefaultTenantId();
}

@Override
public Optional<TenantId> getCurrentTenantId() {
return Optional.of(new TenantId(defaultTenantId));
}

@Override
public void setTenantId(TenantId tenantId) {
// do nothing
}

@Override
public void clear() {
// do nothing
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,4 @@
public interface TenantContextHolder {

Optional<TenantId> getCurrentTenantId();

void setTenantId(TenantId tenantId);

void clear();
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@ class WebConfiguration implements WebMvcConfigurer {

private final AuthoritiesModelProvider authoritiesModelProvider;
private final DoubleFormatter doubleFormatter;
private final CurrentTenantInterceptor currentTenantInterceptor;

WebConfiguration(AuthoritiesModelProvider authoritiesModelProvider, CurrentTenantInterceptor currentTenantInterceptor, DoubleFormatter doubleFormatter) {
WebConfiguration(AuthoritiesModelProvider authoritiesModelProvider, DoubleFormatter doubleFormatter) {
this.authoritiesModelProvider = authoritiesModelProvider;
this.currentTenantInterceptor = currentTenantInterceptor;
this.doubleFormatter = doubleFormatter;
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(authoritiesModelProvider);
registry.addInterceptor(currentTenantInterceptor);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,56 @@


import de.focusshift.zeiterfassung.tenancy.tenant.TenantId;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.security.oauth2.core.user.OAuth2User;

import java.util.List;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

class TenantContextHolderMultiTenantTest {

private final TenantContextHolderMultiTenant sut = new TenantContextHolderMultiTenant();

@BeforeEach
void setup() {
sut.clear();
}
@Test
void ensureTenantForOAuth2AuthenticationToken() {

@AfterEach
void tearDown() {
sut.clear();
}
final OAuth2User oAuth2User = mock(OAuth2User.class);
final Authentication authentication = new OAuth2AuthenticationToken(oAuth2User, List.of(), "a154bc4e");

@Test
void hasTenantId() {
sut.setTenantId(new TenantId("a154bc4e"));
assertThat(sut.getCurrentTenantId()).hasValue(new TenantId("a154bc4e"));
}
final SecurityContext securityContext = new SecurityContextImpl();
securityContext.setAuthentication(authentication);
SecurityContextHolder.setContext(securityContext);

@Test
void clearsTenantId() {
sut.setTenantId(new TenantId("a154bc4e"));
assertThat(sut.getCurrentTenantId()).hasValue(new TenantId("a154bc4e"));

sut.clear();
assertThat(sut.getCurrentTenantId()).isEmpty();
}

private static Stream<Authentication> authenticationSource() {
return Stream.of(
null,
UsernamePasswordAuthenticationToken.authenticated(null, null, List.of())
);
}

@ParameterizedTest
@MethodSource("authenticationSource")
void ensureExceptionWithoutOAuth2AuthenticationToken(final Authentication authentication) {

final SecurityContext securityContext = new SecurityContextImpl();
securityContext.setAuthentication(authentication);
SecurityContextHolder.setContext(securityContext);

assertThat(sut.getCurrentTenantId()).isNotPresent();
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package de.focusshift.zeiterfassung.timeclock;

import de.focusshift.zeiterfassung.tenancy.tenant.TenantContextHolder;
import de.focusshift.zeiterfassung.tenancy.tenant.TenantId;
import de.focusshift.zeiterfassung.user.CurrentUserProvider;
import de.focusshift.zeiterfassung.user.UserId;
import de.focusshift.zeiterfassung.web.DoubleFormatter;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
Expand Down Expand Up @@ -55,17 +52,9 @@ class TimeClockControllerTest {
@MockBean
private CurrentUserProvider currentUserProvider;

@MockBean
private TenantContextHolder tenantContextHolder;

@MockBean
private DoubleFormatter doubleFormatter;

@BeforeEach
void setUp() {
when(tenantContextHolder.getCurrentTenantId()).thenReturn(Optional.of(new TenantId("default")));
}

@Test
void ensureStartTimeClock() throws Exception {

Expand Down

This file was deleted.

This file was deleted.

0 comments on commit ae7bb8c

Please sign in to comment.