feat: add save

This commit is contained in:
2025-12-16 14:26:31 +08:00
parent 4722eef72c
commit 512859b5c7
6 changed files with 34 additions and 20 deletions

View File

@@ -15,20 +15,17 @@ func (m *User) ComparePassword(ctx context.Context, password string) bool {
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)
bytes, err := bcrypt.GenerateFromPassword([]byte(m.Password), bcrypt.DefaultCost)
if err != nil {
return err
}
m.Password = string(bytes)
return nil
}