1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

refactor: time zone sensitivity (resolve #184)

This commit is contained in:
Ferdinand Mütsch
2021-04-25 14:15:18 +02:00
parent 26ef93c1af
commit c142b525a4
15 changed files with 594 additions and 105 deletions

View File

@ -1,6 +1,9 @@
package models
import "regexp"
import (
"regexp"
"time"
)
func init() {
mailRegex = regexp.MustCompile(MailPattern)
@ -10,6 +13,7 @@ type User struct {
ID string `json:"id" gorm:"primary_key"`
ApiKey string `json:"api_key" gorm:"unique"`
Email string `json:"email" gorm:"index:idx_user_email; size:255"`
Location string `json:"location"`
Password string `json:"-"`
CreatedAt CustomTime `gorm:"type:timestamp; default:CURRENT_TIMESTAMP" swaggertype:"string" format:"date" example:"2006-01-02 15:04:05.000"`
LastLoggedInAt CustomTime `gorm:"type:timestamp; default:CURRENT_TIMESTAMP" swaggertype:"string" format:"date" example:"2006-01-02 15:04:05.000"`
@ -54,7 +58,8 @@ type CredentialsReset struct {
}
type UserDataUpdate struct {
Email string `schema:"email"`
Email string `schema:"email"`
Location string `schema:"location"`
}
type TimeByUser struct {
@ -67,6 +72,22 @@ type CountByUser struct {
Count int64
}
func (u *User) TZ() *time.Location {
if u.Location == "" {
u.Location = "Local"
}
tz, err := time.LoadLocation(u.Location)
if err != nil {
return time.Local
}
return tz
}
func (u *User) TZOffset() time.Duration {
_, offset := time.Now().In(u.TZ()).Zone()
return time.Duration(offset * int(time.Second))
}
func (c *CredentialsReset) IsValid() bool {
return ValidatePassword(c.PasswordNew) &&
c.PasswordNew == c.PasswordRepeat
@ -85,7 +106,7 @@ func (s *Signup) IsValid() bool {
}
func (r *UserDataUpdate) IsValid() bool {
return ValidateEmail(r.Email)
return ValidateEmail(r.Email) && ValidateTimezone(r.Location)
}
func ValidateUsername(username string) bool {
@ -99,3 +120,8 @@ func ValidatePassword(password string) bool {
func ValidateEmail(email string) bool {
return email == "" || mailRegex.Match([]byte(email))
}
func ValidateTimezone(tz string) bool {
_, err := time.LoadLocation(tz)
return err == nil
}