diff --git a/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/annotations/CreateBy.java b/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/annotations/CreateBy.java new file mode 100644 index 000000000..44af3805c --- /dev/null +++ b/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/annotations/CreateBy.java @@ -0,0 +1,28 @@ +/* + * 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 + * + * https://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.bigtop.manager.dao.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(value = {ElementType.FIELD}) +public @interface CreateBy {} diff --git a/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/annotations/CreateTime.java b/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/annotations/CreateTime.java new file mode 100644 index 000000000..288657bf4 --- /dev/null +++ b/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/annotations/CreateTime.java @@ -0,0 +1,28 @@ +/* + * 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 + * + * https://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.bigtop.manager.dao.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(value = {ElementType.FIELD}) +public @interface CreateTime {} diff --git a/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/annotations/UpdateBy.java b/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/annotations/UpdateBy.java new file mode 100644 index 000000000..f2e63eed0 --- /dev/null +++ b/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/annotations/UpdateBy.java @@ -0,0 +1,28 @@ +/* + * 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 + * + * https://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.bigtop.manager.dao.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(value = {ElementType.FIELD}) +public @interface UpdateBy {} diff --git a/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/annotations/UpdateTime.java b/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/annotations/UpdateTime.java new file mode 100644 index 000000000..b26d8242f --- /dev/null +++ b/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/annotations/UpdateTime.java @@ -0,0 +1,28 @@ +/* + * 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 + * + * https://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.bigtop.manager.dao.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(value = {ElementType.FIELD}) +public @interface UpdateTime {} diff --git a/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/po/BasePO.java b/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/po/BasePO.java index 7fe37f9ee..42fef7236 100644 --- a/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/po/BasePO.java +++ b/bigtop-manager-dao/src/main/java/org/apache/bigtop/manager/dao/po/BasePO.java @@ -18,6 +18,11 @@ */ package org.apache.bigtop.manager.dao.po; +import org.apache.bigtop.manager.dao.annotations.CreateBy; +import org.apache.bigtop.manager.dao.annotations.CreateTime; +import org.apache.bigtop.manager.dao.annotations.UpdateBy; +import org.apache.bigtop.manager.dao.annotations.UpdateTime; + import lombok.Data; import jakarta.persistence.Column; @@ -26,15 +31,19 @@ @Data public abstract class BasePO { + @CreateTime @Column(name = "create_time") private Timestamp createTime; + @UpdateTime @Column(name = "update_time") private Timestamp updateTime; + @CreateBy @Column(name = "create_by") private Long createBy; + @UpdateBy @Column(name = "update_by") private Long updateBy; } diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/interceptor/AuditingInterceptor.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/interceptor/AuditingInterceptor.java new file mode 100644 index 000000000..71e8d5cbe --- /dev/null +++ b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/interceptor/AuditingInterceptor.java @@ -0,0 +1,100 @@ +/* + * 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.bigtop.manager.server.interceptor; + +import org.apache.bigtop.manager.common.utils.ClassUtils; +import org.apache.bigtop.manager.dao.annotations.CreateBy; +import org.apache.bigtop.manager.dao.annotations.CreateTime; +import org.apache.bigtop.manager.dao.annotations.UpdateBy; +import org.apache.bigtop.manager.dao.annotations.UpdateTime; +import org.apache.bigtop.manager.dao.po.BasePO; +import org.apache.bigtop.manager.server.holder.SessionUserHolder; + +import org.apache.ibatis.executor.Executor; +import org.apache.ibatis.mapping.MappedStatement; +import org.apache.ibatis.mapping.SqlCommandType; +import org.apache.ibatis.plugin.Interceptor; +import org.apache.ibatis.plugin.Intercepts; +import org.apache.ibatis.plugin.Invocation; +import org.apache.ibatis.plugin.Signature; + +import org.springframework.stereotype.Component; + +import lombok.extern.slf4j.Slf4j; + +import java.lang.reflect.Field; +import java.sql.Timestamp; +import java.util.List; + +@Slf4j +@Component +@Intercepts({ + @Signature( + type = Executor.class, + method = "update", + args = {MappedStatement.class, Object.class}) +}) +public class AuditingInterceptor implements Interceptor { + + @Override + public Object intercept(Invocation invocation) throws Throwable { + MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0]; + SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType(); + + // Get Entity Object + Object parameter = invocation.getArgs()[1]; + log.debug("sqlCommandType {}", sqlCommandType); + + if (!(parameter instanceof BasePO)) { + return invocation.proceed(); + } + + // Get the current time and operator + Timestamp timestamp = new Timestamp(System.currentTimeMillis()); + Long currentUser = SessionUserHolder.getUserId(); + log.debug("timestamp: {} currentUser: {}", timestamp, currentUser); + + // Modify audit fields + List fields = ClassUtils.getFields(parameter.getClass()); + if (SqlCommandType.INSERT == sqlCommandType || SqlCommandType.UPDATE == sqlCommandType) { + for (Field field : fields) { + boolean accessible = field.canAccess(parameter); + field.setAccessible(true); + if (field.isAnnotationPresent(CreateBy.class) + && SqlCommandType.INSERT == sqlCommandType + && currentUser != null) { + field.set(parameter, currentUser); + } + if (field.isAnnotationPresent(CreateTime.class) && SqlCommandType.INSERT == sqlCommandType) { + field.set(parameter, timestamp); + } + if (field.isAnnotationPresent(UpdateBy.class) && currentUser != null) { + field.set(parameter, currentUser); + } + if (field.isAnnotationPresent(UpdateTime.class)) { + field.set(parameter, timestamp); + } + field.setAccessible(accessible); + } + } + + return invocation.proceed(); + } +}