2018-05-27 09:36:35 +00:00
|
|
|
package common
|
|
|
|
|
2019-07-26 22:26:52 +00:00
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
qgen "github.com/Azareal/Gosora/query_gen"
|
|
|
|
)
|
2018-05-27 09:36:35 +00:00
|
|
|
|
|
|
|
var Emails EmailStore
|
|
|
|
|
2019-10-30 06:41:05 +00:00
|
|
|
type Email struct {
|
|
|
|
UserID int
|
|
|
|
Email string
|
|
|
|
Validated bool
|
|
|
|
Primary bool
|
|
|
|
Token string
|
|
|
|
}
|
|
|
|
|
2018-05-27 09:36:35 +00:00
|
|
|
type EmailStore interface {
|
2019-10-30 06:41:05 +00:00
|
|
|
// TODO: Add an autoincrement key
|
|
|
|
Get(user *User, email string) (Email, error)
|
2018-05-27 09:36:35 +00:00
|
|
|
GetEmailsByUser(user *User) (emails []Email, err error)
|
2019-10-30 06:41:05 +00:00
|
|
|
Add(uid int, email, token string) error
|
|
|
|
Delete(uid int, email string) error
|
2018-05-27 09:36:35 +00:00
|
|
|
VerifyEmail(email string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type DefaultEmailStore struct {
|
2019-10-30 06:41:05 +00:00
|
|
|
get *sql.Stmt
|
2018-05-27 09:36:35 +00:00
|
|
|
getEmailsByUser *sql.Stmt
|
2019-10-30 06:41:05 +00:00
|
|
|
add *sql.Stmt
|
|
|
|
delete *sql.Stmt
|
2018-05-27 09:36:35 +00:00
|
|
|
verifyEmail *sql.Stmt
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefaultEmailStore(acc *qgen.Accumulator) (*DefaultEmailStore, error) {
|
2019-10-30 06:41:05 +00:00
|
|
|
e := "emails"
|
2018-05-27 09:36:35 +00:00
|
|
|
return &DefaultEmailStore{
|
2019-10-30 06:41:05 +00:00
|
|
|
get: acc.Select(e).Columns("email,validated,token").Where("uid=? AND email=?").Prepare(),
|
|
|
|
getEmailsByUser: acc.Select(e).Columns("email,validated,token").Where("uid=?").Prepare(),
|
|
|
|
add: acc.Insert(e).Columns("uid,email,validated,token").Fields("?,?,?,?").Prepare(),
|
|
|
|
delete: acc.Delete(e).Where("uid=? AND email=?").Prepare(),
|
2018-05-27 09:36:35 +00:00
|
|
|
|
|
|
|
// Need to fix this: Empty string isn't working, it gets set to 1 instead x.x -- Has this been fixed?
|
2019-10-30 06:41:05 +00:00
|
|
|
verifyEmail: acc.Update(e).Set("validated=1,token=''").Where("email=?").Prepare(),
|
2018-05-27 09:36:35 +00:00
|
|
|
}, acc.FirstError()
|
|
|
|
}
|
|
|
|
|
2019-10-30 06:41:05 +00:00
|
|
|
func (s *DefaultEmailStore) Get(user *User, email string) (Email, error) {
|
|
|
|
e := Email{UserID:user.ID, Primary:email !="" && user.Email==email}
|
|
|
|
err := s.get.QueryRow(user.ID, email).Scan(&e.Email, &e.Validated, &e.Token)
|
|
|
|
return e, err
|
|
|
|
}
|
|
|
|
|
2019-07-26 22:26:52 +00:00
|
|
|
func (s *DefaultEmailStore) GetEmailsByUser(user *User) (emails []Email, err error) {
|
2019-09-29 04:56:39 +00:00
|
|
|
e := Email{UserID: user.ID}
|
2019-07-26 22:26:52 +00:00
|
|
|
rows, err := s.getEmailsByUser.Query(user.ID)
|
2018-05-27 09:36:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return emails, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
2019-09-29 04:56:39 +00:00
|
|
|
err := rows.Scan(&e.Email, &e.Validated, &e.Token)
|
2018-05-27 09:36:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return emails, err
|
|
|
|
}
|
2019-09-29 04:56:39 +00:00
|
|
|
if e.Email == user.Email {
|
|
|
|
e.Primary = true
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
2019-09-29 04:56:39 +00:00
|
|
|
emails = append(emails, e)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
return emails, rows.Err()
|
|
|
|
}
|
|
|
|
|
2019-10-30 06:41:05 +00:00
|
|
|
func (s *DefaultEmailStore) Add(uid int, email string, token string) error {
|
|
|
|
_, err := s.add.Exec(uid, email, 0, token)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultEmailStore) Delete(uid int, email string) error {
|
|
|
|
_, err := s.delete.Exec(uid,email)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-26 22:26:52 +00:00
|
|
|
func (s *DefaultEmailStore) VerifyEmail(email string) error {
|
|
|
|
_, err := s.verifyEmail.Exec(email)
|
2018-05-27 09:36:35 +00:00
|
|
|
return err
|
|
|
|
}
|