2024-01-16 01:58:20 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2024-02-14 00:36:23 +00:00
|
|
|
"errors"
|
2024-02-17 13:45:06 +00:00
|
|
|
"time"
|
|
|
|
|
2024-02-14 00:41:00 +00:00
|
|
|
emailverifier "github.com/AfterShip/email-verifier"
|
2024-01-16 01:58:20 +00:00
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
gorm.Model
|
2024-02-14 00:06:04 +00:00
|
|
|
FirstName string
|
|
|
|
LastName string
|
2024-01-16 01:58:20 +00:00
|
|
|
Email string `gorm:"unique"`
|
|
|
|
PasswordHash string
|
|
|
|
Role string
|
|
|
|
PublicKeys []PublicKey
|
|
|
|
APIKeys []APIKey
|
|
|
|
Uploads []Upload
|
|
|
|
LastLogin *time.Time
|
|
|
|
LastLoginIP string
|
2024-02-14 04:21:53 +00:00
|
|
|
OTPEnabled bool `gorm:"default:false;"`
|
|
|
|
OTPVerified bool `gorm:"default:false;"`
|
|
|
|
OTPSecret string
|
|
|
|
OTPAuthUrl string
|
2024-01-16 01:58:20 +00:00
|
|
|
}
|
2024-02-14 00:36:23 +00:00
|
|
|
|
|
|
|
func (u *User) BeforeUpdate(tx *gorm.DB) error {
|
2024-02-17 14:00:44 +00:00
|
|
|
if tx.Statement.Changed("Email") {
|
|
|
|
verify, err := getEmailVerfier().Verify(u.Email)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !verify.Syntax.Valid {
|
|
|
|
return errors.New("email is invalid")
|
|
|
|
}
|
2024-02-14 00:41:00 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 00:36:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-02-14 00:41:00 +00:00
|
|
|
|
|
|
|
func getEmailVerfier() *emailverifier.Verifier {
|
|
|
|
verifier := emailverifier.NewVerifier()
|
|
|
|
|
|
|
|
verifier.DisableSMTPCheck()
|
|
|
|
verifier.DisableGravatarCheck()
|
|
|
|
verifier.DisableDomainSuggest()
|
|
|
|
verifier.DisableAutoUpdateDisposable()
|
|
|
|
|
|
|
|
return verifier
|
|
|
|
}
|