recovery
This commit is contained in:
parent
b042b4dbc9
commit
173612b58d
36
emails.go
36
emails.go
|
@ -3,7 +3,6 @@ package scsusers
|
|||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"database/sql"
|
||||
"encoding/base32"
|
||||
"fmt"
|
||||
"log"
|
||||
|
@ -12,34 +11,17 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
func RecoverByUsername(username string) {
|
||||
var email string
|
||||
username = strings.ToLower(username)
|
||||
|
||||
q := fmt.Sprintf("select email from %s_auth where username = ?", c.TablePrefix)
|
||||
err := c.db.Get(&email, q, username)
|
||||
if err != sql.ErrNoRows {
|
||||
func RecoverByEmail(email string) {
|
||||
u, ok := Get(email)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
recoverycode := randBytes(16)
|
||||
qq := fmt.Sprintf("update %s_auth set recoverycode=?, recoverytime=NOW() where username = ?", c.TablePrefix)
|
||||
_, err := c.db.Exec(qq, recoverycode, username)
|
||||
if err == nil {
|
||||
SendRecoveryEmail(email, username, string(recoverycode))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func RecoverByEmail(e string) {
|
||||
var username, email string
|
||||
q := fmt.Sprintf("select username from %s_auth where email = ?", c.TablePrefix)
|
||||
err := c.db.Get(&username, q, e)
|
||||
if err != sql.ErrNoRows {
|
||||
recoverycode := randBytes(16)
|
||||
qq := fmt.Sprintf("update %s_auth set recoverycode=?, recoverytime=NOW() where username = ?", c.TablePrefix)
|
||||
_, err := c.db.Exec(qq, recoverycode, username)
|
||||
if err == nil {
|
||||
SendRecoveryEmail(email, username, string(recoverycode))
|
||||
}
|
||||
}
|
||||
u.Set("recoverycode", string(recoverycode))
|
||||
u.Set("recoverytime", fmt.Sprintf("%d", time.Now().Unix()))
|
||||
|
||||
SendRecoveryEmail(email, email, string(recoverycode))
|
||||
}
|
||||
|
||||
func randBytes(n int) []byte {
|
||||
|
|
Loading…
Reference in New Issue