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"
|
|
|
|
)
|
|
|
|
|
2024-02-29 02:20:05 +00:00
|
|
|
func init() {
|
|
|
|
registerModel(&User{})
|
|
|
|
}
|
|
|
|
|
2024-01-16 01:58:20 +00:00
|
|
|
type User struct {
|
|
|
|
gorm.Model
|
2024-02-26 12:47:05 +00:00
|
|
|
FirstName string
|
|
|
|
LastName string
|
|
|
|
Email string `gorm:"unique"`
|
|
|
|
PasswordHash string
|
|
|
|
Role string
|
|
|
|
PublicKeys []PublicKey
|
|
|
|
APIKeys []APIKey
|
|
|
|
Uploads []Upload
|
|
|
|
LastLogin *time.Time
|
|
|
|
LastLoginIP string
|
|
|
|
OTPEnabled bool `gorm:"default:false;"`
|
|
|
|
OTPVerified bool `gorm:"default:false;"`
|
|
|
|
OTPSecret string
|
|
|
|
OTPAuthUrl string
|
|
|
|
Verified bool `gorm:"default:false;"`
|
|
|
|
EmailVerifications []EmailVerification
|
|
|
|
PasswordResets []PasswordReset
|
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
|
|
|
|
}
|