From 1f833e27841588d837ba3382525909258af23477 Mon Sep 17 00:00:00 2001 From: xiwa Date: Mon, 30 May 2022 17:57:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E5=92=8C=E6=9D=83=E9=99=90?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../manager/config/GlobalExceptionHandler.java | 6 ++++++ .../iotkit/manager/config/SaTokenConfigure.java | 1 - .../manager/controller/ProtocolController.java | 6 ++++-- .../manager/controller/RuleEngineController.java | 16 ++++++++-------- .../controller/VirtualDeviceController.java | 10 ++++++++-- 5 files changed, 26 insertions(+), 13 deletions(-) diff --git a/manager/src/main/java/cc/iotkit/manager/config/GlobalExceptionHandler.java b/manager/src/main/java/cc/iotkit/manager/config/GlobalExceptionHandler.java index f38a0bae..cc56bfed 100755 --- a/manager/src/main/java/cc/iotkit/manager/config/GlobalExceptionHandler.java +++ b/manager/src/main/java/cc/iotkit/manager/config/GlobalExceptionHandler.java @@ -1,6 +1,7 @@ package cc.iotkit.manager.config; import cn.dev33.satoken.exception.NotLoginException; +import cn.dev33.satoken.exception.NotPermissionException; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @@ -24,6 +25,11 @@ public class GlobalExceptionHandler { return new RequestResult("401", "未授权的请求"); } + if (e instanceof NotPermissionException) { + response.setStatus(403); + return new RequestResult("403", "没有权限"); + } + if (e.getMessage().contains("Unauthorized")) { response.setStatus(403); return new RequestResult("403", "没有权限"); diff --git a/manager/src/main/java/cc/iotkit/manager/config/SaTokenConfigure.java b/manager/src/main/java/cc/iotkit/manager/config/SaTokenConfigure.java index 42364006..d056e72f 100755 --- a/manager/src/main/java/cc/iotkit/manager/config/SaTokenConfigure.java +++ b/manager/src/main/java/cc/iotkit/manager/config/SaTokenConfigure.java @@ -3,7 +3,6 @@ package cc.iotkit.manager.config; import cn.dev33.satoken.interceptor.SaAnnotationInterceptor; import cn.dev33.satoken.interceptor.SaRouteInterceptor; import cn.dev33.satoken.router.SaRouter; -import cn.dev33.satoken.router.SaRouterStaff; import cn.dev33.satoken.stp.StpUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Configuration; diff --git a/manager/src/main/java/cc/iotkit/manager/controller/ProtocolController.java b/manager/src/main/java/cc/iotkit/manager/controller/ProtocolController.java index 0d1fe4d8..a83a8fb3 100755 --- a/manager/src/main/java/cc/iotkit/manager/controller/ProtocolController.java +++ b/manager/src/main/java/cc/iotkit/manager/controller/ProtocolController.java @@ -53,7 +53,9 @@ public class ProtocolController { private ComponentManager componentManager; @PostMapping("/uploadJar") - public String uploadJar(@RequestParam("file") MultipartFile file, String id) { + public String uploadJar( + @RequestParam("file") MultipartFile file, + @RequestParam("id") String id) { if (file == null) { throw new BizException("file is null"); } @@ -297,7 +299,7 @@ public class ProtocolController { public void changeComponentState(@PathVariable("id") String id, @PathVariable("state") String state) { ProtocolComponent component = getAndCheckComponent(id); - if(ProtocolComponent.TYPE_DEVICE.equals(component.getType())){ + if (ProtocolComponent.TYPE_DEVICE.equals(component.getType())) { String converterId = component.getConverter(); getAndCheckConverter(converterId); } diff --git a/manager/src/main/java/cc/iotkit/manager/controller/RuleEngineController.java b/manager/src/main/java/cc/iotkit/manager/controller/RuleEngineController.java index 617639be..c8e6a407 100755 --- a/manager/src/main/java/cc/iotkit/manager/controller/RuleEngineController.java +++ b/manager/src/main/java/cc/iotkit/manager/controller/RuleEngineController.java @@ -99,7 +99,7 @@ public class RuleEngineController { @PostMapping("/rule/{ruleId}/pause") public void pauseRule(@PathVariable("ruleId") String ruleId) { Optional ruleOpt = ruleInfoRepository.findById(ruleId); - if (!ruleOpt.isPresent()) { + if (ruleOpt.isEmpty()) { throw new BizException("Rule does not exist"); } RuleInfo ruleInfo = ruleOpt.get(); @@ -112,7 +112,7 @@ public class RuleEngineController { @PostMapping("/rule/{ruleId}/resume") public void resumeRule(@PathVariable("ruleId") String ruleId) { Optional ruleOpt = ruleInfoRepository.findById(ruleId); - if (!ruleOpt.isPresent()) { + if (ruleOpt.isEmpty()) { throw new BizException("Rule does not exist"); } RuleInfo ruleInfo = ruleOpt.get(); @@ -125,7 +125,7 @@ public class RuleEngineController { @DeleteMapping("/rule/{ruleId}/delete") public void deleteRule(@PathVariable("ruleId") String ruleId) { Optional ruleOpt = ruleInfoRepository.findById(ruleId); - if (!ruleOpt.isPresent()) { + if (ruleOpt.isEmpty()) { throw new BizException("Rule does not exist"); } RuleInfo ruleInfo = ruleOpt.get(); @@ -169,7 +169,7 @@ public class RuleEngineController { taskInfo.setState(TaskInfo.STATE_STOP); } else { Optional taskOpt = taskInfoRepository.findById(taskInfo.getId()); - if (!taskOpt.isPresent()) { + if (taskOpt.isEmpty()) { throw new BizException("Task does not exist"); } TaskInfo oldTask = taskOpt.get(); @@ -183,7 +183,7 @@ public class RuleEngineController { @PostMapping("/task/{taskId}/pause") public void pauseTask(@PathVariable("taskId") String taskId) { Optional optionalTaskInfo = taskInfoRepository.findById(taskId); - if (!optionalTaskInfo.isPresent()) { + if (optionalTaskInfo.isEmpty()) { throw new BizException("Task does not exist"); } dataOwnerService.checkOwner(optionalTaskInfo.get()); @@ -193,7 +193,7 @@ public class RuleEngineController { @PostMapping("/task/{taskId}/resume") public void resumeTask(@PathVariable("taskId") String taskId) { Optional optionalTaskInfo = taskInfoRepository.findById(taskId); - if (!optionalTaskInfo.isPresent()) { + if (optionalTaskInfo.isEmpty()) { throw new BizException("Task does not exist"); } dataOwnerService.checkOwner(optionalTaskInfo.get()); @@ -203,7 +203,7 @@ public class RuleEngineController { @PostMapping("/task/{taskId}/renew") public void renewTask(@PathVariable("taskId") String taskId) { Optional optionalTaskInfo = taskInfoRepository.findById(taskId); - if (!optionalTaskInfo.isPresent()) { + if (optionalTaskInfo.isEmpty()) { throw new BizException("Task does not exist"); } TaskInfo taskInfo = optionalTaskInfo.get(); @@ -222,7 +222,7 @@ public class RuleEngineController { @DeleteMapping("/task/{taskId}/delete") public void deleteTask(@PathVariable("taskId") String taskId) { Optional optionalTaskInfo = taskInfoRepository.findById(taskId); - if (!optionalTaskInfo.isPresent()) { + if (optionalTaskInfo.isEmpty()) { throw new BizException("Task does not exist"); } TaskInfo taskInfo = optionalTaskInfo.get(); diff --git a/manager/src/main/java/cc/iotkit/manager/controller/VirtualDeviceController.java b/manager/src/main/java/cc/iotkit/manager/controller/VirtualDeviceController.java index ff433a17..ba491398 100644 --- a/manager/src/main/java/cc/iotkit/manager/controller/VirtualDeviceController.java +++ b/manager/src/main/java/cc/iotkit/manager/controller/VirtualDeviceController.java @@ -15,6 +15,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.web.bind.annotation.*; @@ -40,8 +41,13 @@ public class VirtualDeviceController { @PathVariable("size") int size, @PathVariable("page") int page) { String uid = AuthUtil.getUserId(); - Page virtualDevices = virtualDeviceRepository.findByUid(uid, - PageRequest.of(page - 1, size, Sort.by(Sort.Order.desc("createAt")))); + Pageable pageable = PageRequest.of(page - 1, size, Sort.by(Sort.Order.desc("createAt"))); + Page virtualDevices; + if (AuthUtil.isAdmin()) { + virtualDevices = virtualDeviceRepository.findAll(pageable); + } else { + virtualDevices = virtualDeviceRepository.findByUid(uid, pageable); + } return new Paging<>(virtualDevices.getTotalElements(), virtualDevices.getContent()); }