This is an automated email from the ASF dual-hosted git repository. hefengen pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push: new 4a75fec871 [refactor] refactor create user default ns rel (#5975) 4a75fec871 is described below commit 4a75fec871c57c6f4411af49afbb24db65888448 Author: aias00 <liuhon...@apache.org> AuthorDate: Thu Mar 27 08:43:15 2025 +0800 [refactor] refactor create user default ns rel (#5975) * [refactor] refactor create user default ns rel * [refactor] refactor create user default ns rel --- .../shenyu/admin/service/impl/DashboardUserServiceImpl.java | 12 ++++++++++-- .../shenyu/admin/service/impl/NamespaceUserServiceImpl.java | 12 ------------ .../shenyu/admin/service/DashboardUserServiceTest.java | 10 ++++++++-- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/DashboardUserServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/DashboardUserServiceImpl.java index 0c8dcd1e02..18b41cd7c0 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/DashboardUserServiceImpl.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/DashboardUserServiceImpl.java @@ -42,6 +42,7 @@ import org.apache.shenyu.admin.model.vo.DashboardUserVO; import org.apache.shenyu.admin.model.vo.LoginDashboardUserVO; import org.apache.shenyu.admin.model.vo.RoleVO; import org.apache.shenyu.admin.service.DashboardUserService; +import org.apache.shenyu.admin.service.NamespaceUserService; import org.apache.shenyu.admin.service.publish.UserEventPublisher; import org.apache.shenyu.admin.transfer.DashboardUserTransfer; import org.apache.shenyu.admin.utils.Assert; @@ -50,6 +51,7 @@ import org.apache.shenyu.admin.utils.JwtUtils; import org.apache.shenyu.admin.utils.SessionUtil; import org.apache.shenyu.admin.utils.WebI18nAssert; import org.apache.shenyu.common.constant.AdminConstants; +import org.apache.shenyu.common.constant.Constants; import org.apache.shenyu.common.utils.AesUtils; import org.apache.shenyu.common.utils.DigestUtils; import org.apache.shenyu.common.utils.ListUtil; @@ -95,6 +97,8 @@ public class DashboardUserServiceImpl implements DashboardUserService { private final SecretProperties secretProperties; + private final NamespaceUserService namespaceUserService; + public DashboardUserServiceImpl(final DashboardUserMapper dashboardUserMapper, final UserRoleMapper userRoleMapper, final RoleMapper roleMapper, @@ -103,7 +107,8 @@ public class DashboardUserServiceImpl implements DashboardUserService { final JwtProperties jwtProperties, final UserEventPublisher publisher, final DashboardProperties properties, - final SecretProperties secretProperties) { + final SecretProperties secretProperties, + final NamespaceUserService namespaceUserService) { this.dashboardUserMapper = dashboardUserMapper; this.userRoleMapper = userRoleMapper; this.roleMapper = roleMapper; @@ -113,6 +118,7 @@ public class DashboardUserServiceImpl implements DashboardUserService { this.publisher = publisher; this.properties = properties; this.secretProperties = secretProperties; + this.namespaceUserService = namespaceUserService; } /** @@ -135,9 +141,11 @@ public class DashboardUserServiceImpl implements DashboardUserService { DashboardUserDO dashboardUserDO = DashboardUserDO.buildDashboardUserDO(dashboardUserDTO); // create new user final int insertCount = dashboardUserMapper.insertSelective(dashboardUserDO); - bindUserRole(dashboardUserDO.getId(), dashboardUserDTO.getRoles()); + String userId = dashboardUserDO.getId(); + bindUserRole(userId, dashboardUserDTO.getRoles()); if (insertCount > 0) { publisher.onCreated(dashboardUserDO); + namespaceUserService.create(Constants.SYS_DEFAULT_NAMESPACE_ID, userId); } return insertCount; } diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/NamespaceUserServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/NamespaceUserServiceImpl.java index 82f359ffa3..17fd54b4b4 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/NamespaceUserServiceImpl.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/NamespaceUserServiceImpl.java @@ -20,15 +20,12 @@ package org.apache.shenyu.admin.service.impl; import com.google.common.collect.Lists; import org.apache.shenyu.admin.exception.ShenyuAdminException; import org.apache.shenyu.admin.mapper.NamespaceUserRelMapper; -import org.apache.shenyu.admin.model.entity.DashboardUserDO; import org.apache.shenyu.admin.model.entity.NamespaceDO; import org.apache.shenyu.admin.model.entity.NamespaceUserRelDO; import org.apache.shenyu.admin.model.event.namespace.NamespaceCreatedEvent; -import org.apache.shenyu.admin.model.event.user.UserCreatedEvent; import org.apache.shenyu.admin.model.vo.NamespaceUserRelVO; import org.apache.shenyu.admin.service.NamespaceUserService; import org.apache.shenyu.common.constant.AdminConstants; -import org.apache.shenyu.common.constant.Constants; import org.apache.shenyu.common.utils.UUIDUtils; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Service; @@ -76,15 +73,6 @@ public class NamespaceUserServiceImpl implements NamespaceUserService { return namespaceUserRelDOS.stream().map(NamespaceUserRelDO::getNamespaceId).collect(Collectors.toList()); } - @EventListener(value = UserCreatedEvent.class) - public void onUserCreated(final UserCreatedEvent event) { - DashboardUserDO dashboardUserDO = event.getChangedUser(); - if (Objects.isNull(dashboardUserDO)) { - return; - } - create(Constants.SYS_DEFAULT_NAMESPACE_ID, dashboardUserDO.getId()); - } - @EventListener(value = NamespaceCreatedEvent.class) public void onNamespaceCreated(final NamespaceCreatedEvent event) { NamespaceDO namespaceDO = (NamespaceDO) event.getSource(); diff --git a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/DashboardUserServiceTest.java b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/DashboardUserServiceTest.java index c26cd4c269..d1ca7ed01b 100644 --- a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/DashboardUserServiceTest.java +++ b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/DashboardUserServiceTest.java @@ -33,12 +33,13 @@ import org.apache.shenyu.admin.model.page.PageParameter; import org.apache.shenyu.admin.model.query.DashboardUserQuery; import org.apache.shenyu.admin.model.vo.DashboardUserVO; import org.apache.shenyu.admin.model.vo.LoginDashboardUserVO; +import org.apache.shenyu.admin.model.vo.NamespaceUserRelVO; import org.apache.shenyu.admin.service.impl.DashboardUserServiceImpl; import org.apache.shenyu.admin.service.publish.UserEventPublisher; -import org.apache.shenyu.common.utils.AesUtils; -import org.apache.shenyu.common.utils.ListUtil; import org.apache.shenyu.admin.utils.SessionUtil; +import org.apache.shenyu.common.utils.AesUtils; import org.apache.shenyu.common.utils.DigestUtils; +import org.apache.shenyu.common.utils.ListUtil; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -46,6 +47,7 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.ldap.core.LdapTemplate; import org.springframework.test.util.ReflectionTestUtils; + import java.sql.Timestamp; import java.util.Collections; import java.util.HashSet; @@ -99,6 +101,9 @@ public final class DashboardUserServiceTest { @Mock private SecretProperties secretProperties; + @Mock + private NamespaceUserService namespaceUserService; + @Test public void testCreateOrUpdate() { SessionUtil.setLocalVisitor(UserInfo.builder().userId("1").userName("admin").build()); @@ -106,6 +111,7 @@ public final class DashboardUserServiceTest { .userName(TEST_USER_NAME).password(TEST_PASSWORD).roles(Collections.singletonList("1")) .build(); given(dashboardUserMapper.insertSelective(any(DashboardUserDO.class))).willReturn(1); + given(namespaceUserService.create(any(), any())).willReturn(new NamespaceUserRelVO()); assertEquals(1, dashboardUserService.createOrUpdate(dashboardUserDTO)); verify(dashboardUserMapper).insertSelective(any(DashboardUserDO.class));