ruanwenjun commented on code in PR #15554:
URL: 
https://github.com/apache/dolphinscheduler/pull/15554#discussion_r1505823868


##########
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/audit/OperatorLog.java:
##########
@@ -0,0 +1,63 @@
+/*
+ * 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.dolphinscheduler.api.audit;
+
+import org.apache.dolphinscheduler.common.enums.AuditObjectType;
+import org.apache.dolphinscheduler.common.enums.AuditOperationType;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Custom annotation for logging and auditing operator actions in the system.
+ * This annotation can be applied to methods to indicate the type of 
operation, object type,
+ * and specific parameters to be recorded in the logs.
+ */
+@Target(ElementType.METHOD)
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface OperatorLog {
+
+    /**
+     * Specifies the type of object involved in the audit operation (default: 
PROJECT).
+     * Possible values include PROJECT, USER, etc.
+     */
+    AuditObjectType objectType() default AuditObjectType.PROJECT;
+
+    /**
+     * Specifies the type of the audit operation (default: CREATE).
+     * Possible values include CREATE, UPDATE, DELETE, etc.
+     */
+    AuditOperationType operationType() default AuditOperationType.CREATE;
+
+    /**
+     * The names of the fields in the API request to be recorded.
+     * Represents an array of key-value pairs, e.g., ["id", "status"].
+     */
+    String[] requestParamName() default {};
+
+    /**
+     * The names of the fields in the returned object to be recorded.
+     * Represents an array of field names, e.g., ["id", "code"].
+     * Specify the field names to record from the returned object.
+     */
+    String[] returnObjectFieldName() default {};

Review Comment:
   ```suggestion
       String[] resultFieldName() default {};
   ```
   Can we use json path here?



##########
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/enums/AuditOperationType.java:
##########
@@ -17,46 +17,86 @@
 
 package org.apache.dolphinscheduler.common.enums;
 
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
+
+import lombok.Getter;
 
 /**
  * Audit Operation type
  */
+@Getter
 public enum AuditOperationType {
 
-    CREATE(0, "CREATE"),
-    READ(1, "READ"),
-    UPDATE(2, "UPDATE"),
-    DELETE(3, "DELETE");
+    CREATE(0, "Create", false, false),

Review Comment:
   It's better to combine the AuditOperationType with AuditObjectType into one 
enum. e.g. PROJECT_CREATE, PROJECT_DELETE, PROJECT_UPLOAD...



##########
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/audit/OperatorLog.java:
##########
@@ -0,0 +1,63 @@
+/*
+ * 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.dolphinscheduler.api.audit;
+
+import org.apache.dolphinscheduler.common.enums.AuditObjectType;
+import org.apache.dolphinscheduler.common.enums.AuditOperationType;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Custom annotation for logging and auditing operator actions in the system.
+ * This annotation can be applied to methods to indicate the type of 
operation, object type,
+ * and specific parameters to be recorded in the logs.
+ */
+@Target(ElementType.METHOD)
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface OperatorLog {
+
+    /**
+     * Specifies the type of object involved in the audit operation (default: 
PROJECT).
+     * Possible values include PROJECT, USER, etc.
+     */
+    AuditObjectType objectType() default AuditObjectType.PROJECT;

Review Comment:
   ```suggestion
       AuditType auditType() default AuditType.PROJECT;
   ```
   Use AuditType?



##########
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/enums/AuditOperationType.java:
##########
@@ -17,46 +17,86 @@
 
 package org.apache.dolphinscheduler.common.enums;
 
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
+
+import lombok.Getter;
 
 /**
  * Audit Operation type
  */
+@Getter
 public enum AuditOperationType {
 
-    CREATE(0, "CREATE"),
-    READ(1, "READ"),
-    UPDATE(2, "UPDATE"),
-    DELETE(3, "DELETE");
+    CREATE(0, "Create", false, false),
+    UPDATE(1, "Update", false, false),
+    DELETE(2, "Delete", false, true),
+    CLOSE(3, "Close", false, true),
+
+    RELEASE(4, "Release", true, false),
+    ONLINE(5, "Online", false, false),
+    OFFLINE(6, "Offline", false, false),
+
+    RESUME_PAUSE(7, "Resume pause", false, false),
+    RESUME_FAILURE(8, "Resume failure", false, false),
+
+    IMPORT(9, "Import", false, false),
+    EXPORT(10, "Export", false, false),
+
+    EXECUTE(11, "Execute", true, false),
+    START(12, "Start", false, false),
+    RUN(13, "Run", false, false),
+    RERUN(14, "Rerun", false, false),
+    STOP(15, "Stop", false, false),
+    KILL(16, "Kill", false, false),
+    PAUSE(17, "Pause", false, false),
+    MOVE(18, "Move", false, false),
+
+    SWITCH_STATUS(19, "Switch status", false, false),
+    SWITCH_VERSION(20, "Switch version", false, false),
+    DELETE_VERSION(21, "Delete version", false, false),
+    FORCE_SUCCESS(22, "Force success", false, false),
+    RENAME(23, "Rename", false, false),
+    UPLOAD(24, "Upload", false, false),
+    AUTHORIZE(25, "Authorize", false, false),
+    UN_AUTHORIZE(26, "Un authorize", false, false),
+    COPY(27, "Copy", false, true),
+    ;
 
     private final int code;

Review Comment:
   It's better to remove the code, directly use name().



##########
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/audit/OperatorLogAspect.java:
##########
@@ -0,0 +1,396 @@
+/*
+ * 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.dolphinscheduler.api.audit;
+
+import org.apache.dolphinscheduler.api.enums.ExecuteType;
+import org.apache.dolphinscheduler.api.service.AuditService;
+import org.apache.dolphinscheduler.api.utils.Result;
+import org.apache.dolphinscheduler.common.enums.AuditObjectType;
+import org.apache.dolphinscheduler.common.enums.AuditOperationType;
+import org.apache.dolphinscheduler.common.enums.ReleaseState;
+import org.apache.dolphinscheduler.dao.entity.AuditLog;
+import org.apache.dolphinscheduler.dao.entity.Schedule;
+import org.apache.dolphinscheduler.dao.entity.User;
+import org.apache.dolphinscheduler.dao.mapper.ScheduleMapper;
+import org.apache.dolphinscheduler.dao.mapper.UserMapper;
+import org.apache.dolphinscheduler.spi.enums.ResourceType;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import lombok.extern.slf4j.Slf4j;
+
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Pointcut;
+import org.aspectj.lang.reflect.MethodSignature;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import io.swagger.v3.oas.annotations.Operation;
+
+@Aspect
+@Component
+@Slf4j
+public class OperatorLogAspect {
+
+    @Autowired
+    private AuditService auditService;
+
+    @Autowired
+    private ScheduleMapper scheduleMapper;
+
+    @Autowired
+    private UserMapper userMapper;
+
+    @Pointcut("@annotation(OperatorLog)")
+    public void logPointCut() {
+
+    }
+
+    @Around("logPointCut()")
+    public Object around(ProceedingJoinPoint point) throws Throwable {
+        long beginTime = System.currentTimeMillis();
+        MethodSignature signature = (MethodSignature) point.getSignature();
+        Method method = signature.getMethod();
+
+        OperatorLog operatorLog = method.getAnnotation(OperatorLog.class);
+        // Api don't need record log
+        if (operatorLog == null) {
+            return point.proceed();
+        }

Review Comment:
   This case shouldn't happen?
   ```suggestion
          
   ```



##########
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/audit/OperatorLogAspect.java:
##########
@@ -0,0 +1,396 @@
+/*
+ * 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.dolphinscheduler.api.audit;
+
+import org.apache.dolphinscheduler.api.enums.ExecuteType;
+import org.apache.dolphinscheduler.api.service.AuditService;
+import org.apache.dolphinscheduler.api.utils.Result;
+import org.apache.dolphinscheduler.common.enums.AuditObjectType;
+import org.apache.dolphinscheduler.common.enums.AuditOperationType;
+import org.apache.dolphinscheduler.common.enums.ReleaseState;
+import org.apache.dolphinscheduler.dao.entity.AuditLog;
+import org.apache.dolphinscheduler.dao.entity.Schedule;
+import org.apache.dolphinscheduler.dao.entity.User;
+import org.apache.dolphinscheduler.dao.mapper.ScheduleMapper;
+import org.apache.dolphinscheduler.dao.mapper.UserMapper;
+import org.apache.dolphinscheduler.spi.enums.ResourceType;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import lombok.extern.slf4j.Slf4j;
+
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Pointcut;
+import org.aspectj.lang.reflect.MethodSignature;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import io.swagger.v3.oas.annotations.Operation;
+
+@Aspect
+@Component
+@Slf4j
+public class OperatorLogAspect {
+
+    @Autowired
+    private AuditService auditService;
+
+    @Autowired
+    private ScheduleMapper scheduleMapper;
+
+    @Autowired
+    private UserMapper userMapper;
+
+    @Pointcut("@annotation(OperatorLog)")
+    public void logPointCut() {
+
+    }
+
+    @Around("logPointCut()")
+    public Object around(ProceedingJoinPoint point) throws Throwable {
+        long beginTime = System.currentTimeMillis();
+        MethodSignature signature = (MethodSignature) point.getSignature();
+        Method method = signature.getMethod();
+
+        OperatorLog operatorLog = method.getAnnotation(OperatorLog.class);
+        // Api don't need record log
+        if (operatorLog == null) {
+            return point.proceed();
+        }
+
+        AuditObjectType auditObjectType = operatorLog.objectType();
+        AuditOperationType auditOperationType = operatorLog.operationType();
+
+        Operation operation = method.getAnnotation(Operation.class);
+        if (operation == null) {
+            log.error("api operation is null");

Review Comment:
   ```suggestion
               log.warn("Operation is null of method: {}", method.getName);
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to