diff --git a/api/oauth_api/oauth.go b/api/oauth_api/oauth.go index 796c156..b4bb496 100644 --- a/api/oauth_api/oauth.go +++ b/api/oauth_api/oauth.go @@ -122,6 +122,7 @@ func HandelUserLogin(userId string) (bool, map[string]interface{}) { "data": data, "success": true, } + global.DB.Set("user_id", userId) return true, responseData } diff --git a/api/user_api/user_api.go b/api/user_api/user_api.go index 71012d8..4c1dce2 100644 --- a/api/user_api/user_api.go +++ b/api/user_api/user_api.go @@ -344,7 +344,7 @@ func handelUserLogin(user model.ScaAuthUser, autoLogin bool, c *gin.Context) { result.FailWithMessage(ginI18n.MustGetMessage(c, "LoginFailed"), c) return } - + global.DB.Set("user_id", user.UID) result.OkWithData(data, c) } diff --git a/model/sca_auth_permission.go b/model/sca_auth_permission.go index 68c171d..4982e34 100644 --- a/model/sca_auth_permission.go +++ b/model/sca_auth_permission.go @@ -2,6 +2,9 @@ package model import ( "encoding/json" + "fmt" + "gorm.io/gorm" + "schisandra-cloud-album/global" "time" ) @@ -39,3 +42,36 @@ func (permission *ScaAuthPermission) MarshalBinary() ([]byte, error) { func (permission *ScaAuthPermission) UnmarshalBinary(data []byte) error { return json.Unmarshal(data, permission) } +func (permission *ScaAuthPermission) BeforeCreate(tx *gorm.DB) (err error) { + userId, b := global.DB.Get("user_id") + if !b { + global.LOG.Error("user_id is not in global.DB") + return fmt.Errorf("user_id is not in global.DB") + } + + userIdStr, ok := userId.(*string) + if !ok { + global.LOG.Error("user_id is not of type *string") + return fmt.Errorf("user_id is not of type *string") + } + + permission.CreatedBy = userIdStr + return nil +} + +func (permission *ScaAuthPermission) BeforeUpdate(tx *gorm.DB) (err error) { + userId, b := global.DB.Get("user_id") + if !b { + global.LOG.Error("user_id is not in global.DB") + return fmt.Errorf("user_id is not in global.DB") + } + + userIdStr, ok := userId.(*string) + if !ok { + global.LOG.Error("user_id is not of type *string") + return fmt.Errorf("user_id is not of type *string") + } + + permission.UpdateBy = userIdStr + return nil +} diff --git a/model/sca_auth_role.go b/model/sca_auth_role.go index 9036216..60027d5 100644 --- a/model/sca_auth_role.go +++ b/model/sca_auth_role.go @@ -2,6 +2,9 @@ package model import ( "encoding/json" + "fmt" + "gorm.io/gorm" + "schisandra-cloud-album/global" "time" ) @@ -31,3 +34,39 @@ func (role *ScaAuthRole) MarshalBinary() ([]byte, error) { func (role *ScaAuthRole) UnmarshalBinary(data []byte) error { return json.Unmarshal(data, role) } + +func (role *ScaAuthRole) BeforeCreate(tx *gorm.DB) (err error) { + userId, b := global.DB.Get("user_id") + if !b { + creator := "system" + role.CreatedBy = &creator + return nil + } + + userIdStr, ok := userId.(*string) + if !ok { + global.LOG.Error("user_id is not of type *string") + return fmt.Errorf("user_id is not of type *string") + } + + role.CreatedBy = userIdStr + return nil +} + +func (role *ScaAuthRole) BeforeUpdate(tx *gorm.DB) (err error) { + userId, b := global.DB.Get("user_id") + if !b { + creator := "system" + role.CreatedBy = &creator + return nil + } + + userIdStr, ok := userId.(*string) + if !ok { + global.LOG.Error("user_id is not of type *string") + return fmt.Errorf("user_id is not of type *string") + } + + role.UpdateBy = userIdStr + return nil +} diff --git a/model/sca_auth_user.go b/model/sca_auth_user.go index 1ce7e5a..71ae7f1 100644 --- a/model/sca_auth_user.go +++ b/model/sca_auth_user.go @@ -2,6 +2,9 @@ package model import ( "encoding/json" + "fmt" + "gorm.io/gorm" + "schisandra-cloud-album/global" "time" ) @@ -42,3 +45,38 @@ func (user *ScaAuthUser) MarshalBinary() ([]byte, error) { func (user *ScaAuthUser) UnmarshalBinary(data []byte) error { return json.Unmarshal(data, user) } +func (user *ScaAuthUser) BeforeCreate(tx *gorm.DB) (err error) { + userId, b := global.DB.Get("user_id") + if !b { + creator := "system" + user.CreatedBy = &creator + return nil + } + + userIdStr, ok := userId.(*string) + if !ok { + global.LOG.Error("user_id is not of type *string") + return fmt.Errorf("user_id is not of type *string") + } + + user.CreatedBy = userIdStr + return nil +} + +func (user *ScaAuthUser) BeforeUpdate(tx *gorm.DB) (err error) { + userId, b := global.DB.Get("user_id") + if !b { + creator := "system" + user.CreatedBy = &creator + return nil + } + + userIdStr, ok := userId.(*string) + if !ok { + global.LOG.Error("user_id is not of type *string") + return fmt.Errorf("user_id is not of type *string") + } + + user.UpdateBy = userIdStr + return nil +} diff --git a/model/sca_auth_user_device.go b/model/sca_auth_user_device.go index 081a3dc..e91129a 100644 --- a/model/sca_auth_user_device.go +++ b/model/sca_auth_user_device.go @@ -1,6 +1,9 @@ package model import ( + "fmt" + "gorm.io/gorm" + "schisandra-cloud-album/global" "time" ) @@ -25,7 +28,7 @@ type ScaAuthUserDevice struct { Platform *string `gorm:"column:platform;type:varchar(20);comment:平台" json:"platform"` // 平台 EngineName *string `gorm:"column:engine_name;type:varchar(20);comment:引擎名称" json:"engine_name"` // 引擎名称 EngineVersion *string `gorm:"column:engine_version;type:varchar(20);comment:引擎版本" json:"engine_version"` // 引擎版本 - CreatedBy *string `gorm:"column:created_by;type:varchar(32);comment:创建人" json:"created_by"` // 创建人 + CreatedBy *string `gorm:"column:created_by;type:varchar(32);default:system;comment:创建人" json:"created_by"` // 创建人 UpdateBy *string `gorm:"column:update_by;type:varchar(32);comment:更新人" json:"update_by"` } @@ -33,3 +36,19 @@ type ScaAuthUserDevice struct { func (*ScaAuthUserDevice) TableName() string { return TableNameScaAuthUserDevice } + +func (device *ScaAuthUserDevice) BeforeUpdate(tx *gorm.DB) (err error) { + userId, b := global.DB.Get("user_id") + if !b { + global.LOG.Error("user_id is not found in global.DB") + return fmt.Errorf("user_id is not found in global.DB") + } + userIdStr, ok := userId.(*string) + if !ok { + global.LOG.Error("user_id is not of type *string") + return fmt.Errorf("user_id is not of type *string") + } + + device.UpdateBy = userIdStr + return nil +} diff --git a/model/sca_auth_user_social.go b/model/sca_auth_user_social.go index fef7b09..138122d 100644 --- a/model/sca_auth_user_social.go +++ b/model/sca_auth_user_social.go @@ -1,6 +1,9 @@ package model import ( + "fmt" + "gorm.io/gorm" + "schisandra-cloud-album/global" "time" ) @@ -16,7 +19,7 @@ type ScaAuthUserSocial struct { CreatedTime *time.Time `gorm:"column:created_time;type:datetime;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_time"` // 创建时间 UpdateTime *time.Time `gorm:"column:update_time;type:datetime;default:CURRENT_TIMESTAMP;comment:更新时间" json:"update_time"` // 更新时间 Deleted *int64 `gorm:"column:deleted;type:int(11);default:0;comment:是否删除" json:"deleted"` // 是否删除 - CreatedBy *string `gorm:"column:created_by;type:varchar(32);comment:创建人" json:"created_by"` // 创建人 + CreatedBy *string `gorm:"column:created_by;type:varchar(32);default:system;comment:创建人" json:"created_by"` // 创建人 UpdateBy *string `gorm:"column:update_by;type:varchar(32);comment:更新人" json:"update_by"` } @@ -24,3 +27,18 @@ type ScaAuthUserSocial struct { func (*ScaAuthUserSocial) TableName() string { return TableNameScaAuthUserSocial } + +func (social *ScaAuthUserSocial) BeforeUpdate(tx *gorm.DB) (err error) { + userId, b := global.DB.Get("user_id") + if !b { + global.LOG.Error("user_id is not found in global.DB") + return fmt.Errorf("user_id is not found in global.DB") + } + userIdStr, ok := userId.(*string) + if !ok { + global.LOG.Error("user_id is not of type *string") + return fmt.Errorf("user_id is not of type *string") + } + social.UpdateBy = userIdStr + return nil +}