41 lines
892 B
Go
41 lines
892 B
Go
package models
|
|
|
|
import (
|
|
"crypto"
|
|
"encoding/base64"
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Account struct {
|
|
ID uint32 `gorm:"primary_key" json:"-"`
|
|
Login string `gorm:"unique;size:64;not null" json:"login"`
|
|
Password string `gorm:"-" json:"-"`
|
|
EncryptedPassword string `gorm:"size:44;not null" json:"-"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"-"`
|
|
}
|
|
|
|
func (a *Account) BeforeCreate(tx *gorm.DB) error {
|
|
if a.EncryptedPassword == "" {
|
|
a.EncryptPassword()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (a *Account) EncryptPassword() {
|
|
sha1 := crypto.SHA256.New()
|
|
sha1.Write([]byte(a.Password))
|
|
a.EncryptedPassword = base64.StdEncoding.EncodeToString(sha1.Sum(nil))
|
|
}
|
|
|
|
func NewAccount(login string, password string) *Account {
|
|
a := &Account{
|
|
Login: login,
|
|
Password: password,
|
|
}
|
|
a.EncryptPassword()
|
|
return a
|
|
}
|