Kaynağa Gözat

feat(user): 添加租户ID支持并优化用户查询逻辑

- 在 AdminUserService 中新增带租户ID的查询方法
- 实现根据租户ID和账号精确查找用户的功能
- 移除 WebClientController 控制器及相关接口
- 清理 WebUserGateway 中客户端管理相关方法- 更新 WebUserGatewayImpl 实现类中的权限校验逻辑-为用户添加、修改操作增加租户ID自动填充功能
- 强化用户账号唯一性检查,防止重复创建
- 删除TblRoleService中冗余的角色查询方法
- 优化用户删除逻辑,同步清理用户角色关联数据
chejianzheng 3 hafta önce
ebeveyn
işleme
9841e7dd3f

+ 0 - 61
portal-service-application/src/main/java/com/hfln/portal/application/controller/web/WebClientController.java

@@ -1,61 +0,0 @@
-
-package com.hfln.portal.application.controller.web;
-
-
-import cn.hfln.framework.catchlog.CatchAndLog;
-import cn.hfln.framework.dto.ApiResult;
-import com.hfln.portal.common.dto.data.user.AdminUserDto;
-import com.hfln.portal.common.request.web.ClientAddParam;
-import com.hfln.portal.common.request.web.ClientModParam;
-import com.hfln.portal.common.request.web.ClientQueryParam;
-import com.hfln.portal.common.vo.PageRecord;
-import com.hfln.portal.domain.gateway.WebUserGateway;
-import io.swagger.v3.oas.annotations.Operation;
-import io.swagger.v3.oas.annotations.tags.Tag;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.*;
-
-import javax.validation.Valid;
-
-@RestController
-@CatchAndLog
-@Tag(name = "web端客户管理相关")
-@Slf4j
-@RequestMapping("/web/client")
-public class WebClientController {
-
-    @Autowired
-    private WebUserGateway webUserGateway;
-
-    @PostMapping("/query")
-    @Operation(summary = "查询用户")
-    public ApiResult<PageRecord<AdminUserDto>> queryClient(@Valid @RequestBody ClientQueryParam param) {
-
-        return ApiResult.success(webUserGateway.queryClient(param));
-    }
-
-    @PostMapping("/add")
-    @Operation(summary = "超管添加用户管理员")
-    public ApiResult<Void> addClient(@Valid @RequestBody ClientAddParam param) {
-
-        webUserGateway.addClient(param);
-        return ApiResult.success();
-    }
-
-    @PostMapping("/mod")
-    @Operation(summary = "超管修改用户管理员信息")
-    public ApiResult<Void> modClient(@Valid @RequestBody ClientModParam param) {
-
-        webUserGateway.modClient(param);
-        return ApiResult.success();
-    }
-
-    @GetMapping("/del")
-    @Operation(summary = "超管删除用户管理员")
-    public ApiResult<Void> delClient(@RequestParam Long userId) {
-
-        webUserGateway.delClient(userId);
-        return ApiResult.success();
-    }
-}

+ 0 - 9
portal-service-domain/src/main/java/com/hfln/portal/domain/gateway/WebUserGateway.java

@@ -16,13 +16,4 @@ public interface WebUserGateway {
 
     void delMananger(Long userId);
 
-
-    // 用户超管 管理用户
-    PageRecord<AdminUserDto> queryClient(ClientQueryParam param);
-
-    void addClient(ClientAddParam param);
-
-    void modClient(ClientModParam param);
-
-    void delClient(Long userId);
 }

+ 27 - 114
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/gateway/impl/WebUserGatewayImpl.java

@@ -6,7 +6,9 @@ import cn.hfln.framework.extension.BizException;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.hfln.portal.common.constant.UserConstants;
 import com.hfln.portal.common.dto.data.user.AdminUserDto;
-import com.hfln.portal.common.request.web.*;
+import com.hfln.portal.common.request.web.AddAccountParam;
+import com.hfln.portal.common.request.web.AdminModParam;
+import com.hfln.portal.common.request.web.QueryManagerParam;
 import com.hfln.portal.common.vo.PageRecord;
 import com.hfln.portal.domain.customer.AdminUserType;
 import com.hfln.portal.domain.customer.OprLogType;
@@ -25,7 +27,6 @@ import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
-import org.springframework.util.StringUtils;
 
 import java.util.List;
 import java.util.Objects;
@@ -49,9 +50,10 @@ public class WebUserGatewayImpl implements WebUserGateway {
 
         // 对 后台超级管理员 和用户超级管理员 进行权限控制
         String userType = (String) StpUtil.getSession().get(UserConstants.SA_USER_TYPE);
-        log.info("当前登录用户类型:userType:{}", userType);
-        if (!AdminUserType.getBgManagerTypes().contains(userType)) {
-            throw new BizException(ErrorEnum.USER_INFO_ERROR.getErrorCode(), ErrorEnum.USER_INFO_ERROR.getErrorMessage());
+        Long tenantId = (Long) StpUtil.getSession().get(UserConstants.SA_USER_TENANT_ID);
+        log.info("当前登录用户类型:userType:{}, tenantId:{}", userType, tenantId);
+        if (tenantId != null) {
+            param.setTenantId(tenantId);
         }
 
         // 执行分页查询
@@ -64,8 +66,13 @@ public class WebUserGatewayImpl implements WebUserGateway {
     @Override
     @Transactional
     public void addManager(AddAccountParam param) {
+
+        Long tenantId = (Long) StpUtil.getSession().get(UserConstants.SA_USER_TENANT_ID);
+        if (tenantId != null) {
+            param.setTenantId(tenantId);
+        }
         // 1. 检查账号是否已存在
-        AdminUserInfo exist = adminUserService.queryByAccount(param.getAccount());
+        AdminUserInfo exist = adminUserService.queryByAccount(param.getTenantId(), param.getAccount());
         if (Objects.nonNull(exist)) {
             throw new BizException(ErrorEnum.USER_ALREADY_EXISTS.getErrorCode(), ErrorEnum.USER_ALREADY_EXISTS.getErrorMessage());
         }
@@ -73,9 +80,7 @@ public class WebUserGatewayImpl implements WebUserGateway {
         // 对 后台超级管理员 和用户超级管理员 进行权限控制
         String userType = (String) StpUtil.getSession().get(UserConstants.SA_USER_TYPE);
         log.info("当前登录用户类型:userType:{}", userType);
-        if (!AdminUserType.getBgManagerTypes().contains(userType)) {
-            throw new BizException(ErrorEnum.USER_INFO_ERROR.getErrorCode(), ErrorEnum.USER_INFO_ERROR.getErrorMessage());
-        }
+
         // 如果创建的用户类型为普通用户,则需要指定租户id
         if (AdminUserType.getUserTypes().contains(param.getUserType()) && param.getTenantId() == null) {
             throw new BizException(ErrorEnum.USER_INFO_ERROR.getErrorCode(), ErrorEnum.USER_INFO_ERROR.getErrorMessage());
@@ -101,6 +106,17 @@ public class WebUserGatewayImpl implements WebUserGateway {
     @Transactional
     public void modManager(AdminModParam param) {
 
+        Long tenantId = (Long) StpUtil.getSession().get(UserConstants.SA_USER_TENANT_ID);
+        if (tenantId != null) {
+            param.setTenantId(tenantId);
+        }
+
+        // 1. 检查账号是否已存在
+        AdminUserInfo alreadyExst = adminUserService.queryByAccount(param.getTenantId(), param.getAccount());
+        if (Objects.nonNull(alreadyExst) && !alreadyExst.getUserId().equals(param.getUserId())) {
+            throw new BizException(ErrorEnum.USER_ALREADY_EXISTS.getErrorCode(), ErrorEnum.USER_ALREADY_EXISTS.getErrorMessage());
+        }
+
         // 1. 根据用户ID查询现有用户信息
         AdminUserInfo exist = adminUserService.getById(param.getUserId());
         if (Objects.isNull(exist)) {
@@ -110,9 +126,6 @@ public class WebUserGatewayImpl implements WebUserGateway {
         // 对 后台超级管理员 和用户超级管理员 进行权限控制
         String userType = (String) StpUtil.getSession().get(UserConstants.SA_USER_TYPE);
         log.info("当前登录用户类型:userType:{}", userType);
-        if (!AdminUserType.getBgManagerTypes().contains(userType)) {
-            throw new BizException(ErrorEnum.USER_INFO_ERROR.getErrorCode(), ErrorEnum.USER_INFO_ERROR.getErrorMessage());
-        }
 
         // 如果修改的用户类型为普通用户,则需要指定租户id
         if (AdminUserType.getUserTypes().contains(param.getUserType()) && param.getTenantId() == null) {
@@ -148,113 +161,13 @@ public class WebUserGatewayImpl implements WebUserGateway {
         // 对 后台超级管理员 和用户超级管理员 进行权限控制
         String userType = (String) StpUtil.getSession().get(UserConstants.SA_USER_TYPE);
         log.info("当前登录用户类型:userType:{}", userType);
-        if (!AdminUserType.getBgManagerTypes().contains(userType)) {
-            throw new BizException(ErrorEnum.USER_INFO_ERROR.getErrorCode(), ErrorEnum.USER_INFO_ERROR.getErrorMessage());
-        }
-        check(userType, exist.getUserType());
-
-        logService.saveLog(userId,OprLogType.DEL_MANAGER.getCode(), exist);
-        adminUserService.deleteById(exist.getUserId());
-    }
 
-    @Override
-    public PageRecord<AdminUserDto> queryClient(ClientQueryParam param) {
-        // 对 后台超级管理员 和用户超级管理员 进行权限控制
-        String userType = (String) StpUtil.getSession().get(UserConstants.SA_USER_TYPE);
-        Long tenantId = (Long) StpUtil.getSession().get(UserConstants.SA_USER_TENANT_ID);
-        log.info("当前登录用户类型:userType:{}, tenantId:{}", userType, tenantId);
-        if (!StringUtils.hasText(userType) || !AdminUserType.USER_ADMIN.getCode().equals(userType) || tenantId == null) {
-            throw new BizException(ErrorEnum.USER_INFO_ERROR.getErrorCode(), ErrorEnum.USER_INFO_ERROR.getErrorMessage());
-        }
-
-        // 执行分页查询
-        Page<AdminUserInfo> userInfoPage = adminUserService.queryClientList(param, tenantId);
-        // 换为目标VO
-        List<AdminUserDto> targets = CopyUtils.copyList(userInfoPage.getRecords(), AdminUserDto.class);
-        return CopyUtils.copyPage(userInfoPage, targets);
-    }
-
-    @Override
-    public void addClient(ClientAddParam param) {
-        // 1. 检查账号是否已存在
-        AdminUserInfo exist = adminUserService.queryByAccount(param.getAccount());
-        if (Objects.nonNull(exist)) {
-            throw new BizException(ErrorEnum.USER_ALREADY_EXISTS.getErrorCode(), ErrorEnum.USER_ALREADY_EXISTS.getErrorMessage());
-        }
-
-        // 对 后台超级管理员 和用户超级管理员 进行权限控制
-        String userType = (String) StpUtil.getSession().get(UserConstants.SA_USER_TYPE);
-        Long tenantId = (Long) StpUtil.getSession().get(UserConstants.SA_USER_TENANT_ID);
-        log.info("当前登录用户类型:userType:{}, tenantId:{}", userType, tenantId);
-        if (!AdminUserType.USER_ADMIN.getCode().equals(userType) || tenantId == null) {
-            throw new BizException(ErrorEnum.USER_ALREADY_EXISTS.getErrorCode(), ErrorEnum.USER_ALREADY_EXISTS.getErrorMessage());
-        }
-
-        //2.保存新用户到 admin_user_info 表
-        AdminUserInfo adminUserInfo = CopyUtils.copy(param, AdminUserInfo.class);
-        adminUserInfo.setPassword(PasswordUtil.encrypt(param.getPassword()));
-        adminUserInfo.setUserType(AdminUserType.USER_MANAGER.getCode());
-        adminUserService.save(adminUserInfo);
-
-        //3.保存用户角色到 tbl_user_role 表
-//        TblRole tblRole = tblRoleService.queryByRoleCode(UserConstants.ROLE_USER_MANAGER_DEFAULT);
-        TblUserRole userRole = new TblUserRole();
-        userRole.setUserId(adminUserInfo.getUserId());
-        userRole.setRoleId(param.getRoleId());
-        tblUserRoleService.save(userRole);
-
-        logService.saveLog(adminUserInfo.getUserId(),OprLogType.ADD_CLIENT.getCode(), adminUserInfo);
-    }
-
-    @Override
-    public void modClient(ClientModParam param) {
-        // 1. 检查账号是否已存在
-        AdminUserInfo exist = adminUserService.queryByAccount(param.getAccount());
-        if (Objects.nonNull(exist)) {
-            throw new BizException(ErrorEnum.USER_ALREADY_EXISTS.getErrorCode(), ErrorEnum.USER_ALREADY_EXISTS.getErrorMessage());
-        }
-
-        // 对 后台超级管理员 和用户超级管理员 进行权限控制
-        String userType = (String) StpUtil.getSession().get(UserConstants.SA_USER_TYPE);
-        Long tenantId = (Long) StpUtil.getSession().get(UserConstants.SA_USER_TENANT_ID);
-        log.info("当前登录用户类型:userType:{}, tenantId:{}", userType, tenantId);
-        if (!AdminUserType.USER_ADMIN.getCode().equals(userType) || tenantId == null) {
-            throw new BizException(ErrorEnum.USER_ALREADY_EXISTS.getErrorCode(), ErrorEnum.USER_ALREADY_EXISTS.getErrorMessage());
-        }
-
-        logService.saveLog(param.getUserId(),OprLogType.MOD_CLIENT.getCode(), exist);
-        //2.保存新用户到 admin_user_info 表
-        BeanUtils.copyProperties(param, exist, "userId");
-        exist.setPassword(PasswordUtil.encrypt(param.getPassword()));
-        exist.setUserType(AdminUserType.USER_MANAGER.getCode());
-        adminUserService.save(exist);
-
-        //3.保存用户角色到 tbl_user_role 表
-//        TblRole tblRole = tblRoleService.queryByRoleCode(UserConstants.ROLE_USER_MANAGER_DEFAULT);
-        TblUserRole userRole = new TblUserRole();
-        userRole.setUserId(exist.getUserId());
-        userRole.setRoleId(param.getRoleId());
-        tblUserRoleService.save(userRole);
-    }
-
-    @Override
-    public void delClient(Long userId) {
-
-        // 1. 根据用户ID查询现有用户信息
-        AdminUserInfo exist = adminUserService.getById(userId);
-        if (Objects.isNull(exist)) {
-            throw new BizException(ErrorEnum.USER_IS_NOT_EXIST.getErrorCode(), ErrorEnum.USER_IS_NOT_EXIST.getErrorMessage());
-        }
-        // 对 后台超级管理员 和用户超级管理员 进行权限控制
-        String userType = (String) StpUtil.getSession().get(UserConstants.SA_USER_TYPE);
-        log.info("当前登录用户类型:userType:{}", userType);
-        if (AdminUserType.USER_ADMIN.getCode().equals(userType)) {
-            throw new BizException(ErrorEnum.OPR_NOT_ALLOWED.getErrorCode(), ErrorEnum.OPR_NOT_ALLOWED.getErrorMessage());
-        }
         check(userType, exist.getUserType());
 
         logService.saveLog(userId,OprLogType.DEL_MANAGER.getCode(), exist);
         adminUserService.deleteById(exist.getUserId());
+
+        tblUserRoleService.delByUserId(exist.getUserId());
     }
 
     private void check(String userType, String createdUserType) {

+ 2 - 0
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/service/AdminUserService.java

@@ -16,6 +16,8 @@ public interface AdminUserService extends IService<AdminUserInfo> {
 
     AdminUserInfo queryByAccount(String account);
 
+    AdminUserInfo queryByAccount(Long tenantId, String account);
+
     Page<AdminUserInfo> queryList(QueryManagerParam param);
 
     Page<AdminUserInfo> queryClientList(ClientQueryParam param, Long tenantId);

+ 0 - 2
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/service/TblRoleService.java

@@ -11,8 +11,6 @@ public interface TblRoleService extends IService<TblRole> {
 
     TblRole findAvailableByCode(String roleCode);
 
-    TblRole queryByRoleCode(String roleCode);
-
     TblRole queryRole(Long tenantId, String roleCode);
 
     TblRole queryRole(Long tenantId, String roleCode, String roleName, Long roleId);

+ 11 - 0
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/service/impl/AdminUserServiceImpl.java

@@ -52,6 +52,17 @@ public class AdminUserServiceImpl extends ServiceImpl<AdminUserInfoMapper, Admin
     }
 
     @Override
+    public AdminUserInfo queryByAccount(Long tenantId, String account) {
+        LambdaQueryWrapper<AdminUserInfo> queryWrapper = new LambdaQueryWrapper<>();
+        if (tenantId != null) {
+            queryWrapper.eq(AdminUserInfo::getTenantId, tenantId);
+        }
+        queryWrapper.eq(AdminUserInfo::getAccount, account);
+        queryWrapper.eq(AdminUserInfo::getIsDeleted, BasePO.DeleteFlag.NOT_DELETED);
+        return this.baseMapper.selectOne(queryWrapper);
+    }
+
+    @Override
     public Page<AdminUserInfo> queryList(QueryManagerParam queryReq) {
         Page<AdminUserInfo> page = new Page<>(queryReq.getPageNo(), queryReq.getPageSize());
 

+ 0 - 8
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/service/impl/TblRoleServiceImpl.java

@@ -34,14 +34,6 @@ public class TblRoleServiceImpl extends ServiceImpl<TblRoleMapper, TblRole> impl
     }
 
     @Override
-    public TblRole queryByRoleCode(String roleCode) {
-        LambdaQueryWrapper<TblRole> queryWrapper = new LambdaQueryWrapper<>();
-        queryWrapper.eq(TblRole::getRoleCode, roleCode);
-        queryWrapper.eq(TblRole::getIsDeleted, BasePO.DeleteFlag.NOT_DELETED);
-        return this.baseMapper.selectOne(queryWrapper);
-    }
-
-    @Override
     public TblRole queryRole(Long tenantId, String roleCode) {
         LambdaQueryWrapper<TblRole> queryWrapper = new LambdaQueryWrapper<>();
         if (tenantId != null) {