package models import ( "time" "golang.org/x/crypto/bcrypt" "gorm.io/gorm" ) // Account status constants. const ( StatusDisabled = 0 // 禁用 StatusNormal = 1 // 正常 StatusLocked = 2 // 锁定 StatusUnactivated = 3 // 未激活 ) // Role constants. const ( RoleAdmin = "admin" RoleAuthor = "author" ) // User represents a blog user (author / admin). type User struct { gorm.Model Username string `gorm:"uniqueIndex;not null;size:255" json:"username"` Password string `gorm:"not null" json:"-"` DisplayName string `gorm:"size:255" json:"display_name"` Avatar string `gorm:"size:512" json:"avatar"` Birthday *time.Time `json:"birthday"` Gender string `gorm:"size:16" json:"gender"` Email string `gorm:"size:255" json:"email"` Status int `gorm:"default:1" json:"status"` Role string `gorm:"size:32;default:author" json:"role"` } // SetPassword hashes the plain-text password with bcrypt and stores it. func (u *User) SetPassword(plain string) error { hash, err := bcrypt.GenerateFromPassword([]byte(plain), bcrypt.DefaultCost) if err != nil { return err } u.Password = string(hash) return nil } // CheckPassword compares a plain-text password against the stored bcrypt hash. func (u *User) CheckPassword(plain string) bool { err := bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(plain)) return err == nil }