Skip to content

Commit

Permalink
fix: preventing access to login page after Halo setup (#6865)
Browse files Browse the repository at this point in the history
#### What type of PR is this?
/kind bug
/area core
/milestone 2.20.x

#### What this PR does / why we need it:
修复初始化 Halo 之后无法进入登录页面的问题

此问题原因是更改了 AuthProvider 的逻辑,当系统启动之后缺少默认的登录方式导致登录页面无法正确渲染
此 PR 将确保默认的登录方式始终存在来解决此问题

how to test it?
重新初始化 Halo 之后能正确渲染登录页面并登录即为符合预期

#### Does this PR introduce a user-facing change?
```release-note
修复初始化 Halo 之后无法进入登录页面的问题
```
  • Loading branch information
guqing authored Oct 14, 2024
1 parent dd5f02e commit 45aae9c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import lombok.experimental.Accessors;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.lang.NonNull;
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
Expand All @@ -29,6 +30,7 @@
import run.halo.app.extension.MetadataUtil;
import run.halo.app.extension.ReactiveExtensionClient;
import run.halo.app.extension.index.query.QueryFactory;
import run.halo.app.infra.SystemConfigurableEnvironmentFetcher;
import run.halo.app.infra.SystemSetting;
import run.halo.app.infra.utils.JsonUtils;

Expand All @@ -42,6 +44,7 @@
@RequiredArgsConstructor
public class AuthProviderServiceImpl implements AuthProviderService {
private final ReactiveExtensionClient client;
private final ObjectProvider<SystemConfigurableEnvironmentFetcher> environmentFetcherProvider;

@Override
public Mono<AuthProvider> enable(String name) {
Expand Down Expand Up @@ -146,7 +149,7 @@ public int compareTo(@NonNull AuthProviderWithPriority o) {
}

private Mono<List<SystemSetting.AuthProviderState>> fetchProviderStates() {
return fetchSystemConfigMap()
return getSystemConfigMap()
.map(AuthProviderServiceImpl::getAuthProviderConfig)
.map(SystemSetting.AuthProvider::getStates)
.defaultIfEmpty(List.of())
Expand Down Expand Up @@ -218,7 +221,7 @@ private static SystemSetting.AuthProvider getAuthProviderConfig(ConfigMap config
}

private Mono<ConfigMap> updateAuthProviderEnabled(String name, boolean enabled) {
return Mono.defer(() -> fetchSystemConfigMap()
return Mono.defer(() -> getSystemConfigMap()
.flatMap(configMap -> {
var providerConfig = getAuthProviderConfig(configMap);
var stateToFoundOpt = providerConfig.getStates()
Expand All @@ -244,7 +247,12 @@ private Mono<ConfigMap> updateAuthProviderEnabled(String name, boolean enabled)
.filter(OptimisticLockingFailureException.class::isInstance));
}

Mono<ConfigMap> fetchSystemConfigMap() {
return client.fetch(ConfigMap.class, SystemSetting.SYSTEM_CONFIG);
private Mono<ConfigMap> getSystemConfigMap() {
var systemFetcher = environmentFetcherProvider.getIfUnique();
if (systemFetcher == null) {
return Mono.error(
new IllegalStateException("No SystemConfigurableEnvironmentFetcher found"));
}
return systemFetcher.getConfigMap();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,11 @@ data:
{
"search-engine": ["search-engine-lucene"]
}
authProvider: |
{
"states": [{
"name": "local",
"enabled": true,
"priority": 0
}]
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@

import java.util.HashMap;
import org.json.JSONException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.skyscreamer.jsonassert.JSONAssert;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.data.domain.Sort;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit.jupiter.SpringExtension;
Expand All @@ -28,6 +30,7 @@
import run.halo.app.extension.ListOptions;
import run.halo.app.extension.Metadata;
import run.halo.app.extension.ReactiveExtensionClient;
import run.halo.app.infra.SystemConfigurableEnvironmentFetcher;
import run.halo.app.infra.SystemSetting;
import run.halo.app.infra.utils.JsonUtils;

Expand All @@ -43,9 +46,20 @@ class AuthProviderServiceImplTest {
@Mock
ReactiveExtensionClient client;

@Mock
ObjectProvider<SystemConfigurableEnvironmentFetcher> systemFetchProvider;

@Mock
SystemConfigurableEnvironmentFetcher systemConfigFetcher;

@InjectMocks
AuthProviderServiceImpl authProviderService;

@BeforeEach
void setUp() {
when(systemFetchProvider.getIfUnique()).thenReturn(systemConfigFetcher);
}

@Test
void testEnable() throws JSONException {
// Create a test auth provider
Expand Down Expand Up @@ -199,7 +213,7 @@ AuthProvider createAuthProvider(String name) {
void pileSystemConfigMap() {
ConfigMap configMap = new ConfigMap();
configMap.setData(new HashMap<>());
when(client.fetch(eq(ConfigMap.class), eq(SystemSetting.SYSTEM_CONFIG)))
when(systemConfigFetcher.getConfigMap())
.thenReturn(Mono.just(configMap));
}
}

0 comments on commit 45aae9c

Please sign in to comment.