|
|
@@ -5,10 +5,13 @@ import com.njuzr.eaibackend.dto.UserRegisterDTO;
|
|
|
import com.njuzr.eaibackend.exception.MyException;
|
|
|
import com.njuzr.eaibackend.service.UserService;
|
|
|
import com.njuzr.eaibackend.utils.ModelMapperUtil;
|
|
|
+import com.njuzr.eaibackend.utils.ObjectFieldCheckerUtil;
|
|
|
import com.njuzr.eaibackend.vo.UserVO;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
|
+import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
|
|
+import org.springframework.security.core.userdetails.UserDetails;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
/**
|
|
|
@@ -29,10 +32,32 @@ public class UserController {
|
|
|
this.userService = userService;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 用户更新自己的用户信息(固定信息不可更改,例如真实姓名、邮箱、学号、角色等)
|
|
|
+ * @param id
|
|
|
+ * @param userDTO
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @PreAuthorize("hasRole('ROLE_STUDENT') or hasRole('ROLE_TEACHER')")
|
|
|
+ @PutMapping
|
|
|
+ public MyResponse updateUser(@AuthenticationPrincipal(expression = "id") Long id, @RequestBody UserDTO userDTO) {
|
|
|
+ log.info("UserDTO解析成功,对象如下:"+userDTO);
|
|
|
+ UserVO userVO;
|
|
|
+ if (ObjectFieldCheckerUtil.areAllFieldsNullOrAbsent(userDTO, "id", "name", "officialEmail", "officialNumber", "role", "createTime")) {
|
|
|
+ userVO = userService.updateUser(id, userDTO);
|
|
|
+ return MyResponse.success(userVO);
|
|
|
+ }
|
|
|
+ return MyResponse.error(400, "请求体中包含了不能更新的字段");
|
|
|
+ }
|
|
|
+
|
|
|
@GetMapping("/{id}")
|
|
|
- @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_TEACHER')")
|
|
|
- public MyResponse findUserById(@PathVariable Long id) {
|
|
|
- return MyResponse.success(id);
|
|
|
+// @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_TEACHER')")
|
|
|
+ public MyResponse findUserById(@PathVariable Long id, @AuthenticationPrincipal UserDetails userDetails) {
|
|
|
+ log.info("用户认证成功,解析出的UserDetails信息如下:"+userDetails.getAuthorities());
|
|
|
+ UserVO userVO = userService.findById(id);
|
|
|
+ log.info("生成的userVO信息如下:"+userVO);
|
|
|
+ return MyResponse.success(userVO);
|
|
|
}
|
|
|
|
|
|
}
|