From ea378559918f830f59b0e06ad9408c83de2e86c8 Mon Sep 17 00:00:00 2001 From: cuijiawang Date: Wed, 24 Sep 2025 11:51:27 +0800 Subject: [PATCH] fix user --- .../common/satoken/pojo/LoginUser.java | 3 +- .../client/service/ISysClientService.java | 8 ++- .../service/impl/SysClientServiceImpl.java | 11 +++- .../user/controller/SysUserController.java | 50 ++++++++++++++----- .../system/user/service/ISysUserService.java | 2 + .../user/service/impl/SysUserServiceImpl.java | 37 ++++++++++++-- .../auth/controller/AuthController.java | 2 +- .../agileboot/auth/pojo/dto/LoginBody.java | 5 +- .../com/agileboot/auth/pojo/vo/LoginVO.java | 2 +- .../auth/service/SysLoginService.java | 1 + .../strategy/PasswordAuthStrategy.java | 7 +-- .../system/client/entity/SysClient.java | 4 +- .../system/client/vo/SysClientVO.java | 13 ++++- .../system/user/dto/AddUserCommand.java | 2 + .../system/user/dto/ChangeStatusCommand.java | 2 +- .../agileboot/system/user/entity/SysUser.java | 6 +-- 16 files changed, 119 insertions(+), 36 deletions(-) diff --git a/agileboot-common/wol-common-satoken/src/main/java/com/agileboot/common/satoken/pojo/LoginUser.java b/agileboot-common/wol-common-satoken/src/main/java/com/agileboot/common/satoken/pojo/LoginUser.java index f8a2fbb..cd9af6e 100644 --- a/agileboot-common/wol-common-satoken/src/main/java/com/agileboot/common/satoken/pojo/LoginUser.java +++ b/agileboot-common/wol-common-satoken/src/main/java/com/agileboot/common/satoken/pojo/LoginUser.java @@ -139,7 +139,8 @@ public class LoginUser implements Serializable { * 是否是超级管理员 */ private Integer isAdmin; - private String clientId; + private Long clientId; + private Integer status; /** * 获取登录id diff --git a/agileboot-system/agileboot-system-base/src/main/java/com/agileboot/system/client/service/ISysClientService.java b/agileboot-system/agileboot-system-base/src/main/java/com/agileboot/system/client/service/ISysClientService.java index 61ee862..0b1d0eb 100644 --- a/agileboot-system/agileboot-system-base/src/main/java/com/agileboot/system/client/service/ISysClientService.java +++ b/agileboot-system/agileboot-system-base/src/main/java/com/agileboot/system/client/service/ISysClientService.java @@ -1,7 +1,13 @@ package com.agileboot.system.client.service; +import com.agileboot.system.client.entity.SysClient; import com.agileboot.system.client.vo.SysClientVO; +import java.util.List; + public interface ISysClientService { - SysClientVO queryByClientId(String clientId); + + SysClientVO queryByClientId(Long clientId); + + List list(); } diff --git a/agileboot-system/agileboot-system-base/src/main/java/com/agileboot/system/client/service/impl/SysClientServiceImpl.java b/agileboot-system/agileboot-system-base/src/main/java/com/agileboot/system/client/service/impl/SysClientServiceImpl.java index 8af20b7..6094a4c 100644 --- a/agileboot-system/agileboot-system-base/src/main/java/com/agileboot/system/client/service/impl/SysClientServiceImpl.java +++ b/agileboot-system/agileboot-system-base/src/main/java/com/agileboot/system/client/service/impl/SysClientServiceImpl.java @@ -9,11 +9,18 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.stereotype.Service; +import java.util.List; + @Service public class SysClientServiceImpl extends ServiceImpl implements ISysClientService { @Override - public SysClientVO queryByClientId(String clientId) { - SysClient client = super.baseMapper.selectOne(new LambdaQueryWrapper().eq(SysClient::getClientId, clientId)); + public SysClientVO queryByClientId(Long clientId) { + SysClient client = super.baseMapper.selectOne(new LambdaQueryWrapper().eq(SysClient::getId, clientId)); return BeanUtil.copyProperties(client, SysClientVO.class); } + + @Override + public List list() { + return super.list(); + } } diff --git a/agileboot-system/agileboot-system-base/src/main/java/com/agileboot/system/user/controller/SysUserController.java b/agileboot-system/agileboot-system-base/src/main/java/com/agileboot/system/user/controller/SysUserController.java index e90f218..9d201d4 100644 --- a/agileboot-system/agileboot-system-base/src/main/java/com/agileboot/system/user/controller/SysUserController.java +++ b/agileboot-system/agileboot-system-base/src/main/java/com/agileboot/system/user/controller/SysUserController.java @@ -4,6 +4,9 @@ import cn.hutool.core.collection.ListUtil; import com.agileboot.common.core.core.R; import com.agileboot.common.core.utils.poi.CustomExcelUtil; import com.agileboot.common.mybatis.core.page.PageR; +import com.agileboot.system.client.entity.SysClient; +import com.agileboot.system.client.service.ISysClientService; +import com.agileboot.system.client.vo.SysClientVO; import com.agileboot.system.user.dto.*; import com.agileboot.system.user.service.ISysUserService; import com.baomidou.mybatisplus.core.metadata.IPage; @@ -13,8 +16,8 @@ import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; -import java.util.HashSet; import java.util.List; +import java.util.Set; /** * 用户信息 @@ -25,11 +28,12 @@ import java.util.List; public class SysUserController { private final ISysUserService sysUserService; + private final ISysClientService sysClientService; /** * 获取用户列表 */ - @GetMapping + @GetMapping("list") public PageR userList(SearchUserQuery dto) { IPage page = sysUserService.getUserList(dto); return new PageR<>(page); @@ -74,10 +78,20 @@ public class SysUserController { return R.ok(userDetailInfo); } + /** + * 获取所有客户端 + */ + @GetMapping("/client") + public R> getClient() { + List list = sysClientService.list(); + List clientVOS = list.stream().map(SysClientVO::new).toList(); + return R.ok(clientVOS); + } + /** * 新增用户 */ - @PostMapping + @PostMapping("/create") public R add(@Validated @RequestBody AddUserCommand command) { sysUserService.addUser(command); return R.ok(); @@ -86,8 +100,9 @@ public class SysUserController { /** * 修改用户 */ - @PostMapping("/{userId}") - public R edit(@Validated @RequestBody UpdateUserCommand command) { + @PostMapping("/update/{userId}") + public R edit(@PathVariable(value = "userId") Long userId, @Validated @RequestBody UpdateUserCommand command) { + command.setUserId(userId); sysUserService.updateUser(command); return R.ok(); } @@ -95,27 +110,36 @@ public class SysUserController { /** * 删除用户 */ - @PostMapping("/{userIds}") - public R remove(@PathVariable List userIds) { - sysUserService.deleteUsers(new HashSet<>(userIds)); + @PostMapping("/del/{userId}") + public R remove(@PathVariable(value = "userId") Long userId) { + sysUserService.deleteUsers(Set.of(userId)); return R.ok(); } /** - * 重置密码 + * 管理员重置用户密码 */ - @PostMapping("/{userId}/password") - public R resetPassword(@PathVariable Long userId, @RequestBody ResetPasswordCommand command) { + @PostMapping("/re_pwd_ad/{userId}") + public R resetPasswordByAdmin(@PathVariable(value = "userId") Long userId, @RequestBody ResetPasswordCommand command) { command.setUserId(userId); sysUserService.resetUserPassword(command); return R.ok(); } + /** + * 重置密码 + */ + @PostMapping("/re_pwd") + public R resetPassword(@RequestBody ResetPasswordCommand command) { + sysUserService.resetUserPasswordByAdmin(command); + return R.ok(); + } + /** * 修改用户状态 */ - @PostMapping("/{userId}/status") - public R changeStatus(@PathVariable Long userId, @RequestBody ChangeStatusCommand command) { + @PostMapping("/status/{userId}") + public R changeStatus(@PathVariable(value = "userId") Long userId, @RequestBody ChangeStatusCommand command) { command.setUserId(userId); sysUserService.changeUserStatus(command); return R.ok(); diff --git a/agileboot-system/agileboot-system-base/src/main/java/com/agileboot/system/user/service/ISysUserService.java b/agileboot-system/agileboot-system-base/src/main/java/com/agileboot/system/user/service/ISysUserService.java index 04288db..30fa8a4 100644 --- a/agileboot-system/agileboot-system-base/src/main/java/com/agileboot/system/user/service/ISysUserService.java +++ b/agileboot-system/agileboot-system-base/src/main/java/com/agileboot/system/user/service/ISysUserService.java @@ -45,4 +45,6 @@ public interface ISysUserService { void checkAnyPostIsAssignedToUser(List ids); Map geIdNameByIds(Set userIds); + + void resetUserPasswordByAdmin(ResetPasswordCommand command); } diff --git a/agileboot-system/agileboot-system-base/src/main/java/com/agileboot/system/user/service/impl/SysUserServiceImpl.java b/agileboot-system/agileboot-system-base/src/main/java/com/agileboot/system/user/service/impl/SysUserServiceImpl.java index e33faae..f8a8581 100644 --- a/agileboot-system/agileboot-system-base/src/main/java/com/agileboot/system/user/service/impl/SysUserServiceImpl.java +++ b/agileboot-system/agileboot-system-base/src/main/java/com/agileboot/system/user/service/impl/SysUserServiceImpl.java @@ -2,7 +2,8 @@ package com.agileboot.system.user.service.impl; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; -import com.agileboot.common.core.constant.Constants; +import cn.hutool.crypto.digest.BCrypt; +import com.agileboot.common.core.enums.common.UserStatusEnum; import com.agileboot.common.core.exception.BizException; import com.agileboot.common.core.exception.error.ErrorCode; import com.agileboot.common.mybatis.core.page.PageQuery; @@ -37,8 +38,12 @@ public class SysUserServiceImpl extends ServiceImpl impl if (ObjectUtil.isNull(sysUser)) { throw new BizException("user.not.exists", username); } - if (Constants.DISABLE.equals(sysUser.getStatus())) { + if (UserStatusEnum.DISABLED.getValue().equals(sysUser.getStatus())) { throw new BizException("user.blocked", username); + } else if (UserStatusEnum.FROZEN.getValue().equals(sysUser.getStatus())) { + throw new BizException("user.freeze", username); + } else if (!UserStatusEnum.NORMAL.getValue().equals(sysUser.getStatus())) { + throw new BizException("user.status.error", username); } LoginUser loginUser = new LoginUser(); loginUser.setUserId(sysUser.getUserId()); @@ -51,6 +56,7 @@ public class SysUserServiceImpl extends ServiceImpl impl loginUser.setIsAdmin(sysUser.getIsAdmin()); loginUser.setRoleId(sysUser.getRoleId()); loginUser.setClientId(sysUser.getClientId()); + loginUser.setStatus(sysUser.getStatus()); return loginUser; } @@ -125,6 +131,7 @@ public class SysUserServiceImpl extends ServiceImpl impl } // password encrypt entity.setPassword(command.getPassword()); + entity.setClientId(command.getClientId()); super.baseMapper.insert(entity); } @@ -140,17 +147,39 @@ public class SysUserServiceImpl extends ServiceImpl impl @Override public void resetUserPassword(ResetPasswordCommand command) { +// if (!LoginHelper.isSuperAdmin() || !LoginHelper.isTenantAdmin()) { +// throw new BizException("permission.denied"); +// } + SysUser sysUser = new SysUser(); + String hashpw = BCrypt.hashpw(command.getPassword()); + sysUser.setPassword(command.getPassword()); + sysUser.setUserId(command.getUserId()); + super.updateById(sysUser); + } + @Override + public void resetUserPasswordByAdmin(ResetPasswordCommand command) { + SysUser sysUser = new SysUser(); + String hashpw = BCrypt.hashpw(command.getPassword()); + sysUser.setPassword(command.getPassword()); + sysUser.setUserId(LoginHelper.getUserId()); + super.updateById(sysUser); } @Override public void changeUserStatus(ChangeStatusCommand command) { - + SysUser sysUser = new SysUser(); + sysUser.setStatus(command.getStatus()); + sysUser.setUserId(command.getUserId()); + super.updateById(sysUser); } @Override public void deleteUsers(Set longs) { - +// if (!LoginHelper.isSuperAdmin() || !LoginHelper.isTenantAdmin()) { +// throw new BizException("permission.denied"); +// } + super.removeByIds(longs); } @Override diff --git a/agileboot-system/wol-auth/src/main/java/com/agileboot/auth/controller/AuthController.java b/agileboot-system/wol-auth/src/main/java/com/agileboot/auth/controller/AuthController.java index ed1a220..18f83b7 100644 --- a/agileboot-system/wol-auth/src/main/java/com/agileboot/auth/controller/AuthController.java +++ b/agileboot-system/wol-auth/src/main/java/com/agileboot/auth/controller/AuthController.java @@ -54,7 +54,7 @@ public class AuthController { public R login(@RequestBody String body) { LoginBody loginBody = JSONObject.parseObject(body, LoginBody.class); ValidatorUtils.validate(loginBody); - String clientId = loginBody.getClientId(); + Long clientId = loginBody.getClientId(); String grantType = loginBody.getGrantType(); SysClientVO clientVo = sysClientService.queryByClientId(clientId); diff --git a/agileboot-system/wol-auth/src/main/java/com/agileboot/auth/pojo/dto/LoginBody.java b/agileboot-system/wol-auth/src/main/java/com/agileboot/auth/pojo/dto/LoginBody.java index 00e7083..417212c 100644 --- a/agileboot-system/wol-auth/src/main/java/com/agileboot/auth/pojo/dto/LoginBody.java +++ b/agileboot-system/wol-auth/src/main/java/com/agileboot/auth/pojo/dto/LoginBody.java @@ -1,6 +1,7 @@ package com.agileboot.auth.pojo.dto; import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; import lombok.Data; import java.io.Serial; @@ -21,8 +22,8 @@ public class LoginBody implements Serializable { /** * 客户端id */ - @NotBlank(message = "Auth clientid cannot be blank") - private String clientId; + @NotNull(message = "Auth clientid cannot be blank") + private Long clientId; /** * 授权类型 diff --git a/agileboot-system/wol-auth/src/main/java/com/agileboot/auth/pojo/vo/LoginVO.java b/agileboot-system/wol-auth/src/main/java/com/agileboot/auth/pojo/vo/LoginVO.java index 18599c4..0322a16 100644 --- a/agileboot-system/wol-auth/src/main/java/com/agileboot/auth/pojo/vo/LoginVO.java +++ b/agileboot-system/wol-auth/src/main/java/com/agileboot/auth/pojo/vo/LoginVO.java @@ -39,7 +39,7 @@ public class LoginVO { * 应用id */ // @JsonProperty("client_id") - private String clientId; + private Long clientId; /** * 令牌权限 diff --git a/agileboot-system/wol-auth/src/main/java/com/agileboot/auth/service/SysLoginService.java b/agileboot-system/wol-auth/src/main/java/com/agileboot/auth/service/SysLoginService.java index 0728c26..4cc1412 100644 --- a/agileboot-system/wol-auth/src/main/java/com/agileboot/auth/service/SysLoginService.java +++ b/agileboot-system/wol-auth/src/main/java/com/agileboot/auth/service/SysLoginService.java @@ -116,6 +116,7 @@ public class SysLoginService { sysUser.setNickname(username); sysUser.setPassword(BCrypt.hashpw(password)); sysUser.setUserType(userType); + sysUser.setClientId(registerBody.getClientId()); boolean regFlag = sysUserService.registerUserInfo(sysUser); if (!regFlag) { diff --git a/agileboot-system/wol-auth/src/main/java/com/agileboot/auth/service/strategy/PasswordAuthStrategy.java b/agileboot-system/wol-auth/src/main/java/com/agileboot/auth/service/strategy/PasswordAuthStrategy.java index f879b27..317fb03 100644 --- a/agileboot-system/wol-auth/src/main/java/com/agileboot/auth/service/strategy/PasswordAuthStrategy.java +++ b/agileboot-system/wol-auth/src/main/java/com/agileboot/auth/service/strategy/PasswordAuthStrategy.java @@ -56,8 +56,9 @@ public class PasswordAuthStrategy implements IAuthStrategy { } + Long clientId = client.getId(); LoginUser loginUser = userService.getUserInfo(username); - if (!Objects.equals(loginUser.getClientId(), client.getClientId())) { + if (!Objects.equals(loginUser.getClientId(), clientId)) { throw new BizException("client.not.match"); } loginService.checkLogin(LoginType.PASSWORD, null, loginUser.getUsername(), () -> false); @@ -70,14 +71,14 @@ public class PasswordAuthStrategy implements IAuthStrategy { // 例如: 后台用户30分钟过期 app用户1天过期 model.setTimeout(client.getTimeout()); model.setActiveTimeout(client.getActiveTimeout()); - model.setExtra(LoginHelper.CLIENT_KEY, client.getClientId()); + model.setExtra(LoginHelper.CLIENT_KEY, clientId); // 生成token LoginHelper.login(loginUser, model); LoginVO loginVo = new LoginVO(); loginVo.setAccessToken(StpUtil.getTokenValue()); loginVo.setExpireIn(StpUtil.getTokenTimeout()); - loginVo.setClientId(client.getClientId()); + loginVo.setClientId(clientId); RoleVO roleInfo = roleService.getRoleInfo(loginUser.getRoleId()); CurrentLoginUserVO vo = new CurrentLoginUserVO(); diff --git a/wol-domain/src/main/java/com/agileboot/system/client/entity/SysClient.java b/wol-domain/src/main/java/com/agileboot/system/client/entity/SysClient.java index 12a1e48..209daab 100644 --- a/wol-domain/src/main/java/com/agileboot/system/client/entity/SysClient.java +++ b/wol-domain/src/main/java/com/agileboot/system/client/entity/SysClient.java @@ -27,9 +27,9 @@ public class SysClient extends BaseEntity { private Long id; /** - * 客户端id + * 客户端name */ - private String clientId; + private String clientName; /** * 客户端key diff --git a/wol-domain/src/main/java/com/agileboot/system/client/vo/SysClientVO.java b/wol-domain/src/main/java/com/agileboot/system/client/vo/SysClientVO.java index 9f86deb..8536ed3 100644 --- a/wol-domain/src/main/java/com/agileboot/system/client/vo/SysClientVO.java +++ b/wol-domain/src/main/java/com/agileboot/system/client/vo/SysClientVO.java @@ -1,5 +1,6 @@ package com.agileboot.system.client.vo; +import com.agileboot.system.client.entity.SysClient; import lombok.Data; import java.io.Serial; @@ -12,15 +13,23 @@ public class SysClientVO implements Serializable { @Serial private static final long serialVersionUID = 1L; + public SysClientVO() { + } + + public SysClientVO(SysClient entity) { + this.setId(entity.getId()); + this.setClientName(entity.getClientName()); + } + /** * id */ private Long id; /** - * 客户端id + * clientName */ - private String clientId; + private String clientName; /** * 客户端key diff --git a/wol-domain/src/main/java/com/agileboot/system/user/dto/AddUserCommand.java b/wol-domain/src/main/java/com/agileboot/system/user/dto/AddUserCommand.java index 9795218..950bd4b 100644 --- a/wol-domain/src/main/java/com/agileboot/system/user/dto/AddUserCommand.java +++ b/wol-domain/src/main/java/com/agileboot/system/user/dto/AddUserCommand.java @@ -45,5 +45,7 @@ public class AddUserCommand { @ExcelColumn(name = "备注") private String remark; + @ExcelColumn(name = "clientId") + private Long clientId; } diff --git a/wol-domain/src/main/java/com/agileboot/system/user/dto/ChangeStatusCommand.java b/wol-domain/src/main/java/com/agileboot/system/user/dto/ChangeStatusCommand.java index 688de86..6304c2f 100644 --- a/wol-domain/src/main/java/com/agileboot/system/user/dto/ChangeStatusCommand.java +++ b/wol-domain/src/main/java/com/agileboot/system/user/dto/ChangeStatusCommand.java @@ -9,6 +9,6 @@ import lombok.Data; public class ChangeStatusCommand { private Long userId; - private String status; + private Integer status; } diff --git a/wol-domain/src/main/java/com/agileboot/system/user/entity/SysUser.java b/wol-domain/src/main/java/com/agileboot/system/user/entity/SysUser.java index f4fa585..1e005f0 100644 --- a/wol-domain/src/main/java/com/agileboot/system/user/entity/SysUser.java +++ b/wol-domain/src/main/java/com/agileboot/system/user/entity/SysUser.java @@ -79,9 +79,9 @@ public class SysUser extends BaseEntity { private String password; /** - * 帐号状态(0正常 1停用) + * 帐号状态(1正常 2停用 3冻结) */ - private String status; + private Integer status; /** * 最后登录IP @@ -112,5 +112,5 @@ public class SysUser extends BaseEntity { * 超级管理员标志(1是,0否) */ private Integer isAdmin; - private String clientId; + private Long clientId; }