|
|
@@ -39,11 +39,16 @@ public class PostController {
|
|
|
@RequestParam(defaultValue = "10") int pageSize,
|
|
|
Authentication authentication) {
|
|
|
Long userId = null;
|
|
|
+ Long expertId = null;
|
|
|
if (authentication != null && authentication.isAuthenticated()) {
|
|
|
JwtUserDetails details = (JwtUserDetails) authentication.getPrincipal();
|
|
|
- userId = details.getUser().getId();
|
|
|
+ User currentUser = details.getUser();
|
|
|
+ userId = currentUser.getId();
|
|
|
+ if ("expert".equals(currentUser.getRole())) {
|
|
|
+ expertId = currentUser.getId();
|
|
|
+ }
|
|
|
}
|
|
|
- Page<PostVo> page = postService.listPosts(keyword, status, pageNum, pageSize, userId);
|
|
|
+ Page<PostVo> page = postService.listPosts(keyword, status, pageNum, pageSize, userId, expertId);
|
|
|
return Response.ok(new PageVo<>(page.getTotal(), page.getRecords()));
|
|
|
}
|
|
|
|
|
|
@@ -99,10 +104,21 @@ public class PostController {
|
|
|
return Response.ok();
|
|
|
}
|
|
|
|
|
|
- @Operation(summary = "更新帖子(管理员)")
|
|
|
- @PreAuthorize("hasRole('ROLE_admin')")
|
|
|
+ @Operation(summary = "更新帖子(管理员/本人)")
|
|
|
@PutMapping("{id}")
|
|
|
- public Response<Void> updatePost(@PathVariable Long id, @Valid @RequestBody PostDto dto) {
|
|
|
+ public Response<Void> updatePost(@PathVariable Long id, @Valid @RequestBody PostDto dto, Authentication authentication) {
|
|
|
+ JwtUserDetails details = (JwtUserDetails) authentication.getPrincipal();
|
|
|
+ User currentUser = userService.queryById(details.getUser().getId());
|
|
|
+
|
|
|
+ Post existing = postService.getById(id);
|
|
|
+ if (existing == null) return Response.error("帖子不存在");
|
|
|
+
|
|
|
+ boolean isAdmin = "admin".equals(currentUser.getRole());
|
|
|
+ boolean isOwner = existing.getExpertId().equals(currentUser.getId());
|
|
|
+ if (!isAdmin && !isOwner) {
|
|
|
+ return Response.error("无权编辑此帖子");
|
|
|
+ }
|
|
|
+
|
|
|
Post post = new Post();
|
|
|
post.setTitle(dto.getTitle());
|
|
|
post.setContentIntro(dto.getContentIntro());
|