feat: add login log record
This commit is contained in:
@@ -26,6 +26,15 @@ func CheckAuth(username, password string) (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func GetUserByUsername(username string) (*Auth, error) {
|
||||
var auth Auth
|
||||
err := db.Where("username = ?", username).First(&auth).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &auth, nil
|
||||
}
|
||||
|
||||
// EditUser 编辑用户信息
|
||||
func EditUser(username string, data map[string]interface{}) error {
|
||||
if err := db.Model(&Auth{}).Where("username = ? ", username).Updates(data).Error; err != nil {
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package models
|
||||
|
||||
import "message-nest/pkg/util"
|
||||
|
||||
type LoginLog struct {
|
||||
ID uint `gorm:"autoIncrement;type:integer;primaryKey" json:"id"`
|
||||
UserID int `json:"user_id" gorm:"type:int;index"`
|
||||
Username string `json:"username" gorm:"type:varchar(100);default:'';index"`
|
||||
IP string `json:"ip" gorm:"type:varchar(64);default:'';"`
|
||||
UA string `json:"ua" gorm:"type:varchar(512);default:'';"`
|
||||
CreatedAt util.Time `json:"created_on" gorm:"column:created_on;autoCreateTime"`
|
||||
}
|
||||
|
||||
func AddLoginLog(userID int, username string, ip string, ua string) error {
|
||||
log := LoginLog{UserID: userID, Username: username, IP: ip, UA: ua}
|
||||
return db.Create(&log).Error
|
||||
}
|
||||
|
||||
func GetRecentLoginLogs(limit int) ([]LoginLog, error) {
|
||||
if limit <= 0 {
|
||||
limit = 8
|
||||
}
|
||||
var logs []LoginLog
|
||||
err := db.Model(&LoginLog{}).Order("id DESC").Limit(limit).Find(&logs).Error
|
||||
return logs, err
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user