修复 「1.2.2 用户管理-用户列表勾选单一用户会全选 」 close #216

This commit is contained in:
dushixiang
2022-01-23 17:53:22 +08:00
parent 29c066ca3a
commit d35b348a33
130 changed files with 5467 additions and 4554 deletions

View File

@ -1,7 +1,7 @@
package repository
import (
"encoding/base64"
"context"
"os"
"path"
"time"
@ -9,23 +9,15 @@ import (
"next-terminal/server/config"
"next-terminal/server/constant"
"next-terminal/server/model"
"next-terminal/server/utils"
"gorm.io/gorm"
)
type SessionRepository struct {
DB *gorm.DB
type sessionRepository struct {
baseRepository
}
func NewSessionRepository(db *gorm.DB) *SessionRepository {
sessionRepository = &SessionRepository{DB: db}
return sessionRepository
}
func (r sessionRepository) Find(c context.Context, pageIndex, pageSize int, status, userId, clientIp, assetId, protocol, reviewed string) (results []model.SessionForPage, total int64, err error) {
func (r SessionRepository) Find(pageIndex, pageSize int, status, userId, clientIp, assetId, protocol, reviewed string) (results []model.SessionForPage, total int64, err error) {
db := r.DB
db := r.GetDB(c)
var params []interface{}
params = append(params, status)
@ -77,152 +69,102 @@ func (r SessionRepository) Find(pageIndex, pageSize int, status, userId, clientI
return
}
func (r SessionRepository) FindByStatus(status string) (o []model.Session, err error) {
err = r.DB.Where("status = ?", status).Find(&o).Error
func (r sessionRepository) FindByStatus(c context.Context, status string) (o []model.Session, err error) {
err = r.GetDB(c).Where("status = ?", status).Find(&o).Error
return
}
func (r SessionRepository) FindByStatusIn(statuses []string) (o []model.Session, err error) {
err = r.DB.Where("status in ?", statuses).Find(&o).Error
func (r sessionRepository) FindByStatusIn(c context.Context, statuses []string) (o []model.Session, err error) {
err = r.GetDB(c).Where("status in ?", statuses).Find(&o).Error
return
}
func (r SessionRepository) FindOutTimeSessions(dayLimit int) (o []model.Session, err error) {
func (r sessionRepository) FindOutTimeSessions(c context.Context, dayLimit int) (o []model.Session, err error) {
limitTime := time.Now().Add(time.Duration(-dayLimit*24) * time.Hour)
err = r.DB.Where("status = ? and connected_time < ?", constant.Disconnected, limitTime).Find(&o).Error
err = r.GetDB(c).Where("status = ? and connected_time < ?", constant.Disconnected, limitTime).Find(&o).Error
return
}
func (r SessionRepository) Create(o *model.Session) (err error) {
err = r.DB.Create(o).Error
func (r sessionRepository) Create(c context.Context, o *model.Session) (err error) {
err = r.GetDB(c).Create(o).Error
return
}
func (r SessionRepository) FindById(id string) (o model.Session, err error) {
err = r.DB.Where("id = ?", id).First(&o).Error
func (r sessionRepository) FindById(c context.Context, id string) (o model.Session, err error) {
err = r.GetDB(c).Where("id = ?", id).First(&o).Error
return
}
func (r SessionRepository) FindByIdAndDecrypt(id string) (o model.Session, err error) {
err = r.DB.Where("id = ?", id).First(&o).Error
if err == nil {
err = r.Decrypt(&o)
}
func (r sessionRepository) FindByConnectionId(c context.Context, connectionId string) (o model.Session, err error) {
err = r.GetDB(c).Where("connection_id = ?", connectionId).First(&o).Error
return
}
func (r SessionRepository) Decrypt(item *model.Session) error {
if item.Password != "" && item.Password != "-" {
origData, err := base64.StdEncoding.DecodeString(item.Password)
if err != nil {
return err
}
decryptedCBC, err := utils.AesDecryptCBC(origData, config.GlobalCfg.EncryptionPassword)
if err != nil {
return err
}
item.Password = string(decryptedCBC)
}
if item.PrivateKey != "" && item.PrivateKey != "-" {
origData, err := base64.StdEncoding.DecodeString(item.PrivateKey)
if err != nil {
return err
}
decryptedCBC, err := utils.AesDecryptCBC(origData, config.GlobalCfg.EncryptionPassword)
if err != nil {
return err
}
item.PrivateKey = string(decryptedCBC)
}
if item.Passphrase != "" && item.Passphrase != "-" {
origData, err := base64.StdEncoding.DecodeString(item.Passphrase)
if err != nil {
return err
}
decryptedCBC, err := utils.AesDecryptCBC(origData, config.GlobalCfg.EncryptionPassword)
if err != nil {
return err
}
item.Passphrase = string(decryptedCBC)
}
return nil
}
func (r SessionRepository) FindByConnectionId(connectionId string) (o model.Session, err error) {
err = r.DB.Where("connection_id = ?", connectionId).First(&o).Error
return
}
func (r SessionRepository) UpdateById(o *model.Session, id string) error {
func (r sessionRepository) UpdateById(c context.Context, o *model.Session, id string) error {
o.ID = id
return r.DB.Updates(o).Error
return r.GetDB(c).Updates(o).Error
}
func (r SessionRepository) UpdateWindowSizeById(width, height int, id string) error {
func (r sessionRepository) UpdateWindowSizeById(c context.Context, width, height int, id string) error {
session := model.Session{}
session.Width = width
session.Height = height
return r.UpdateById(&session, id)
return r.UpdateById(c, &session, id)
}
func (r SessionRepository) DeleteById(id string) error {
return r.DB.Where("id = ?", id).Delete(&model.Session{}).Error
func (r sessionRepository) DeleteById(c context.Context, id string) error {
return r.GetDB(c).Where("id = ?", id).Delete(&model.Session{}).Error
}
func (r SessionRepository) DeleteByIds(sessionIds []string) error {
func (r sessionRepository) DeleteByIds(c context.Context, sessionIds []string) error {
recordingPath := config.GlobalCfg.Guacd.Recording
for i := range sessionIds {
if err := os.RemoveAll(path.Join(recordingPath, sessionIds[i])); err != nil {
return err
}
if err := r.DeleteById(sessionIds[i]); err != nil {
if err := r.DeleteById(c, sessionIds[i]); err != nil {
return err
}
}
return nil
}
func (r SessionRepository) DeleteByStatus(status string) error {
return r.DB.Where("status = ?", status).Delete(&model.Session{}).Error
func (r sessionRepository) DeleteByStatus(c context.Context, status string) error {
return r.GetDB(c).Where("status = ?", status).Delete(&model.Session{}).Error
}
func (r SessionRepository) CountOnlineSession() (total int64, err error) {
err = r.DB.Where("status = ?", constant.Connected).Find(&model.Session{}).Count(&total).Error
func (r sessionRepository) CountOnlineSession(c context.Context) (total int64, err error) {
err = r.GetDB(c).Where("status = ?", constant.Connected).Find(&model.Session{}).Count(&total).Error
return
}
func (r SessionRepository) EmptyPassword() error {
func (r sessionRepository) EmptyPassword(c context.Context) error {
sql := "update sessions set password = '-',private_key = '-', passphrase = '-' where 1=1"
return r.DB.Exec(sql).Error
return r.GetDB(c).Exec(sql).Error
}
func (r SessionRepository) CountByStatus(status string) (total int64, err error) {
err = r.DB.Find(&model.Session{}).Where("status = ?", status).Count(&total).Error
func (r sessionRepository) CountByStatus(c context.Context, status string) (total int64, err error) {
err = r.GetDB(c).Find(&model.Session{}).Where("status = ?", status).Count(&total).Error
return
}
func (r SessionRepository) OverviewAccess(account model.User) (o []model.SessionForAccess, err error) {
db := r.DB
if constant.TypeUser == account.Type {
sql := "SELECT s.asset_id, s.ip, s.port, s.protocol, s.username, count(s.asset_id) AS access_count FROM sessions AS s where s.creator = ? GROUP BY s.asset_id, s.ip, s.port, s.protocol, s.username ORDER BY access_count DESC limit 10"
err = db.Raw(sql, []string{account.ID}).Scan(&o).Error
} else {
sql := "SELECT s.asset_id, s.ip, s.port, s.protocol, s.username, count(s.asset_id) AS access_count FROM sessions AS s GROUP BY s.asset_id, s.ip, s.port, s.protocol, s.username ORDER BY access_count DESC limit 10"
err = db.Raw(sql).Scan(&o).Error
}
func (r sessionRepository) OverviewAccess(c context.Context) (o []model.SessionForAccess, err error) {
db := r.GetDB(c)
sql := "SELECT s.asset_id, s.ip, s.port, s.protocol, s.username, count(s.asset_id) AS access_count FROM sessions AS s GROUP BY s.asset_id, s.ip, s.port, s.protocol, s.username ORDER BY access_count DESC limit 10"
err = db.Raw(sql).Scan(&o).Error
if o == nil {
o = make([]model.SessionForAccess, 0)
}
return
}
func (r SessionRepository) UpdateReadByIds(reviewed bool, ids []string) error {
func (r sessionRepository) UpdateReadByIds(c context.Context, reviewed bool, ids []string) error {
sql := "update sessions set reviewed = ? where id in ?"
return r.DB.Exec(sql, reviewed, ids).Error
return r.GetDB(c).Exec(sql, reviewed, ids).Error
}
func (r SessionRepository) FindAllUnReviewed() (o []model.Session, err error) {
err = r.DB.Where("reviewed = false or reviewed is null").Find(&o).Error
func (r sessionRepository) FindAllUnReviewed(c context.Context) (o []model.Session, err error) {
err = r.GetDB(c).Where("reviewed = false or reviewed is null").Find(&o).Error
return
}