Przeglądaj źródła

fix:修复管理员不能通过id查询非自己宠物的问题

yangyi 4 miesięcy temu
rodzic
commit
696735944f

+ 2 - 1
src/main/java/space/anyi/petServiceManagementSystem/server/controller/PetController.java

@@ -40,8 +40,9 @@ public class PetController {
     @GetMapping("/{id}")
     @Operation(summary = "获取宠物详情")
     public R<PetVo> getById(@PathVariable Long id, HttpServletRequest request) {
+        String role = (String) request.getAttribute("role");
         Long userId = (Long) request.getAttribute("userId");
-        return R.ok(petService.getById(id, userId));
+        return R.ok(petService.getById(id, userId,role));
     }
 
     @PutMapping("/{id}")

+ 1 - 1
src/main/java/space/anyi/petServiceManagementSystem/server/service/PetService.java

@@ -12,7 +12,7 @@ import java.util.List;
 public interface PetService {
     List<PetVo> list(Long userId);
     PetVo add(PetDto dto, Long userId);
-    PetVo getById(Long id, Long userId);
+    PetVo getById(Long id, Long userId,String role);
     Boolean update(Long id, PetDto dto, Long userId);
     Boolean delete(Long id, Long userId);
     List<VaccinationVo> getVaccinations(Long petId, Long userId);

+ 2 - 2
src/main/java/space/anyi/petServiceManagementSystem/server/service/impl/PetServiceImpl.java

@@ -57,9 +57,9 @@ public class PetServiceImpl implements PetService {
     }
 
     @Override
-    public PetVo getById(Long id, Long userId) {
+    public PetVo getById(Long id, Long userId,String role) {
         Pet pet = petMapper.selectById(id);
-        if (pet == null || !pet.getUserId().equals(userId)) {
+        if (pet == null || !pet.getUserId().equals(userId) && !"admin".equals(role)) {
             throw new BusinessException("宠物不存在或无权访问");
         }
         return toPetVo(pet);