This is an automated email from the ASF dual-hosted git repository.

benjobs pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-streampark.git


The following commit(s) were added to refs/heads/dev by this push:
     new 1f23ae925 [Improve] some code improvement: Simplify annotations (#2642)
1f23ae925 is described below

commit 1f23ae9259646a8d28c5ca3f5418af14a9eff4f9
Author: zhoulii <[email protected]>
AuthorDate: Mon Apr 17 12:43:35 2023 +0800

    [Improve] some code improvement: Simplify annotations (#2642)
    
    Co-authored-by: zhoulii <[email protected]>
---
 .../console/core/annotation/CheckUser.java         | 29 -------
 .../{CheckApp.java => PermissionAction.java}       |  8 +-
 .../console/core/aspect/StreamParkAspect.java      | 97 +++++++++-------------
 .../ApplicationBuildPipelineController.java        |  5 +-
 .../core/controller/ApplicationController.java     | 26 +++---
 .../CheckTeam.java => enums/PermissionType.java}   | 29 +++++--
 .../system/controller/MemberController.java        |  9 +-
 .../console/system/controller/UserController.java  |  5 +-
 8 files changed, 88 insertions(+), 120 deletions(-)

diff --git 
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/annotation/CheckUser.java
 
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/annotation/CheckUser.java
deleted file mode 100644
index b3dd6dce7..000000000
--- 
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/annotation/CheckUser.java
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *    http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.streampark.console.core.annotation;
-
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
-
-@Target(ElementType.METHOD)
-@Retention(RetentionPolicy.RUNTIME)
-public @interface CheckUser {
-  String value() default "";
-}
diff --git 
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/annotation/CheckApp.java
 
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/annotation/PermissionAction.java
similarity index 88%
rename from 
streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/annotation/CheckApp.java
rename to 
streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/annotation/PermissionAction.java
index ae683b6aa..04d964c61 100644
--- 
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/annotation/CheckApp.java
+++ 
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/annotation/PermissionAction.java
@@ -17,6 +17,8 @@
 
 package org.apache.streampark.console.core.annotation;
 
+import org.apache.streampark.console.core.enums.PermissionType;
+
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
@@ -24,6 +26,8 @@ import java.lang.annotation.Target;
 
 @Target(ElementType.METHOD)
 @Retention(RetentionPolicy.RUNTIME)
-public @interface CheckApp {
-  String value() default "";
+public @interface PermissionAction {
+  String id();
+
+  PermissionType type();
 }
diff --git 
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/aspect/StreamParkAspect.java
 
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/aspect/StreamParkAspect.java
index ce06b9009..bee22859e 100644
--- 
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/aspect/StreamParkAspect.java
+++ 
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/aspect/StreamParkAspect.java
@@ -22,10 +22,9 @@ package org.apache.streampark.console.core.aspect;
 import org.apache.streampark.console.base.domain.RestResponse;
 import org.apache.streampark.console.base.exception.ApiAlertException;
 import org.apache.streampark.console.core.annotation.ApiAccess;
-import org.apache.streampark.console.core.annotation.CheckApp;
-import org.apache.streampark.console.core.annotation.CheckTeam;
-import org.apache.streampark.console.core.annotation.CheckUser;
+import org.apache.streampark.console.core.annotation.PermissionAction;
 import org.apache.streampark.console.core.entity.Application;
+import org.apache.streampark.console.core.enums.PermissionType;
 import org.apache.streampark.console.core.enums.UserType;
 import org.apache.streampark.console.core.service.ApplicationService;
 import org.apache.streampark.console.core.service.CommonService;
@@ -97,66 +96,46 @@ public class StreamParkAspect {
     return target;
   }
 
-  
@Pointcut("@annotation(org.apache.streampark.console.core.annotation.CheckUser)")
-  public void checkUser() {}
+  
@Pointcut("@annotation(org.apache.streampark.console.core.annotation.PermissionAction)")
+  public void permissionAction() {}
 
-  @Around("checkUser()")
-  public RestResponse checkUser(ProceedingJoinPoint joinPoint) throws 
Throwable {
+  @Around("permissionAction()")
+  public RestResponse permissionAction(ProceedingJoinPoint joinPoint) throws 
Throwable {
     MethodSignature methodSignature = (MethodSignature) 
joinPoint.getSignature();
-    CheckUser checkUser = 
methodSignature.getMethod().getAnnotation(CheckUser.class);
-    String spELString = checkUser.value();
+    PermissionAction permissionAction =
+        methodSignature.getMethod().getAnnotation(PermissionAction.class);
+    String spELString = permissionAction.id();
+    PermissionType permissionType = permissionAction.type();
 
-    Long paramUserId = getId(joinPoint, methodSignature, spELString);
     User currentUser = commonService.getCurrentUser();
-    if (currentUser == null
-        || (currentUser.getUserType() != UserType.ADMIN
-            && !currentUser.getUserId().equals(paramUserId))) {
-      throw new ApiAlertException(
-          "Permission denied, only ADMIN user or user himself can access this 
permission");
-    }
-
-    return (RestResponse) joinPoint.proceed();
-  }
-
-  
@Pointcut("@annotation(org.apache.streampark.console.core.annotation.CheckTeam)")
-  public void checkTeam() {}
-
-  @Around("checkTeam()")
-  public RestResponse checkTeam(ProceedingJoinPoint joinPoint) throws 
Throwable {
-    MethodSignature methodSignature = (MethodSignature) 
joinPoint.getSignature();
-    CheckTeam checkTeam = 
methodSignature.getMethod().getAnnotation(CheckTeam.class);
-    String spELString = checkTeam.value();
-
-    Long paramTeamId = getId(joinPoint, methodSignature, spELString);
-    User currentUser = commonService.getCurrentUser();
-    if (currentUser == null
-        || (currentUser.getUserType() != UserType.ADMIN
-            && memberService.findByUserName(paramTeamId, 
currentUser.getUsername()) == null)) {
-      throw new ApiAlertException(
-          "Permission denied, only ADMIN user or user belongs to this team can 
access this permission");
-    }
-
-    return (RestResponse) joinPoint.proceed();
-  }
-
-  
@Pointcut("@annotation(org.apache.streampark.console.core.annotation.CheckApp)")
-  public void checkApp() {}
-
-  @Around("checkApp()")
-  public RestResponse checkApp(ProceedingJoinPoint joinPoint) throws Throwable 
{
-    MethodSignature methodSignature = (MethodSignature) 
joinPoint.getSignature();
-    CheckApp checkApp = 
methodSignature.getMethod().getAnnotation(CheckApp.class);
-    String spELString = checkApp.value();
-
-    Long paramAppId = getId(joinPoint, methodSignature, spELString);
-    Application app = applicationService.getById(paramAppId);
-    User currentUser = commonService.getCurrentUser();
-    if (currentUser == null
-        || (app != null
-            && currentUser.getUserType() != UserType.ADMIN
-            && memberService.findByUserName(app.getTeamId(), 
currentUser.getUsername()) == null)) {
-      throw new ApiAlertException(
-          "Permission denied, only ADMIN user or user belongs to this team can 
access this permission");
+    ApiAlertException.throwIfNull(currentUser, "Permission denied, please 
login first.");
+
+    Long paramId = getId(joinPoint, methodSignature, spELString);
+    switch (permissionType) {
+      case USER:
+        ApiAlertException.throwIfFalse(
+            !(currentUser.getUserType() != UserType.ADMIN
+                && !currentUser.getUserId().equals(paramId)),
+            "Permission denied, only ADMIN user or user himself can access 
this permission");
+        break;
+      case TEAM:
+        ApiAlertException.throwIfFalse(
+            !(currentUser.getUserType() != UserType.ADMIN
+                && memberService.findByUserName(paramId, 
currentUser.getUsername()) == null),
+            "Permission denied, only ADMIN user or user belongs to this team 
can access this permission");
+        break;
+      case APP:
+        Application app = applicationService.getById(paramId);
+        ApiAlertException.throwIfFalse(
+            !(app != null
+                && currentUser.getUserType() != UserType.ADMIN
+                && memberService.findByUserName(app.getTeamId(), 
currentUser.getUsername())
+                    == null),
+            "Permission denied, only ADMIN user or user belongs to this team 
can access this permission");
+        break;
+      default:
+        throw new IllegalArgumentException(
+            String.format("Permission type %s is not supported.", 
permissionType));
     }
 
     return (RestResponse) joinPoint.proceed();
diff --git 
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/controller/ApplicationBuildPipelineController.java
 
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/controller/ApplicationBuildPipelineController.java
index 1d4927b61..52aecfcb5 100644
--- 
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/controller/ApplicationBuildPipelineController.java
+++ 
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/controller/ApplicationBuildPipelineController.java
@@ -21,12 +21,13 @@ import 
org.apache.streampark.console.base.domain.ApiDocConstant;
 import org.apache.streampark.console.base.domain.RestResponse;
 import org.apache.streampark.console.base.exception.ApiAlertException;
 import org.apache.streampark.console.core.annotation.ApiAccess;
-import org.apache.streampark.console.core.annotation.CheckApp;
+import org.apache.streampark.console.core.annotation.PermissionAction;
 import org.apache.streampark.console.core.bean.AppBuildDockerResolvedDetail;
 import org.apache.streampark.console.core.entity.AppBuildPipeline;
 import org.apache.streampark.console.core.entity.Application;
 import org.apache.streampark.console.core.entity.ApplicationLog;
 import org.apache.streampark.console.core.entity.FlinkEnv;
+import org.apache.streampark.console.core.enums.PermissionType;
 import org.apache.streampark.console.core.service.AppBuildPipeService;
 import org.apache.streampark.console.core.service.ApplicationLogService;
 import org.apache.streampark.console.core.service.ApplicationService;
@@ -91,7 +92,7 @@ public class ApplicationBuildPipelineController {
         schema = @Schema(defaultValue = "false", implementation = 
boolean.class))
   })
   @ApiAccess
-  @CheckApp("#appId")
+  @PermissionAction(id = "#appId", type = PermissionType.APP)
   @PostMapping(value = "build")
   @RequiresPermissions("app:create")
   public RestResponse buildApplication(Long appId, boolean forceBuild) {
diff --git 
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/controller/ApplicationController.java
 
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/controller/ApplicationController.java
index 0639fb392..12486ef8a 100644
--- 
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/controller/ApplicationController.java
+++ 
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/controller/ApplicationController.java
@@ -25,13 +25,13 @@ import 
org.apache.streampark.console.base.domain.RestResponse;
 import org.apache.streampark.console.base.exception.InternalException;
 import org.apache.streampark.console.core.annotation.ApiAccess;
 import org.apache.streampark.console.core.annotation.AppUpdated;
-import org.apache.streampark.console.core.annotation.CheckApp;
-import org.apache.streampark.console.core.annotation.CheckTeam;
+import org.apache.streampark.console.core.annotation.PermissionAction;
 import org.apache.streampark.console.core.bean.AppControl;
 import org.apache.streampark.console.core.entity.Application;
 import org.apache.streampark.console.core.entity.ApplicationBackUp;
 import org.apache.streampark.console.core.entity.ApplicationLog;
 import org.apache.streampark.console.core.enums.AppExistsState;
+import org.apache.streampark.console.core.enums.PermissionType;
 import org.apache.streampark.console.core.service.AppBuildPipeService;
 import org.apache.streampark.console.core.service.ApplicationBackUpService;
 import org.apache.streampark.console.core.service.ApplicationLogService;
@@ -91,7 +91,7 @@ public class ApplicationController {
 
   @Operation(summary = "Create application")
   @ApiAccess
-  @CheckTeam("#app.teamId")
+  @PermissionAction(id = "#app.teamId", type = PermissionType.TEAM)
   @PostMapping("create")
   @RequiresPermissions("app:create")
   public RestResponse create(Application app) throws IOException {
@@ -117,7 +117,7 @@ public class ApplicationController {
     @Parameter(name = "args", description = "new application args", in = 
ParameterIn.QUERY)
   })
   @ApiAccess
-  @CheckApp("#app.id")
+  @PermissionAction(id = "#app.id", type = PermissionType.APP)
   @PostMapping(value = "copy")
   @RequiresPermissions("app:copy")
   public RestResponse copy(@Parameter(hidden = true) Application app) throws 
IOException {
@@ -131,7 +131,7 @@ public class ApplicationController {
 
   @Operation(summary = "Update application")
   @AppUpdated
-  @CheckApp("#app.id")
+  @PermissionAction(id = "#app.id", type = PermissionType.APP)
   @PostMapping("update")
   @RequiresPermissions("app:update")
   public RestResponse update(Application app) {
@@ -194,7 +194,7 @@ public class ApplicationController {
 
   @Operation(summary = "Revoke application")
   @AppUpdated
-  @CheckApp("#app.id")
+  @PermissionAction(id = "#app.id", type = PermissionType.APP)
   @PostMapping("revoke")
   @RequiresPermissions("app:release")
   public RestResponse revoke(Application app) {
@@ -233,7 +233,7 @@ public class ApplicationController {
         schema = @Schema(implementation = boolean.class, defaultValue = 
"false"))
   })
   @ApiAccess
-  @CheckApp("#app.id")
+  @PermissionAction(id = "#app.id", type = PermissionType.APP)
   @PostMapping(value = "start")
   @RequiresPermissions("app:start")
   public RestResponse start(@Parameter(hidden = true) Application app) {
@@ -278,7 +278,7 @@ public class ApplicationController {
         example = "false",
         schema = @Schema(implementation = boolean.class, defaultValue = 
"false"))
   })
-  @CheckApp("#app.id")
+  @PermissionAction(id = "#app.id", type = PermissionType.APP)
   @PostMapping(value = "cancel")
   @RequiresPermissions("app:cancel")
   public RestResponse cancel(@Parameter(hidden = true) Application app) throws 
Exception {
@@ -289,7 +289,7 @@ public class ApplicationController {
   @Operation(summary = "Clean application")
   @AppUpdated
   @ApiAccess
-  @CheckApp("#app.id")
+  @PermissionAction(id = "#app.id", type = PermissionType.APP)
   @PostMapping("clean")
   @RequiresPermissions("app:clean")
   public RestResponse clean(Application app) {
@@ -299,7 +299,7 @@ public class ApplicationController {
 
   /** force stop(stop normal start or in progress) */
   @Operation(summary = "Force stop application")
-  @CheckApp("#app.id")
+  @PermissionAction(id = "#app.id", type = PermissionType.APP)
   @PostMapping("forcedStop")
   @RequiresPermissions("app:cancel")
   public RestResponse forcedStop(Application app) {
@@ -356,7 +356,7 @@ public class ApplicationController {
   }
 
   @Operation(summary = "Delete application operation log")
-  @CheckApp("#applicationLog.appId")
+  @PermissionAction(id = "#applicationLog.appId", type = PermissionType.APP)
   @PostMapping("deleteOperationLog")
   @RequiresPermissions("app:delete")
   public RestResponse deleteOperationLog(ApplicationLog applicationLog) {
@@ -365,7 +365,7 @@ public class ApplicationController {
   }
 
   @Operation(summary = "Delete application")
-  @CheckApp("#app.id")
+  @PermissionAction(id = "#app.id", type = PermissionType.APP)
   @PostMapping("delete")
   @RequiresPermissions("app:delete")
   public RestResponse delete(Application app) throws InternalException {
@@ -374,7 +374,7 @@ public class ApplicationController {
   }
 
   @Operation(summary = "Backup application when deleted")
-  @CheckApp("#backUp.appId")
+  @PermissionAction(id = "#backUp.appId", type = PermissionType.APP)
   @PostMapping("deletebak")
   public RestResponse deleteBak(ApplicationBackUp backUp) throws 
InternalException {
     Boolean deleted = backUpService.delete(backUp.getId());
diff --git 
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/annotation/CheckTeam.java
 
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/PermissionType.java
similarity index 65%
rename from 
streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/annotation/CheckTeam.java
rename to 
streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/PermissionType.java
index 7c69b6046..2609e9f5f 100644
--- 
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/annotation/CheckTeam.java
+++ 
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/core/enums/PermissionType.java
@@ -15,15 +15,26 @@
  * limitations under the License.
  */
 
-package org.apache.streampark.console.core.annotation;
+package org.apache.streampark.console.core.enums;
 
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
+import java.util.Arrays;
 
-@Target(ElementType.METHOD)
-@Retention(RetentionPolicy.RUNTIME)
-public @interface CheckTeam {
-  String value() default "";
+public enum PermissionType {
+  USER(1),
+  TEAM(2),
+  APP(3);
+
+  private final int value;
+
+  public int get() {
+    return this.value;
+  }
+
+  PermissionType(int value) {
+    this.value = value;
+  }
+
+  public static PermissionType of(Integer value) {
+    return Arrays.stream(values()).filter((x) -> x.value == 
value).findFirst().orElse(null);
+  }
 }
diff --git 
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/system/controller/MemberController.java
 
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/system/controller/MemberController.java
index 462c38431..bcbfd2046 100644
--- 
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/system/controller/MemberController.java
+++ 
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/system/controller/MemberController.java
@@ -19,7 +19,8 @@ package org.apache.streampark.console.system.controller;
 
 import org.apache.streampark.console.base.domain.RestRequest;
 import org.apache.streampark.console.base.domain.RestResponse;
-import org.apache.streampark.console.core.annotation.CheckTeam;
+import org.apache.streampark.console.core.annotation.PermissionAction;
+import org.apache.streampark.console.core.enums.PermissionType;
 import org.apache.streampark.console.system.entity.Member;
 import org.apache.streampark.console.system.entity.Team;
 import org.apache.streampark.console.system.entity.User;
@@ -82,7 +83,7 @@ public class MemberController {
   }
 
   @Operation(summary = "Create member")
-  @CheckTeam("#member.teamId")
+  @PermissionAction(id = "#member.teamId", type = PermissionType.TEAM)
   @PostMapping("post")
   @RequiresPermissions("member:add")
   public RestResponse create(@Valid Member member) {
@@ -91,7 +92,7 @@ public class MemberController {
   }
 
   @Operation(summary = "Delete member")
-  @CheckTeam("#member.teamId")
+  @PermissionAction(id = "#member.teamId", type = PermissionType.TEAM)
   @DeleteMapping("delete")
   @RequiresPermissions("member:delete")
   public RestResponse delete(Member member) {
@@ -100,7 +101,7 @@ public class MemberController {
   }
 
   @Operation(summary = "Update member")
-  @CheckTeam("#member.teamId")
+  @PermissionAction(id = "#member.teamId", type = PermissionType.TEAM)
   @PutMapping("update")
   @RequiresPermissions("member:update")
   public RestResponse update(Member member) {
diff --git 
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/system/controller/UserController.java
 
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/system/controller/UserController.java
index 0985c3d0f..665995830 100644
--- 
a/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/system/controller/UserController.java
+++ 
b/streampark-console/streampark-console-service/src/main/java/org/apache/streampark/console/system/controller/UserController.java
@@ -21,8 +21,9 @@ import org.apache.streampark.console.base.domain.ResponseCode;
 import org.apache.streampark.console.base.domain.RestRequest;
 import org.apache.streampark.console.base.domain.RestResponse;
 import org.apache.streampark.console.base.exception.ApiAlertException;
-import org.apache.streampark.console.core.annotation.CheckUser;
+import org.apache.streampark.console.core.annotation.PermissionAction;
 import org.apache.streampark.console.core.enums.LoginType;
+import org.apache.streampark.console.core.enums.PermissionType;
 import org.apache.streampark.console.core.enums.UserType;
 import org.apache.streampark.console.core.service.CommonService;
 import org.apache.streampark.console.system.entity.Team;
@@ -115,7 +116,7 @@ public class UserController {
   }
 
   @Operation(summary = "Update password")
-  @CheckUser("#user.userId")
+  @PermissionAction(id = "#user.userId", type = PermissionType.USER)
   @PutMapping("password")
   public RestResponse updatePassword(User user) throws Exception {
     userService.updatePassword(user);

Reply via email to