Parcourir la source

数据库字段自动填充instId,createdBy,createdDate,modifiedBy,modifiedDate

instId,createdBy,createdDate,modifiedBy,modifiedDate
MaxKey il y a 8 mois
Parent
commit
8b209be4c4

+ 15 - 0
maxkey-authentications/maxkey-authentication-core/src/main/java/org/dromara/maxkey/authn/SignPrincipal.java

@@ -32,6 +32,9 @@ public class SignPrincipal implements  UserDetails {
     
     UserDetails userDetails;
     
+    String instId;
+    String userId;
+    
     String sessionId;
     
     List<GrantedAuthority> grantedAuthority;
@@ -61,6 +64,8 @@ public class SignPrincipal implements  UserDetails {
      */
     public SignPrincipal(UserInfo userInfo) {
         this.userInfo = userInfo;
+        this.userId =userInfo.getId();
+        this.instId = userInfo.getInstId();
         this.authenticated = true;
         this.accountNonExpired = true;
         this.accountNonLocked  = true;
@@ -70,6 +75,8 @@ public class SignPrincipal implements  UserDetails {
     
     public SignPrincipal(UserInfo userInfo,Session session) {
         this.userInfo = userInfo;
+        this.userId =userInfo.getId();
+        this.instId = userInfo.getInstId();
         this.authenticated = true;
         this.accountNonExpired = true;
         this.accountNonLocked  = true;
@@ -157,6 +164,14 @@ public class SignPrincipal implements  UserDetails {
 	public void setSessionId(String sessionId) {
 		this.sessionId = sessionId;
 	}
+	
+	public String getInstId() {
+		return instId;
+	}
+
+	public String getUserId() {
+		return userId;
+	}
 
 	@Override
     public boolean isEnabled() {

+ 48 - 0
maxkey-authentications/maxkey-authentication-core/src/main/java/org/dromara/maxkey/authn/web/PersistFieldAutoFillHandler.java

@@ -0,0 +1,48 @@
+package org.dromara.maxkey.authn.web;
+
+import java.util.Date;
+
+import org.apache.ibatis.reflection.MetaObject;
+import org.dromara.maxkey.authn.SignPrincipal;
+import org.dromara.mybatis.jpa.handler.FieldAutoFillHandler;
+import org.springframework.stereotype.Component;
+
+@Component
+public class PersistFieldAutoFillHandler   extends FieldAutoFillHandler{
+
+	@Override
+	public void insertFill(MetaObject metaObject) {
+		
+		SignPrincipal principal = getPrincipal();
+		if(principal != null) {
+			this.setFieldValue(metaObject , "instId", principal.getInstId());
+			this.setFieldValue(metaObject , "createdBy", principal.getUserId());
+		}
+		this.setFieldValue(metaObject , "createdDate", new Date());
+		
+	}
+
+	@Override
+	public void updateFill(MetaObject metaObject) {
+		SignPrincipal principal = getPrincipal();
+		if(principal != null) {
+			this.setFieldValue(metaObject , "modifiedBy", principal.getUserId());
+		}
+		this.setFieldValue(metaObject , "modifiedDate", new Date());
+	}
+	
+	/**
+	 * 获取principal , 忽略异常情况
+	 * @return
+	 */
+	SignPrincipal getPrincipal() {
+		SignPrincipal principal = null;
+		try {
+			principal = AuthorizationUtils.getPrincipal();
+		}catch(Exception e) {
+			//
+		}
+		return principal;
+	}
+
+}