Files
go-admin/common/models/user.go
T
zhangwenjian 8ffde94433 chore🔧: move to go-admin-core v2
Every import of the module changes, not only the seven packages that
moved out of sdk/pkg: Go requires the major version in the path from v2
on. Both happen in one pass —

    go run github.com/go-admin-team/go-admin-core/tools/coreupgrade@v2.0.0 -w -v2 .
    go mod tidy

— which is the command the release notes give, run here as a consumer
would run it. 210 imports across 95 files.

The compatibility shims this used are gone in v2, so the paths that
moved had to move: sdk/pkg/captcha, sdk/pkg/jwtauth and its user
package, sdk/pkg/response and sdk/pkg/casbin.

The count of unformatted files is unchanged at 34, none of them touched
by this: the tool reformats a file only if it was already gofmt clean,
so a migration cannot disappear into whitespace.
2026-08-23 13:26:46 +08:00

43 lines
1.1 KiB
Go

package models
import (
"gorm.io/gorm"
"github.com/go-admin-team/go-admin-core/v2/sdk/pkg"
)
// BaseUser 密码登录基础用户
type BaseUser struct {
Username string `json:"username" gorm:"type:varchar(100);comment:用户名"`
Salt string `json:"-" gorm:"type:varchar(255);comment:加盐;<-"`
PasswordHash string `json:"-" gorm:"type:varchar(128);comment:密码hash;<-"`
Password string `json:"password" gorm:"-"`
}
// SetPassword 设置密码
func (u *BaseUser) SetPassword(value string) {
u.Password = value
u.generateSalt()
u.PasswordHash = u.GetPasswordHash()
}
// GetPasswordHash 获取密码hash
func (u *BaseUser) GetPasswordHash() string {
passwordHash, err := pkg.SetPassword(u.Password, u.Salt)
if err != nil {
return ""
}
return passwordHash
}
// generateSalt 生成加盐值
func (u *BaseUser) generateSalt() {
u.Salt = pkg.GenerateRandomKey16()
}
// Verify 验证密码
func (u *BaseUser) Verify(db *gorm.DB, tableName string) bool {
db.Table(tableName).Where("username = ?", u.Username).First(u)
return u.GetPasswordHash() == u.PasswordHash
}