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

94 lines
2.6 KiB
Go
Raw Normal View History

2019-05-06 01:40:41 +03:00
package models
import "regexp"
const (
MailPattern = "[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+"
)
var (
mailRegex *regexp.Regexp
)
func init() {
mailRegex = regexp.MustCompile(MailPattern)
}
2019-05-06 01:40:41 +03:00
type User struct {
ID string `json:"id" gorm:"primary_key"`
ApiKey string `json:"api_key" gorm:"unique"`
Email string `json:"email"`
Password string `json:"-"`
2021-02-07 13:54:07 +03:00
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"`
2021-02-07 01:23:26 +03:00
ShareDataMaxDays int `json:"-" gorm:"default:0"`
ShareEditors bool `json:"-" gorm:"default:false; type:bool"`
ShareLanguages bool `json:"-" gorm:"default:false; type:bool"`
ShareProjects bool `json:"-" gorm:"default:false; type:bool"`
ShareOSs bool `json:"-" gorm:"default:false; type:bool; column:share_oss"`
ShareMachines bool `json:"-" gorm:"default:false; type:bool"`
2021-02-12 20:13:49 +03:00
IsAdmin bool `json:"-" gorm:"default:false; type:bool"`
HasData bool `json:"-" gorm:"default:false; type:bool"`
WakatimeApiKey string `json:"-"`
2019-05-06 01:40:41 +03:00
}
type Login struct {
2020-05-24 17:34:32 +03:00
Username string `schema:"username"`
Password string `schema:"password"`
}
type Signup struct {
Username string `schema:"username"`
Email string `schema:"email"`
2020-05-24 17:34:32 +03:00
Password string `schema:"password"`
PasswordRepeat string `schema:"password_repeat"`
}
2020-05-24 22:42:15 +03:00
type CredentialsReset struct {
PasswordOld string `schema:"password_old"`
PasswordNew string `schema:"password_new"`
PasswordRepeat string `schema:"password_repeat"`
}
type UserDataUpdate struct {
Email string `schema:"email"`
}
type TimeByUser struct {
User string
Time CustomTime
}
2021-02-13 13:23:58 +03:00
type CountByUser struct {
User string
Count int64
}
func (c *CredentialsReset) IsValid() bool {
return ValidatePassword(c.PasswordNew) &&
c.PasswordNew == c.PasswordRepeat
}
2020-05-24 22:42:15 +03:00
func (s *Signup) IsValid() bool {
return ValidateUsername(s.Username) &&
ValidateEmail(s.Email) &&
ValidatePassword(s.Password) &&
2020-05-24 22:42:15 +03:00
s.Password == s.PasswordRepeat
}
func (r *UserDataUpdate) IsValid() bool {
return ValidateEmail(r.Email)
}
func ValidateUsername(username string) bool {
2021-01-12 13:05:07 +03:00
return len(username) >= 1 && username != "current"
}
func ValidatePassword(password string) bool {
return len(password) >= 6
}
func ValidateEmail(email string) bool {
return email == "" || mailRegex.Match([]byte(email))
}