mirror of
https://github.com/lejianwen/rustdesk-api.git
synced 2025-12-02 18:13:15 +00:00
feat(admin): Add My Login log
This commit is contained in:
@@ -169,7 +169,7 @@ func InitGlobal() {
|
||||
global.Lock = lock.NewLocal()
|
||||
}
|
||||
func DatabaseAutoUpdate() {
|
||||
version := 246
|
||||
version := 247
|
||||
|
||||
db := global.DB
|
||||
|
||||
|
||||
@@ -56,10 +56,6 @@ func (ct *LoginLog) List(c *gin.Context) {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
|
||||
return
|
||||
}
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
if !service.AllService.UserService.IsAdmin(u) || query.IsMy == 1 {
|
||||
query.UserId = int(u.Id)
|
||||
}
|
||||
res := service.AllService.LoginLogService.List(query.Page, query.PageSize, func(tx *gorm.DB) {
|
||||
if query.UserId > 0 {
|
||||
tx.Where("user_id = ?", query.UserId)
|
||||
@@ -93,21 +89,16 @@ func (ct *LoginLog) Delete(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
l := service.AllService.LoginLogService.InfoById(f.Id)
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
if !service.AllService.UserService.IsAdmin(u) && l.UserId != u.Id {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
|
||||
if l.Id == 0 {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
|
||||
return
|
||||
}
|
||||
if l.Id > 0 {
|
||||
err := service.AllService.LoginLogService.Delete(l)
|
||||
if err == nil {
|
||||
response.Success(c, nil)
|
||||
return
|
||||
}
|
||||
response.Fail(c, 101, err.Error())
|
||||
err := service.AllService.LoginLogService.Delete(l)
|
||||
if err == nil {
|
||||
response.Success(c, nil)
|
||||
return
|
||||
}
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
|
||||
response.Fail(c, 101, err.Error())
|
||||
}
|
||||
|
||||
// BatchDelete 删除
|
||||
@@ -119,7 +110,7 @@ func (ct *LoginLog) Delete(c *gin.Context) {
|
||||
// @Param body body admin.LoginLogIds true "登录日志"
|
||||
// @Success 200 {object} response.Response
|
||||
// @Failure 500 {object} response.Response
|
||||
// @Router /admin/login_log/delete [post]
|
||||
// @Router /admin/login_log/batchDelete [post]
|
||||
// @Security token
|
||||
func (ct *LoginLog) BatchDelete(c *gin.Context) {
|
||||
f := &admin.LoginLogIds{}
|
||||
|
||||
113
http/controller/admin/my/loginLog.go
Normal file
113
http/controller/admin/my/loginLog.go
Normal file
@@ -0,0 +1,113 @@
|
||||
package my
|
||||
|
||||
import (
|
||||
"Gwen/global"
|
||||
"Gwen/http/request/admin"
|
||||
"Gwen/http/response"
|
||||
"Gwen/model"
|
||||
"Gwen/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type LoginLog struct {
|
||||
}
|
||||
|
||||
// List 列表
|
||||
// @Tags 我的登录日志
|
||||
// @Summary 登录日志列表
|
||||
// @Description 登录日志列表
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param page query int false "页码"
|
||||
// @Param page_size query int false "页大小"
|
||||
// @Param user_id query int false "用户ID"
|
||||
// @Success 200 {object} response.Response{data=model.LoginLogList}
|
||||
// @Failure 500 {object} response.Response
|
||||
// @Router /admin/my/login_log/list [get]
|
||||
// @Security token
|
||||
func (ct *LoginLog) List(c *gin.Context) {
|
||||
query := &admin.LoginLogQuery{}
|
||||
if err := c.ShouldBindQuery(query); err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
|
||||
return
|
||||
}
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
res := service.AllService.LoginLogService.List(query.Page, query.PageSize, func(tx *gorm.DB) {
|
||||
tx.Where("user_id = ? and is_deleted = ?", u.Id, model.IsDeletedNo)
|
||||
tx.Order("id desc")
|
||||
})
|
||||
response.Success(c, res)
|
||||
}
|
||||
|
||||
// Delete 删除
|
||||
// @Tags 我的登录日志
|
||||
// @Summary 登录日志删除
|
||||
// @Description 登录日志删除
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body model.LoginLog true "登录日志信息"
|
||||
// @Success 200 {object} response.Response
|
||||
// @Failure 500 {object} response.Response
|
||||
// @Router /admin/my/login_log/delete [post]
|
||||
// @Security token
|
||||
func (ct *LoginLog) Delete(c *gin.Context) {
|
||||
f := &model.LoginLog{}
|
||||
if err := c.ShouldBindJSON(f); err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
|
||||
return
|
||||
}
|
||||
id := f.Id
|
||||
errList := global.Validator.ValidVar(c, id, "required,gt=0")
|
||||
if len(errList) > 0 {
|
||||
response.Fail(c, 101, errList[0])
|
||||
return
|
||||
}
|
||||
l := service.AllService.LoginLogService.InfoById(f.Id)
|
||||
if l.Id == 0 || l.IsDeleted == model.IsDeletedYes {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
|
||||
return
|
||||
}
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
if l.UserId != u.Id {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
|
||||
return
|
||||
}
|
||||
err := service.AllService.LoginLogService.SoftDelete(l)
|
||||
if err == nil {
|
||||
response.Success(c, nil)
|
||||
return
|
||||
}
|
||||
response.Fail(c, 101, err.Error())
|
||||
}
|
||||
|
||||
// BatchDelete 删除
|
||||
// @Tags 我的登录日志
|
||||
// @Summary 登录日志批量删除
|
||||
// @Description 登录日志批量删除
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body admin.LoginLogIds true "登录日志"
|
||||
// @Success 200 {object} response.Response
|
||||
// @Failure 500 {object} response.Response
|
||||
// @Router /admin/my/login_log/batchDelete [post]
|
||||
// @Security token
|
||||
func (ct *LoginLog) BatchDelete(c *gin.Context) {
|
||||
f := &admin.LoginLogIds{}
|
||||
if err := c.ShouldBindJSON(f); err != nil {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
|
||||
return
|
||||
}
|
||||
if len(f.Ids) == 0 {
|
||||
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
|
||||
return
|
||||
}
|
||||
u := service.AllService.UserService.CurUser(c)
|
||||
err := service.AllService.LoginLogService.BatchSoftDelete(u.Id, f.Ids)
|
||||
if err == nil {
|
||||
response.Success(c, nil)
|
||||
return
|
||||
}
|
||||
response.Fail(c, 101, err.Error())
|
||||
return
|
||||
}
|
||||
@@ -19,7 +19,7 @@ func (lp *LoginPayload) FromUser(user *model.User) {
|
||||
}
|
||||
|
||||
var UserRouteNames = []string{
|
||||
"MyTagList", "MyAddressBookList", "MyInfo", "MyAddressBookCollection", "MyPeer", "MyShareRecordList",
|
||||
"MyTagList", "MyAddressBookList", "MyInfo", "MyAddressBookCollection", "MyPeer", "MyShareRecordList", "MyLoginLog",
|
||||
}
|
||||
var AdminRouteNames = []string{"*"}
|
||||
|
||||
|
||||
@@ -160,8 +160,8 @@ func OauthBind(rg *gin.RouterGroup) {
|
||||
|
||||
}
|
||||
func LoginLogBind(rg *gin.RouterGroup) {
|
||||
aR := rg.Group("/login_log")
|
||||
cont := &admin.LoginLog{}
|
||||
aR := rg.Group("/login_log").Use(middleware.AdminPrivilege())
|
||||
aR.GET("/list", cont.List)
|
||||
aR.POST("/delete", cont.Delete)
|
||||
aR.POST("/batchDelete", cont.BatchDelete)
|
||||
@@ -274,6 +274,13 @@ func MyBind(rg *gin.RouterGroup) {
|
||||
rg.GET("/my/peer/list", cont.List)
|
||||
|
||||
}
|
||||
|
||||
{
|
||||
cont := &my.LoginLog{}
|
||||
rg.GET("/my/login_log/list", cont.List)
|
||||
rg.POST("/my/login_log/delete", cont.Delete)
|
||||
rg.POST("/my/login_log/batchDelete", cont.BatchDelete)
|
||||
}
|
||||
}
|
||||
|
||||
func ShareRecordBind(rg *gin.RouterGroup) {
|
||||
|
||||
@@ -4,12 +4,13 @@ type LoginLog struct {
|
||||
IdModel
|
||||
UserId uint `json:"user_id" gorm:"default:0;not null;"`
|
||||
Client string `json:"client"` //webadmin,webclient,app,
|
||||
DeviceId string `json:"device_id"`
|
||||
DeviceId string `json:"device_id"`
|
||||
Uuid string `json:"uuid"`
|
||||
Ip string `json:"ip"`
|
||||
Type string `json:"type"` //account,oauth
|
||||
Platform string `json:"platform"` //windows,linux,mac,android,ios
|
||||
UserTokenId uint `json:"user_token_id" gorm:"default:0;not null;"`
|
||||
IsDeleted uint `json:"is_deleted" gorm:"default:0;not null;"`
|
||||
TimeModel
|
||||
}
|
||||
|
||||
@@ -24,6 +25,11 @@ const (
|
||||
LoginLogTypeOauth = "oauth"
|
||||
)
|
||||
|
||||
const (
|
||||
IsDeletedNo = 0
|
||||
IsDeletedYes = 1
|
||||
)
|
||||
|
||||
type LoginLogList struct {
|
||||
LoginLogs []*LoginLog `json:"list"`
|
||||
Pagination
|
||||
|
||||
@@ -47,3 +47,12 @@ func (us *LoginLogService) Update(u *model.LoginLog) error {
|
||||
func (us *LoginLogService) BatchDelete(ids []uint) error {
|
||||
return global.DB.Where("id in (?)", ids).Delete(&model.LoginLog{}).Error
|
||||
}
|
||||
|
||||
func (us *LoginLogService) SoftDelete(l *model.LoginLog) error {
|
||||
l.IsDeleted = model.IsDeletedYes
|
||||
return us.Update(l)
|
||||
}
|
||||
|
||||
func (us *LoginLogService) BatchSoftDelete(uid uint, ids []uint) error {
|
||||
return global.DB.Model(&model.LoginLog{}).Where("user_id = ? and id in (?)", uid, ids).Update("is_deleted", model.IsDeletedYes).Error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user