package entities import ( "errors" "time" "gorm.io/gorm" "zestack.dev/misc" ) // User 用户 type User struct { ID uint `json:"id" gorm:"primaryKey"` Username string `json:"username" gorm:"unique"` RawPassword string `json:"-" gorm:"-"` // 原始密码 Password string `json:"password"` Disabled bool `json:"disabled" gorm:"default:false"` CreatedAt time.Time `json:"createdAt"` UpdatedAt time.Time `json:"updatedAt"` } func (u *User) BeforeCreate(_ *gorm.DB) error { if u.RawPassword != "" { hash, err := misc.PasswordHash(u.RawPassword) if err != nil { return err } u.Password = hash return nil } return errors.New("缺少密码") }