diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/AppApiKeysServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/AppApiKeysServiceImpl.java index 658a194..90203f4 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/AppApiKeysServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/AppApiKeysServiceImpl.java @@ -58,31 +58,45 @@ public class AppApiKeysServiceImpl extends ServiceImpl list) { + Long currentUserId = requireCurrentUserId(); for (AppApiKeysPlus item : list) { - if (null != SecurityUtils.getUserId() && item.getUserId().longValue() != SecurityUtils.getUserId()) { - throw new RuntimeException("无权限"); + if (item == null || !Objects.equals(item.getUserId(), currentUserId)) { + throw new CustomException("无权操作该 API Key"); } } } private void checkUser(List list) { + Long currentUserId = requireCurrentUserId(); for (AppApiKeys item : list) { - if (null != SecurityUtils.getUserId() && item.getUserId().longValue() != SecurityUtils.getUserId()) { - throw new RuntimeException("无权限"); + if (item == null || !Objects.equals(item.getUserId(), currentUserId)) { + throw new CustomException("无权操作该 API Key"); } } } private void checkUser(AppApiKeysPlus item) { - if (null != SecurityUtils.getUserId() && item.getUserId().longValue() != SecurityUtils.getUserId()) { - throw new RuntimeException("无权限"); + if (item == null) { + throw new CustomException("API Key 不存在或已删除"); + } + if (!Objects.equals(item.getUserId(), requireCurrentUserId())) { + throw new CustomException("无权操作该 API Key"); } } private void checkUser(AppApiKeys item) { - if (null != SecurityUtils.getUserId() && item.getUserId().longValue() != SecurityUtils.getUserId()) { - throw new RuntimeException("无权限"); + if (item == null) { + throw new CustomException("API Key 不存在或已删除"); } + if (!Objects.equals(item.getUserId(), requireCurrentUserId())) { + throw new CustomException("无权操作该 API Key"); + } + } + + private Long requireCurrentUserId() { + Long userId = SecurityUtils.getUserId(); + if (userId == null) throw new CustomException("无法获取当前用户"); + return userId; } @Override @@ -297,7 +311,7 @@ public class AppApiKeysServiceImpl extends ServiceImpl