nightingale/models/user_token.go

101 lines
2.0 KiB
Go
Raw Normal View History

2020-09-26 16:53:10 +08:00
package models
import (
"fmt"
"os"
"time"
"github.com/toolkits/pkg/logger"
2020-09-26 16:53:10 +08:00
"github.com/toolkits/pkg/str"
)
type UserToken struct {
UserId int64 `json:"user_id"`
Username string `json:"username"`
Token string `json:"token"`
}
func (UserToken) TableName() string {
return "user_token"
}
2020-09-26 16:53:10 +08:00
func UserTokenGet(where string, args ...interface{}) (*UserToken, error) {
var obj UserToken
has, err := DB.Where(where, args...).Get(&obj)
2020-09-26 16:53:10 +08:00
if err != nil {
logger.Errorf("mysql.error: query user_token fail: %v", err)
return nil, internalServerError
2020-09-26 16:53:10 +08:00
}
if !has {
return nil, nil
}
return &obj, nil
}
func UserTokenGets(where string, args ...interface{}) ([]UserToken, error) {
var objs []UserToken
err := DB.Where(where, args...).OrderBy("token").Find(&objs)
if err != nil {
logger.Errorf("mysql.error: list user_token fail: %v", err)
return objs, internalServerError
}
if objs == nil {
return []UserToken{}, nil
}
return objs, nil
2020-09-26 16:53:10 +08:00
}
func UserTokenNew(userId int64, username string) (*UserToken, error) {
items, err := UserTokenGets("user_id=?", userId)
if err != nil {
return nil, err
}
if len(items) >= 2 {
return nil, _e("Each user has at most two tokens")
2020-09-26 16:53:10 +08:00
}
obj := UserToken{
UserId: userId,
Username: username,
Token: genToken(userId),
}
err = DBInsertOne(obj)
2020-09-26 16:53:10 +08:00
if err != nil {
return nil, err
}
return &obj, nil
}
func UserTokenReset(userId int64, token string) (*UserToken, error) {
obj, err := UserTokenGet("token=? and user_id=?", token, userId)
if err != nil {
return nil, err
}
if obj == nil {
return nil, _e("No such token")
2020-09-26 16:53:10 +08:00
}
obj.Token = genToken(userId)
_, err = DB.Where("user_id=? and token=?", userId, token).Cols("token").Update(obj)
if err != nil {
logger.Errorf("mysql.error: update user_token fail: %v", err)
return nil, internalServerError
}
return obj, nil
2020-09-26 16:53:10 +08:00
}
func genToken(userId int64) string {
now := time.Now().UnixNano()
rls := str.RandLetters(6)
return str.MD5(fmt.Sprintf("%d%d%d%s", os.Getpid(), userId, now, rls))
}