func assignCreatedBy(scope *gorm.Scope) { if isAuditable(scope) { if user, ok := getCurrentUser(scope); ok { scope.SetColumn("CreatedBy", user) } } }
func updateTimeStampWhenCreate(scope *gorm.Scope) { if !scope.HasError() { now := time.Now() scope.SetColumn("Created", now) scope.SetColumn("Updated", now) } }
func initalizePosition(scope *gorm.Scope) { if !scope.HasError() { if _, ok := scope.Value.(sortingInterface); ok { var lastPosition int scope.NewDB().Set("l10n:mode", "locale").Model(modelValue(scope.Value)).Select("position").Order("position DESC").Limit(1).Row().Scan(&lastPosition) scope.SetColumn("Position", lastPosition+1) } } }
func (user *User) setEncryptedPassword(scope *gorm.Scope) error { pw, err := bcrypt.GenerateFromPassword([]byte(user.Password), 0) if err != nil { return err } scope.SetColumn("EncryptedPassword", string(pw)) user.Password = "" return nil }
func assignUpdatedBy(scope *gorm.Scope) { if isAuditable(scope) { if user, ok := getCurrentUser(scope); ok { if attrs, ok := scope.InstanceGet("gorm:update_attrs"); ok { updateAttrs := attrs.(map[string]interface{}) updateAttrs["updated_by"] = user scope.InstanceSet("gorm:update_attrs", updateAttrs) } else { scope.SetColumn("UpdatedBy", user) } } } }
func (session *Session) BeforeCreate(scope *gorm.Scope) error { scope.SetColumn("Token", GenerateRandomString(64)) scope.SetColumn("UUID", generateUUID()) scope.SetColumn("Moment", time.Now().UTC().Unix()) scope.SetColumn("ExpiresIn", expirationLengthForTokenType(session.TokenType)) return nil }
func assignCreatedBy(scope *gorm.Scope) { if isAuditable(scope) { if user, ok := scope.DB().Get("audited:current_user"); ok { var currentUser string if primaryField := scope.New(user).PrimaryField(); primaryField != nil { currentUser = fmt.Sprintf("%v", primaryField.Field.Interface()) } else { currentUser = fmt.Sprintf("%v", user) } scope.SetColumn("CreatedBy", currentUser) } } }
func assignUpdatedBy(scope *gorm.Scope) { if isAuditable(scope) { if user, ok := scope.DB().Get("audited:current_user"); ok { var currentUser string if primaryField := scope.New(user).PrimaryField(); primaryField != nil { currentUser = fmt.Sprintf("%v", primaryField.Field.Interface()) } else { currentUser = fmt.Sprintf("%v", user) } if attrs, ok := scope.InstanceGet("gorm:update_attrs"); ok { updateAttrs := attrs.(map[string]interface{}) updateAttrs["updated_by"] = currentUser scope.InstanceSet("gorm:update_attrs", updateAttrs) } else { scope.SetColumn("UpdatedBy", currentUser) } } } }
func (client *Client) BeforeCreate(scope *gorm.Scope) error { scope.SetColumn("UUID", generateUUID()) scope.SetColumn("Key", GenerateRandomString(32)) if crypted, err := bcrypt.GenerateFromPassword([]byte(client.Secret), bcrypt.DefaultCost); err == nil { scope.SetColumn("Secret", crypted) } else { return err } return nil }
// 顧客情報更新前処理 func (customer *Customer) BeforeUpdate(scope *gorm.Scope) error { // UpdatedAtの設定 scope.SetColumn("UpdatedAt", time.Now()) return nil }
func (friend *Friends) BeforeUpdate(scope *gorm.Scope) (err error) { scope.SetColumn("updated_at", time.Now().Unix()) return }