mirror of
https://gitee.com/mateos/mateclaw.git
synced 2026-09-15 11:58:34 +08:00
fix(memory): resolve morning-card user id from the JWT username
This commit is contained in:
parent
71aba2649e
commit
ff3e5b42f5
@ -17,6 +17,9 @@ import vip.mate.memory.repository.MemoryRecallMapper;
|
|||||||
import vip.mate.memory.service.MorningCardService;
|
import vip.mate.memory.service.MorningCardService;
|
||||||
import vip.mate.memory.service.MemoryHilService;
|
import vip.mate.memory.service.MemoryHilService;
|
||||||
import vip.mate.workspace.core.annotation.RequireWorkspaceRole;
|
import vip.mate.workspace.core.annotation.RequireWorkspaceRole;
|
||||||
|
import vip.mate.auth.model.UserEntity;
|
||||||
|
import vip.mate.auth.service.AuthService;
|
||||||
|
import vip.mate.exception.MateClawException;
|
||||||
|
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -38,6 +41,7 @@ public class DreamController {
|
|||||||
private final MorningCardService morningCardService;
|
private final MorningCardService morningCardService;
|
||||||
private final MemoryHilService hilService;
|
private final MemoryHilService hilService;
|
||||||
private final DreamEventBroadcaster eventBroadcaster;
|
private final DreamEventBroadcaster eventBroadcaster;
|
||||||
|
private final AuthService authService;
|
||||||
|
|
||||||
@Operation(summary = "List dream reports (paginated, newest first)")
|
@Operation(summary = "List dream reports (paginated, newest first)")
|
||||||
@GetMapping("/reports")
|
@GetMapping("/reports")
|
||||||
@ -94,7 +98,6 @@ public class DreamController {
|
|||||||
@RequireWorkspaceRole("member")
|
@RequireWorkspaceRole("member")
|
||||||
public R<Map<String, Object>> getMorningCard(@PathVariable Long agentId, Authentication auth) {
|
public R<Map<String, Object>> getMorningCard(@PathVariable Long agentId, Authentication auth) {
|
||||||
Long userId = resolveUserId(auth);
|
Long userId = resolveUserId(auth);
|
||||||
if (userId == null) return R.fail("Not authenticated");
|
|
||||||
Map<String, Object> card = morningCardService.getCardFor(userId, agentId);
|
Map<String, Object> card = morningCardService.getCardFor(userId, agentId);
|
||||||
return R.ok(card); // null = no card to show
|
return R.ok(card); // null = no card to show
|
||||||
}
|
}
|
||||||
@ -106,7 +109,6 @@ public class DreamController {
|
|||||||
@RequestBody Map<String, Object> body,
|
@RequestBody Map<String, Object> body,
|
||||||
Authentication auth) {
|
Authentication auth) {
|
||||||
Long userId = resolveUserId(auth);
|
Long userId = resolveUserId(auth);
|
||||||
if (userId == null) return R.fail("Not authenticated");
|
|
||||||
Long reportId = body.get("reportId") != null
|
Long reportId = body.get("reportId") != null
|
||||||
? Long.valueOf(body.get("reportId").toString()) : null;
|
? Long.valueOf(body.get("reportId").toString()) : null;
|
||||||
morningCardService.markSeen(userId, agentId, reportId);
|
morningCardService.markSeen(userId, agentId, reportId);
|
||||||
@ -178,17 +180,19 @@ public class DreamController {
|
|||||||
return R.ok(null);
|
return R.ok(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolve the authenticated user's id from the JWT principal. The auth
|
||||||
|
* filter exposes the username via {@link Authentication#getName()}, so the
|
||||||
|
* id is looked up by username — matching AgentController / WorkspaceController.
|
||||||
|
*/
|
||||||
private Long resolveUserId(Authentication auth) {
|
private Long resolveUserId(Authentication auth) {
|
||||||
if (auth == null) return null;
|
if (auth == null) {
|
||||||
try {
|
throw new MateClawException("err.auth.unauthenticated", 401, "Not authenticated");
|
||||||
Object principal = auth.getPrincipal();
|
|
||||||
if (principal instanceof vip.mate.auth.model.UserEntity user) {
|
|
||||||
return user.getId();
|
|
||||||
}
|
|
||||||
// No stable user ID available — refuse rather than fabricate
|
|
||||||
return null;
|
|
||||||
} catch (Exception e) {
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
UserEntity user = authService.findByUsername(auth.getName());
|
||||||
|
if (user == null) {
|
||||||
|
throw new MateClawException("err.auth.user_not_found", 401, "User not found: " + auth.getName());
|
||||||
|
}
|
||||||
|
return user.getId();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user