You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
707 B
32 lines
707 B
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("缺少密码")
|
|
}
|
|
|