fix: fix get simple info can get guest

This commit is contained in:
wzp 2024-12-23 18:35:36 +08:00
parent 34930fc6ae
commit bfd8e72e31
2 changed files with 28 additions and 16 deletions

View File

@ -93,7 +93,7 @@ public class UserController {
@GetMapping("/info/{id}")
@Operation(description = "获取简略用户信息")
public BaseResponse<User> getSimpleInfo(@PathVariable("id") int userId){
return service.getUserInfo(userId);
return service.getSimpleInfo(userId);
}
}

View File

@ -48,7 +48,7 @@ public class UserService {
private final ClubService clubService;
@Autowired
public UserService(UserDao userDao, AuthService authService, JwtUtils jwtUtils, VerifyCodeService verifyCodeService, File tempDir, AvatarDao avatarDao, @Qualifier("avatarDir") File avatarDir, ClubService clubService){
public UserService(UserDao userDao, AuthService authService, JwtUtils jwtUtils, VerifyCodeService verifyCodeService, File tempDir, AvatarDao avatarDao, @Qualifier("avatarDir") File avatarDir, ClubService clubService) {
this.userDao = userDao;
this.authService = authService;
this.jwtUtils = jwtUtils;
@ -65,7 +65,7 @@ public class UserService {
}*/
UserVo userVo = userDao.getUser(user.username(), DigestUtils.sha1Hex(user.password()));
User u = packageUser(userVo);
if (response != null && u != null){
if (response != null && u != null) {
response.addHeader("Set-Authorization", jwtUtils.createToken(u));
}
return u == null ? BaseResponse.failed(404, "无效用户") : BaseResponse.success(true);
@ -88,18 +88,21 @@ public class UserService {
}
return BaseResponse.success(true);
}
public User getUserById(int id){
public User getUserById(int id) {
return packageUser(userDao.getUserById(id));
}
public User getGuestUser(){
public User getGuestUser() {
User user = new User();
user.setId(-1);
user.setName("游客");
user.setAuth(authService.getAuthById(1));
return user;
}
private User packageUser(UserVo vo) {
if (vo == null){
if (vo == null) {
return null;
}
User result = new User();
@ -112,12 +115,13 @@ public class UserService {
}
public BaseResponse<User> getUserInfo(int userId) {
if (userId == -1){
if (userId == -1) {
return BaseResponse.success(getGuestUser());
}
return BaseResponse.success(getUserById(userId));
}
public boolean userHasPermission(int userId, int permissionId){
public boolean userHasPermission(int userId, int permissionId) {
return authService.authHasPermission(userDao.getUserAuthId(userId), permissionId);
}
@ -137,17 +141,18 @@ public class UserService {
}
return BaseResponse.failed(401, "权限不足,修改失败!", false);
}
public BaseResponse<Boolean> changePassword(UserChangePasswordVo changePasswordVo, int userId){
public BaseResponse<Boolean> changePassword(UserChangePasswordVo changePasswordVo, int userId) {
int changeUserId = changePasswordVo.getId();
String oldPassword = changePasswordVo.getOldPassword();
String newPassword = changePasswordVo.getNewPassword();
if (changeUserId == userId) {
if (userDao.changePassword(changeUserId, oldPassword, newPassword) > 0){
if (userDao.changePassword(changeUserId, oldPassword, newPassword) > 0) {
return BaseResponse.success(true);
}
return BaseResponse.failed(404,"旧密码错误!",false);
return BaseResponse.failed(404, "旧密码错误!", false);
}
if (userHasPermission(userId, 4)){
if (userHasPermission(userId, 4)) {
userDao.changePasswordAdmin(changeUserId, newPassword);
return BaseResponse.success(true);
}
@ -173,14 +178,14 @@ public class UserService {
userDao.changeAvatar(operationUserId, sha);
File file = new File(avatarDir, sha);
File tmpFile = new File(tempDir, avatarOperationCode);
if (!tmpFile.exists()){
if (!tmpFile.exists()) {
return BaseResponse.failed(500, "无法找到已上传的头像信息");
}
if (!file.exists()){
if (!file.exists()) {
if (!tmpFile.renameTo(file)) {
log.error("移动头像文件失败!");
}
}else {
} else {
if (!tmpFile.delete()) {
log.error("无法删除临时头像文件");
}
@ -221,7 +226,7 @@ public class UserService {
@SneakyThrows
public void getAvatar(HttpServletResponse response, String sha1) {
File avatar = new File(avatarDir, sha1);
if (avatar.exists()){
if (avatar.exists()) {
FileInputStream fileInputStream = new FileInputStream(avatar);
response.setContentType("image/png");
ServletOutputStream outputStream = response.getOutputStream();
@ -234,4 +239,11 @@ public class UserService {
public UserSimpleInformation getUserSimpleInformation(int userId) {
return userDao.getUserSimpleInformation(userId);
}
public BaseResponse<User> getSimpleInfo(int userId) {
if (userId == -1) {
return BaseResponse.failed(404, "用户不存在!");
}
return this.getUserInfo(userId);
}
}