Ejemplo n.º 1
0
func assignCreatedBy(scope *gorm.Scope) {
	if isAuditable(scope) {
		if user, ok := getCurrentUser(scope); ok {
			scope.SetColumn("CreatedBy", user)
		}
	}
}
Ejemplo n.º 2
0
func updateTimeStampWhenCreate(scope *gorm.Scope) {
	if !scope.HasError() {
		now := time.Now()
		scope.SetColumn("Created", now)
		scope.SetColumn("Updated", now)
	}
}
Ejemplo n.º 3
0
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
}
Ejemplo n.º 5
0
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)
			}
		}
	}
}
Ejemplo n.º 6
0
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
}
Ejemplo n.º 7
0
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)
		}
	}
}
Ejemplo n.º 8
0
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)
			}
		}
	}
}
Ejemplo n.º 9
0
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
}
Ejemplo n.º 10
0
// 顧客情報更新前処理
func (customer *Customer) BeforeUpdate(scope *gorm.Scope) error {
	// UpdatedAtの設定
	scope.SetColumn("UpdatedAt", time.Now())
	return nil
}
Ejemplo n.º 11
0
func (friend *Friends) BeforeUpdate(scope *gorm.Scope) (err error) {
	scope.SetColumn("updated_at", time.Now().Unix())
	return
}