feat(user): implement user profile management functionality
- Add user nickname update endpoint with validation - Introduce user profile update endpoint supporting nickname and password changes - Create UserProfileResponse DTO with nickname field - Add SetUserNicknameRequest DTO for nickname updates - Implement UserProfileUpdateRequest DTO for profile modifications - Refactor UserService to UserProfileService with enhanced capabilities - Update UserMapper to include nickname in result mapping - Add new error codes for nickname and profile update failures - Modify CORS configuration to allow requests from localhost:5173 - Remove obsolete AppConfig RestTemplate bean definition - Rename UserinfoResponse to UserProfileResponse for consistency - Adjust controller endpoints to use updated service and DTOs - Add transactional support for user profile updates - Improve error handling for user-related operations
This commit is contained in:
@@ -75,8 +75,7 @@ public class SecurityConfig {
|
||||
|
||||
CorsConfiguration configuration = new CorsConfiguration();
|
||||
configuration.setAllowedOrigins(Arrays.asList(
|
||||
"http://localhost:63342", // 你的前端运行的地址
|
||||
"http://localhost:8080", // 其他可能的前端地址
|
||||
"http://localhost:5173", // 其他可能的前端地址
|
||||
"http://127.0.0.1:5500", // 另一个常见的本地开发地址
|
||||
"null" // 如果是从文件系统直接打开HTML文件,Origin 会是 "null"。仅用于开发!
|
||||
));
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package cn.nopj.chaos_api.controller;
|
||||
|
||||
|
||||
import cn.nopj.chaos_api.dto.request.SetUserNicknameRequest;
|
||||
import cn.nopj.chaos_api.dto.request.SetUserPasswordRequest;
|
||||
import cn.nopj.chaos_api.dto.request.UpdateUsernameRequest;
|
||||
import cn.nopj.chaos_api.dto.response.UserinfoResponse;
|
||||
import cn.nopj.chaos_api.dto.request.UserProfileUpdateRequest;
|
||||
import cn.nopj.chaos_api.dto.response.UserProfileResponse;
|
||||
import cn.nopj.chaos_api.model.ApiResult;
|
||||
import cn.nopj.chaos_api.service.UserInfoService;
|
||||
import cn.nopj.chaos_api.service.UserProfileServcie;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
@@ -23,7 +25,7 @@ import java.util.List;
|
||||
@RequestMapping("/api/user")
|
||||
public class UserController {
|
||||
@Autowired
|
||||
private UserInfoService userInfoService;
|
||||
private UserProfileServcie userProfileServcie;
|
||||
|
||||
/**
|
||||
* 获取所有用户信息
|
||||
@@ -31,8 +33,8 @@ public class UserController {
|
||||
*/
|
||||
@PreAuthorize("hasAuthority('admin')")
|
||||
@GetMapping("/all")
|
||||
ApiResult<List<UserinfoResponse>> getAllUsers(){
|
||||
return ApiResult.success(userInfoService.getAllUsers());
|
||||
ApiResult<List<UserProfileResponse>> getAllUsers(){
|
||||
return ApiResult.success(userProfileServcie.getAllUsers());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -43,7 +45,7 @@ public class UserController {
|
||||
@PreAuthorize("hasAuthority('admin')")
|
||||
@PutMapping("/setUserPassword")
|
||||
ApiResult<String> setUserPassword(@RequestBody SetUserPasswordRequest request){
|
||||
userInfoService.setUserPassword(request.getUserId(), request.getPassword());
|
||||
userProfileServcie.setUserPassword(request.getUserId(), request.getPassword());
|
||||
return ApiResult.success("用户密码修改成功");
|
||||
}
|
||||
|
||||
@@ -54,7 +56,7 @@ public class UserController {
|
||||
*/
|
||||
@PutMapping("/updateUsername")
|
||||
ApiResult<String> updateUsername(@RequestAttribute("currentUsername") String username,@RequestBody UpdateUsernameRequest request){
|
||||
userInfoService.updateUsername(username, request.getUsername());
|
||||
userProfileServcie.updateUsername(username, request.getUsername());
|
||||
return ApiResult.success("用户名更新成功");
|
||||
}
|
||||
|
||||
@@ -63,8 +65,29 @@ public class UserController {
|
||||
* 获取当前登录用户信息
|
||||
* @return 用户信息
|
||||
*/
|
||||
@GetMapping("/info")
|
||||
ApiResult<UserinfoResponse> getCurrentUserInfo(@RequestAttribute("currentUsername") String username){
|
||||
return ApiResult.success(userInfoService.findUserWithRoles(username));
|
||||
@GetMapping("/profile")
|
||||
ApiResult<UserProfileResponse> getCurrentUserProfile(@RequestAttribute("currentUsername") String username){
|
||||
return ApiResult.success(userProfileServcie.findUserWithRoles(username));
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新用户自身信息
|
||||
* @param username 用户名
|
||||
* @param request 用户更新信息
|
||||
* @return 用户信息
|
||||
*/
|
||||
@PatchMapping("/profile")
|
||||
ApiResult<UserProfileResponse> getUserInfo(@RequestAttribute("currentUsername") String username, @RequestBody UserProfileUpdateRequest request){
|
||||
return ApiResult.success(userProfileServcie.updateUserProfile(username,request));
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置用户昵称
|
||||
* @param request 用户昵称
|
||||
*/
|
||||
@PreAuthorize("hasAuthority('admin')")
|
||||
@PutMapping("/{userId}/nickname")
|
||||
ApiResult<UserProfileResponse> setNickname(@PathVariable Long userId, @RequestBody SetUserNicknameRequest request){
|
||||
return ApiResult.success(userProfileServcie.setUserNickname(userId,request.getNickname()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user