2018-05-16 10:46:14 +00:00
|
|
|
package common
|
|
|
|
|
2018-12-14 04:08:53 +00:00
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"time"
|
|
|
|
|
2020-01-04 05:30:25 +00:00
|
|
|
qgen "github.com/Azareal/Gosora/query_gen"
|
2018-12-14 04:08:53 +00:00
|
|
|
)
|
2018-05-16 10:46:14 +00:00
|
|
|
|
|
|
|
var RegLogs RegLogStore
|
2018-12-17 04:58:55 +00:00
|
|
|
var LoginLogs LoginLogStore
|
2018-05-16 10:46:14 +00:00
|
|
|
|
|
|
|
type RegLogItem struct {
|
|
|
|
ID int
|
|
|
|
Username string
|
|
|
|
Email string
|
|
|
|
FailureReason string
|
|
|
|
Success bool
|
2020-01-04 05:30:25 +00:00
|
|
|
IP string
|
2018-05-16 10:46:14 +00:00
|
|
|
DoneAt string
|
|
|
|
}
|
|
|
|
|
|
|
|
type RegLogStmts struct {
|
|
|
|
update *sql.Stmt
|
|
|
|
create *sql.Stmt
|
|
|
|
}
|
|
|
|
|
|
|
|
var regLogStmts RegLogStmts
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
DbInits.Add(func(acc *qgen.Accumulator) error {
|
2019-10-27 23:53:16 +00:00
|
|
|
rl := "registration_logs"
|
2018-05-16 10:46:14 +00:00
|
|
|
regLogStmts = RegLogStmts{
|
2019-10-27 23:53:16 +00:00
|
|
|
update: acc.Update(rl).Set("username = ?, email = ?, failureReason = ?, success = ?").Where("rlid = ?").Prepare(),
|
|
|
|
create: acc.Insert(rl).Columns("username, email, failureReason, success, ipaddress, doneAt").Fields("?,?,?,?,?,UTC_TIMESTAMP()").Prepare(),
|
2018-05-16 10:46:14 +00:00
|
|
|
}
|
|
|
|
return acc.FirstError()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Reload this item in the store, probably doesn't matter right now, but it might when we start caching this stuff in memory
|
|
|
|
// ! Retroactive updates of date are not permitted for integrity reasons
|
2019-08-31 22:34:43 +00:00
|
|
|
func (l *RegLogItem) Commit() error {
|
|
|
|
_, err := regLogStmts.update.Exec(l.Username, l.Email, l.FailureReason, l.Success, l.ID)
|
2018-05-16 10:46:14 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-31 22:34:43 +00:00
|
|
|
func (l *RegLogItem) Create() (id int, err error) {
|
|
|
|
res, err := regLogStmts.create.Exec(l.Username, l.Email, l.FailureReason, l.Success, l.IP)
|
2018-05-16 10:46:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
id64, err := res.LastInsertId()
|
2019-08-31 22:34:43 +00:00
|
|
|
l.ID = int(id64)
|
|
|
|
return l.ID, err
|
2018-05-16 10:46:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RegLogStore interface {
|
2019-06-01 12:31:48 +00:00
|
|
|
Count() (count int)
|
2018-05-16 10:46:14 +00:00
|
|
|
GetOffset(offset int, perPage int) (logs []RegLogItem, err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type SQLRegLogStore struct {
|
|
|
|
count *sql.Stmt
|
|
|
|
getOffset *sql.Stmt
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRegLogStore(acc *qgen.Accumulator) (*SQLRegLogStore, error) {
|
2019-10-27 23:53:16 +00:00
|
|
|
rl := "registration_logs"
|
2018-05-16 10:46:14 +00:00
|
|
|
return &SQLRegLogStore{
|
2019-10-27 23:53:16 +00:00
|
|
|
count: acc.Count(rl).Prepare(),
|
|
|
|
getOffset: acc.Select(rl).Columns("rlid, username, email, failureReason, success, ipaddress, doneAt").Orderby("doneAt DESC").Limit("?,?").Prepare(),
|
2018-05-16 10:46:14 +00:00
|
|
|
}, acc.FirstError()
|
|
|
|
}
|
|
|
|
|
2019-06-01 12:31:48 +00:00
|
|
|
func (s *SQLRegLogStore) Count() (count int) {
|
|
|
|
err := s.count.QueryRow().Scan(&count)
|
2018-05-16 10:46:14 +00:00
|
|
|
if err != nil {
|
|
|
|
LogError(err)
|
|
|
|
}
|
2019-06-01 12:31:48 +00:00
|
|
|
return count
|
2018-05-16 10:46:14 +00:00
|
|
|
}
|
|
|
|
|
2020-01-04 05:30:25 +00:00
|
|
|
func (s *SQLRegLogStore) GetOffset(offset, perPage int) (logs []RegLogItem, err error) {
|
2019-08-31 22:34:43 +00:00
|
|
|
rows, err := s.getOffset.Query(offset, perPage)
|
2018-05-16 10:46:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return logs, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
for rows.Next() {
|
2019-08-31 22:34:43 +00:00
|
|
|
var l RegLogItem
|
2018-12-14 04:08:53 +00:00
|
|
|
var doneAt time.Time
|
2019-08-31 22:34:43 +00:00
|
|
|
err := rows.Scan(&l.ID, &l.Username, &l.Email, &l.FailureReason, &l.Success, &l.IP, &doneAt)
|
2018-05-16 10:46:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return logs, err
|
|
|
|
}
|
2019-08-31 22:34:43 +00:00
|
|
|
l.DoneAt = doneAt.Format("2006-01-02 15:04:05")
|
|
|
|
logs = append(logs, l)
|
2018-05-16 10:46:14 +00:00
|
|
|
}
|
|
|
|
return logs, rows.Err()
|
|
|
|
}
|
2018-12-17 04:58:55 +00:00
|
|
|
|
|
|
|
type LoginLogItem struct {
|
2020-01-04 05:30:25 +00:00
|
|
|
ID int
|
|
|
|
UID int
|
|
|
|
Success bool
|
|
|
|
IP string
|
|
|
|
DoneAt string
|
2018-12-17 04:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type LoginLogStmts struct {
|
|
|
|
update *sql.Stmt
|
|
|
|
create *sql.Stmt
|
|
|
|
}
|
|
|
|
|
|
|
|
var loginLogStmts LoginLogStmts
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
DbInits.Add(func(acc *qgen.Accumulator) error {
|
2019-10-27 23:53:16 +00:00
|
|
|
ll := "login_logs"
|
2018-12-17 04:58:55 +00:00
|
|
|
loginLogStmts = LoginLogStmts{
|
2019-10-27 23:53:16 +00:00
|
|
|
update: acc.Update(ll).Set("uid = ?, success = ?").Where("lid = ?").Prepare(),
|
|
|
|
create: acc.Insert(ll).Columns("uid, success, ipaddress, doneAt").Fields("?,?,?,UTC_TIMESTAMP()").Prepare(),
|
2018-12-17 04:58:55 +00:00
|
|
|
}
|
|
|
|
return acc.FirstError()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Reload this item in the store, probably doesn't matter right now, but it might when we start caching this stuff in memory
|
|
|
|
// ! Retroactive updates of date are not permitted for integrity reasons
|
2019-08-31 22:34:43 +00:00
|
|
|
func (l *LoginLogItem) Commit() error {
|
|
|
|
_, err := loginLogStmts.update.Exec(l.UID, l.Success, l.ID)
|
2018-12-17 04:58:55 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-31 22:34:43 +00:00
|
|
|
func (l *LoginLogItem) Create() (id int, err error) {
|
|
|
|
res, err := loginLogStmts.create.Exec(l.UID, l.Success, l.IP)
|
2018-12-17 04:58:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
id64, err := res.LastInsertId()
|
2019-08-31 22:34:43 +00:00
|
|
|
l.ID = int(id64)
|
|
|
|
return l.ID, err
|
2018-12-17 04:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type LoginLogStore interface {
|
2019-06-01 12:31:48 +00:00
|
|
|
Count() (count int)
|
|
|
|
CountUser(uid int) (count int)
|
2018-12-17 04:58:55 +00:00
|
|
|
GetOffset(uid int, offset int, perPage int) (logs []LoginLogItem, err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type SQLLoginLogStore struct {
|
|
|
|
count *sql.Stmt
|
2019-02-24 01:29:06 +00:00
|
|
|
countForUser *sql.Stmt
|
2018-12-17 04:58:55 +00:00
|
|
|
getOffsetByUser *sql.Stmt
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLoginLogStore(acc *qgen.Accumulator) (*SQLLoginLogStore, error) {
|
2019-10-27 23:53:16 +00:00
|
|
|
ll := "login_logs"
|
2018-12-17 04:58:55 +00:00
|
|
|
return &SQLLoginLogStore{
|
2019-10-27 23:53:16 +00:00
|
|
|
count: acc.Count(ll).Prepare(),
|
|
|
|
countForUser: acc.Count(ll).Where("uid = ?").Prepare(),
|
|
|
|
getOffsetByUser: acc.Select(ll).Columns("lid, success, ipaddress, doneAt").Where("uid = ?").Orderby("doneAt DESC").Limit("?,?").Prepare(),
|
2018-12-17 04:58:55 +00:00
|
|
|
}, acc.FirstError()
|
|
|
|
}
|
|
|
|
|
2019-06-01 12:31:48 +00:00
|
|
|
func (s *SQLLoginLogStore) Count() (count int) {
|
|
|
|
err := s.count.QueryRow().Scan(&count)
|
2018-12-17 04:58:55 +00:00
|
|
|
if err != nil {
|
|
|
|
LogError(err)
|
|
|
|
}
|
2019-06-01 12:31:48 +00:00
|
|
|
return count
|
2018-12-17 04:58:55 +00:00
|
|
|
}
|
|
|
|
|
2019-06-01 12:31:48 +00:00
|
|
|
func (s *SQLLoginLogStore) CountUser(uid int) (count int) {
|
|
|
|
err := s.countForUser.QueryRow(uid).Scan(&count)
|
2019-02-24 01:29:06 +00:00
|
|
|
if err != nil {
|
|
|
|
LogError(err)
|
|
|
|
}
|
2019-06-01 12:31:48 +00:00
|
|
|
return count
|
2019-02-24 01:29:06 +00:00
|
|
|
}
|
|
|
|
|
2019-08-31 22:34:43 +00:00
|
|
|
func (s *SQLLoginLogStore) GetOffset(uid int, offset int, perPage int) (logs []LoginLogItem, err error) {
|
|
|
|
rows, err := s.getOffsetByUser.Query(uid, offset, perPage)
|
2018-12-17 04:58:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return logs, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
for rows.Next() {
|
2019-08-31 22:34:43 +00:00
|
|
|
l := LoginLogItem{UID: uid}
|
2018-12-17 04:58:55 +00:00
|
|
|
var doneAt time.Time
|
2019-08-31 22:34:43 +00:00
|
|
|
err := rows.Scan(&l.ID, &l.Success, &l.IP, &doneAt)
|
2018-12-17 04:58:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return logs, err
|
|
|
|
}
|
2019-08-31 22:34:43 +00:00
|
|
|
l.DoneAt = doneAt.Format("2006-01-02 15:04:05")
|
|
|
|
logs = append(logs, l)
|
2018-12-17 04:58:55 +00:00
|
|
|
}
|
|
|
|
return logs, rows.Err()
|
|
|
|
}
|