Files
ai-agent/internal/controllers/dashboard/session_controller.go
T
mlogclub 79614b2d07 Refactor import paths to use internal/pkg/httpx/params
- Updated multiple repository and service files to replace imports from "github.com/mlogclub/simple/web/params" with "cs-agent/internal/pkg/httpx/params".
- Adjusted context handling in auth_service and ws_service to use gin.Context instead of iris.Context.
- Ensured consistent usage of HTTP status responses across websocket handlers.
2026-05-23 22:10:20 +08:00

82 lines
2.4 KiB
Go

package dashboard
import (
"cs-agent/internal/pkg/constants"
"cs-agent/internal/pkg/dto/request"
"cs-agent/internal/pkg/dto/response"
"cs-agent/internal/pkg/utils"
"cs-agent/internal/services"
"time"
"cs-agent/internal/pkg/httpx/params"
"github.com/gin-gonic/gin"
"github.com/mlogclub/simple/web"
)
type SessionController struct {
Ctx *gin.Context
}
func (c *SessionController) AnyList() *web.JsonResult {
if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionSessionView); err != nil {
return web.JsonError(err)
}
cnd := params.NewPagedSqlCnd(c.Ctx,
params.QueryFilter{ParamName: "userId"},
params.QueryFilter{ParamName: "clientType"},
).Desc("id")
list, paging := services.LoginSessionService.FindPageByCnd(cnd)
results := make([]response.SessionResponse, 0, len(list))
for _, item := range list {
username := ""
if user := services.UserService.Get(item.UserID); user != nil {
username = user.Username
}
results = append(results, response.SessionResponse{
ID: item.ID,
UserID: item.UserID,
Username: username,
ClientType: item.ClientType,
ClientIP: item.ClientIP,
UserAgent: item.UserAgent,
ExpiredAt: item.ExpiredAt.Format(time.DateTime),
RevokedAt: utils.FormatTimePtr(item.RevokedAt),
LastSeenAt: utils.FormatTimePtr(item.LastSeenAt),
})
}
return web.JsonData(&web.PageResult{Results: results, Page: paging})
}
func (c *SessionController) PostRevoke() *web.JsonResult {
user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionSessionRevoke)
if err != nil {
return web.JsonError(err)
}
req := request.RevokeSessionRequest{}
if err := params.ReadJSON(c.Ctx, &req); err != nil {
return web.JsonError(err)
}
if err := services.LoginSessionService.Revoke(req.ID, user.UserID, user.Username); err != nil {
return web.JsonError(err)
}
return web.JsonSuccess()
}
func (c *SessionController) PostRevokeByUser() *web.JsonResult {
user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionSessionRevoke)
if err != nil {
return web.JsonError(err)
}
req := request.RevokeUserSessionsRequest{}
if err := params.ReadJSON(c.Ctx, &req); err != nil {
return web.JsonError(err)
}
if err := services.LoginSessionService.RevokeByUser(req.UserID, user.UserID, user.Username); err != nil {
return web.JsonError(err)
}
return web.JsonSuccess()
}