This commit is contained in:
2025-12-15 17:55:32 +08:00
commit 28ab17324d
170 changed files with 18373 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
package models
import (
"context"
"quyun/v2/pkg/consts"
"github.com/samber/lo"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
)
func (m *User) ComparePassword(ctx context.Context, password string) bool {
err := bcrypt.CompareHashAndPassword([]byte(m.Password), []byte(password))
return err == nil
}
func (m *User) EncryptPassword(ctx context.Context) error {
bytes, err := bcrypt.GenerateFromPassword([]byte(m.Password), bcrypt.DefaultCost)
if err != nil {
return err
}
m.Password = string(bytes)
return nil
}
func (m *User) HasRole(ctx context.Context, role consts.Role) bool {
return lo.Contains(m.Roles, role)
}
// BeforeCreate
func (m *User) BeforeCreate(tx *gorm.DB) error {
return m.EncryptPassword(tx.Statement.Context)
}