- 增加登录日志

- 增加数据库索引
- 修改原生安装文档
This commit is contained in:
dushixiang
2021-01-20 22:57:26 +08:00
parent 1d4653a561
commit f0157dbaeb
20 changed files with 700 additions and 78 deletions

View File

@ -15,14 +15,14 @@ type Asset struct {
AccountType string `json:"accountType"`
Username string `json:"username"`
Password string `json:"password"`
CredentialId string `json:"credentialId"`
CredentialId string `gorm:"index" json:"credentialId"`
PrivateKey string `json:"privateKey"`
Passphrase string `json:"passphrase"`
Description string `json:"description"`
Active bool `json:"active"`
Created utils.JsonTime `json:"created"`
Tags string `json:"tags"`
Owner string `json:"owner"`
Owner string `gorm:"index" json:"owner"`
}
type AssetVo struct {

View File

@ -10,7 +10,7 @@ type Command struct {
Name string `json:"name"`
Content string `json:"content"`
Created utils.JsonTime `json:"created"`
Owner string `json:"owner"`
Owner string `gorm:"index" json:"owner"`
}
type CommandVo struct {

View File

@ -20,7 +20,7 @@ type Credential struct {
PrivateKey string `json:"privateKey"`
Passphrase string `json:"passphrase"`
Created utils.JsonTime `json:"created"`
Owner string `json:"owner"`
Owner string `gorm:"index" json:"owner"`
}
func (r *Credential) TableName() string {

102
pkg/model/login-log.go Normal file
View File

@ -0,0 +1,102 @@
package model
import (
"github.com/sirupsen/logrus"
"next-terminal/pkg/global"
"next-terminal/pkg/utils"
)
type LoginLog struct {
ID string `gorm:"primary_key" json:"id"`
UserId string `gorm:"index" json:"userId"`
ClientIP string `json:"clientIp"`
ClientUserAgent string `json:"clientUserAgent"`
LoginTime utils.JsonTime `json:"loginTime"`
LogoutTime utils.JsonTime `json:"logoutTime"`
Remember bool `json:"remember"`
}
type LoginLogVo struct {
ID string `json:"id"`
UserId string `json:"userId"`
UserName string `json:"userName"`
ClientIP string `json:"clientIp"`
ClientUserAgent string `json:"clientUserAgent"`
LoginTime utils.JsonTime `json:"loginTime"`
LogoutTime utils.JsonTime `json:"logoutTime"`
Remember bool `json:"remember"`
}
func (r *LoginLog) TableName() string {
return "login_logs"
}
func FindPageLoginLog(pageIndex, pageSize int, userId, clientIp string) (o []LoginLogVo, total int64, err error) {
db := global.DB.Table("login_logs").Select("login_logs.id,login_logs.user_id,login_logs.client_ip,login_logs.client_user_agent,login_logs.login_time, login_logs.logout_time, users.nickname as user_name").Joins("left join users on login_logs.user_id = users.id")
dbCounter := global.DB.Table("login_logs").Select("DISTINCT login_logs.id")
if userId != "" {
db = db.Where("login_logs.user_id = ?", userId)
dbCounter = dbCounter.Where("login_logs.user_id = ?", userId)
}
if clientIp != "" {
db = db.Where("login_logs.client_ip like ?", "%"+clientIp+"%")
dbCounter = dbCounter.Where("login_logs.client_ip like ?", "%"+clientIp+"%")
}
err = dbCounter.Count(&total).Error
if err != nil {
return nil, 0, err
}
err = db.Order("login_logs.login_time desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&o).Error
if o == nil {
o = make([]LoginLogVo, 0)
}
return
}
func FindAliveLoginLogs() (o []LoginLog, err error) {
err = global.DB.Where("logout_time is null").Find(&o).Error
return
}
func FindAliveLoginLogsByUserId(userId string) (o []LoginLog, err error) {
err = global.DB.Where("logout_time is null and user_id = ?", userId).Find(&o).Error
return
}
func CreateNewLoginLog(o *LoginLog) (err error) {
return global.DB.Create(o).Error
}
func DeleteLoginLogByIdIn(ids []string) (err error) {
return global.DB.Where("id in ?", ids).Delete(&LoginLog{}).Error
}
func FindLoginLogById(id string) (o LoginLog, err error) {
err = global.DB.Where("id = ?", id).First(&o).Error
return
}
func Logout(id string) {
loginLog, err := FindLoginLogById(id)
if err != nil {
logrus.Warnf("登录日志「%v」获取失败", id)
return
}
global.DB.Table("login_logs").Where("id = ?", id).Update("logout_time", utils.NowJsonTime())
loginLogs, err := FindAliveLoginLogsByUserId(loginLog.UserId)
if err != nil {
return
}
if len(loginLogs) == 0 {
UpdateUserById(&User{Online: false}, loginLog.UserId)
}
}

View File

@ -8,10 +8,11 @@ import (
)
type ResourceSharer struct {
ID string `gorm:"primary_key" json:"name"`
ResourceId string `json:"resourceId"`
ResourceType string `json:"resourceType"`
UserId string `json:"userId"`
ID string `gorm:"primary_key" json:"id"`
ResourceId string `gorm:"index" json:"resourceId"`
ResourceType string `gorm:"index" json:"resourceType"`
UserId string `gorm:"index" json:"userId"`
UserGroupId string `gorm:"index" json:"userGroupId"`
}
func (r *ResourceSharer) TableName() string {

View File

@ -19,14 +19,14 @@ type Session struct {
IP string `json:"ip"`
Port int `json:"port"`
ConnectionId string `json:"connectionId"`
AssetId string `json:"assetId"`
AssetId string `gorm:"index" json:"assetId"`
Username string `json:"username"`
Password string `json:"password"`
Creator string `json:"creator"`
Creator string `gorm:"index" json:"creator"`
ClientIP string `json:"clientIp"`
Width int `json:"width"`
Height int `json:"height"`
Status string `json:"status"`
Status string `gorm:"index" json:"status"`
Recording string `json:"recording"`
PrivateKey string `json:"privateKey"`
Passphrase string `json:"passphrase"`

View File

@ -4,8 +4,8 @@ import "next-terminal/pkg/global"
type UserGroupMember struct {
ID string `gorm:"primary_key" json:"name"`
UserId string `json:"userId"`
UserGroupId string `json:"userGroupId"`
UserId string `gorm:"index" json:"userId"`
UserGroupId string `gorm:"index" json:"userGroupId"`
}
func (r *UserGroupMember) TableName() string {

View File

@ -13,7 +13,7 @@ type UserGroup struct {
}
type UserGroupVo struct {
ID string `gorm:"primary_key" json:"id"`
ID string `json:"id"`
Name string `json:"name"`
Created utils.JsonTime `json:"created"`
MemberCount int64 `json:"memberCount"`

View File

@ -13,7 +13,7 @@ const (
type User struct {
ID string `gorm:"primary_key" json:"id"`
Username string `json:"username"`
Username string `gorm:"index:unique" json:"username"`
Password string `json:"password"`
Nickname string `json:"nickname"`
TOTPSecret string `json:"-"`
@ -24,7 +24,7 @@ type User struct {
}
type UserVo struct {
ID string `gorm:"primary_key" json:"id"`
ID string `json:"id"`
Username string `json:"username"`
Nickname string `json:"nickname"`
Online bool `json:"online"`