feat: Implement Oauth2 login log service and repository

- Added Oauth2LogLoginService for managing user authorization logs.
- Implemented methods for inserting logs, cleaning logs, and exporting log data.
- Created a new file for Oauth2 login log service.

refactor: Remove unused open_api module

- Deleted the open_api.go file as it was not utilized in the project.

fix: Update error codes in SysProfileController

- Changed error codes for binding errors and user authentication errors to more descriptive values.

fix: Update cache handling in SysConfig and SysDictType services

- Modified Redis set operations to include expiration time for cached values.

refactor: Update middleware authorization checks

- Replaced PreAuthorize middleware with AuthorizeUser across multiple routes in system and tool modules for consistency.

chore: Clean up trace and ws modules

- Updated middleware authorization in trace and ws modules to use AuthorizeUser.
This commit is contained in:
TsMask
2025-04-27 11:07:34 +08:00
parent b29a36e7b5
commit 56991a0b49
72 changed files with 2334 additions and 873 deletions

View File

@@ -48,13 +48,15 @@ func (s *SysCacheController) Info(c *gin.Context) {
func (s SysCacheController) Names(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
caches := []model.SysCache{
model.NewNames(i18n.TKey(language, "cache.name.user"), constants.CACHE_LOGIN_TOKEN),
model.NewNames(i18n.TKey(language, "cache.name.token"), constants.CACHE_TOKEN_DEVICE),
model.NewNames(i18n.TKey(language, "cache.name.sys_config"), constants.CACHE_SYS_CONFIG),
model.NewNames(i18n.TKey(language, "cache.name.sys_dict"), constants.CACHE_SYS_DICT),
model.NewNames(i18n.TKey(language, "cache.name.captcha_codes"), constants.CACHE_CAPTCHA_CODE),
model.NewNames(i18n.TKey(language, "cache.name.repeat_submit"), constants.CACHE_REPEAT_SUBMIT),
model.NewNames(i18n.TKey(language, "cache.name.rate_limit"), constants.CACHE_RATE_LIMIT),
model.NewNames(i18n.TKey(language, "cache.name.pwd_err_cnt"), constants.CACHE_PWD_ERR_COUNT),
model.NewNames(i18n.TKey(language, "cache.name.oauth2_codes"), constants.CACHE_OAUTH2_CODE),
model.NewNames(i18n.TKey(language, "cache.name.oauth2_devices"), constants.CACHE_OAUTH2_DEVICE),
model.NewNames(i18n.TKey(language, "cache.name.i18n"), constants.CACHE_I18N),
model.NewNames(i18n.TKey(language, "cache.name.ne_info"), constants.CACHE_NE_INFO),
model.NewNames(i18n.TKey(language, "cache.name.ne_data"), constants.CACHE_NE_DATA),
@@ -166,7 +168,7 @@ func (s SysCacheController) CleanKeys(c *gin.Context) {
c.JSON(422, resp.CodeMsg(40422, errMsgs))
return
}
if constants.CACHE_LOGIN_TOKEN == query.CacheName {
if constants.CACHE_TOKEN_DEVICE == query.CacheName {
c.JSON(200, resp.ErrMsg("Cannot delete user information cache"))
return
}

View File

@@ -46,7 +46,7 @@ func (s *SysUserOnlineController) List(c *gin.Context) {
userName := c.Query("userName")
// 获取所有在线用户key
keys, _ := redis.GetKeys("", constants.CACHE_LOGIN_TOKEN+":*")
keys, _ := redis.GetKeys("", constants.CACHE_TOKEN_DEVICE+":*")
// 分批获取
arr := make([]string, 0)
@@ -69,13 +69,13 @@ func (s *SysUserOnlineController) List(c *gin.Context) {
continue
}
var tokenInfo token.TokenInfo
err := json.Unmarshal([]byte(str), &tokenInfo)
var info token.UserInfo
err := json.Unmarshal([]byte(str), &info)
if err != nil {
continue
}
onlineUser := s.sysUserOnlineService.TokenInfoToUserOnline(tokenInfo)
onlineUser := s.sysUserOnlineService.UserInfoToUserOnline(info)
if onlineUser.TokenID != "" {
userOnlines = append(userOnlines, onlineUser)
}
@@ -122,15 +122,14 @@ func (s *SysUserOnlineController) List(c *gin.Context) {
func (s SysUserOnlineController) Logout(c *gin.Context) {
tokenIdStr := c.Param("tokenId")
if tokenIdStr == "" || strings.Contains(tokenIdStr, "*") {
c.JSON(400, resp.CodeMsg(40010, "bind err: tokenId is empty"))
c.JSON(422, resp.CodeMsg(422002, "bind err: tokenId is empty"))
return
}
// 处理字符转id数组后去重
ids := strings.Split(tokenIdStr, ",")
uniqueIDs := parse.RemoveDuplicates(ids)
uniqueIDs := parse.RemoveDuplicatesToArray(tokenIdStr, ",")
for _, v := range uniqueIDs {
key := constants.CACHE_LOGIN_TOKEN + ":" + v
key := constants.CACHE_TOKEN_DEVICE + ":" + v
if err := redis.Del("", key); err != nil {
c.JSON(200, resp.ErrMsg(err.Error()))
return

View File

@@ -23,15 +23,14 @@ func Setup(router *gin.Engine) {
monitorGroup := router.Group("/monitor")
{
monitorGroup.GET("/load",
middleware.PreAuthorize(nil),
middleware.AuthorizeUser(nil),
controller.NewMonitor.Load,
)
}
// 服务器信息
router.GET("/monitor/system",
// middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:system:info"}}),
middleware.PreAuthorize(nil),
middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:system:info"}}),
controller.NewSystem.Info,
)
@@ -39,11 +38,11 @@ func Setup(router *gin.Engine) {
sysUserOnlineGroup := router.Group("/monitor/user-online")
{
sysUserOnlineGroup.GET("/list",
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:online:list"}}),
middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:online:list"}}),
controller.NewSysUserOnline.List,
)
sysUserOnlineGroup.DELETE("/logout/:tokenId",
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:online:logout"}}),
middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:online:logout"}}),
controller.NewSysUserOnline.Logout,
)
}
@@ -52,32 +51,31 @@ func Setup(router *gin.Engine) {
sysCacheGroup := router.Group("/monitor/cache")
{
sysCacheGroup.GET("",
// middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:cache:info"}}),
middleware.PreAuthorize(nil),
middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:cache:info"}}),
controller.NewSysCache.Info,
)
sysCacheGroup.GET("/names",
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:cache:list"}}),
middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:cache:list"}}),
controller.NewSysCache.Names,
)
sysCacheGroup.GET("/keys",
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:cache:list"}}),
middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:cache:list"}}),
controller.NewSysCache.Keys,
)
sysCacheGroup.GET("/value",
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:cache:query"}}),
middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:cache:query"}}),
controller.NewSysCache.Value,
)
sysCacheGroup.DELETE("/names",
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:cache:remove"}}),
middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:cache:remove"}}),
controller.NewSysCache.CleanNames,
)
sysCacheGroup.DELETE("/keys",
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:cache:remove"}}),
middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:cache:remove"}}),
controller.NewSysCache.CleanKeys,
)
sysCacheGroup.DELETE("/value",
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:cache:remove"}}),
middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:cache:remove"}}),
controller.NewSysCache.CleanValue,
)
}
@@ -86,26 +84,26 @@ func Setup(router *gin.Engine) {
sysJobLogGroup := router.Group("/monitor/job/log")
{
sysJobLogGroup.GET("/list",
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:list"}}),
middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:job:list"}}),
controller.NewSysJobLog.List,
)
sysJobLogGroup.GET("/:logId",
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:query"}}),
middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:job:query"}}),
controller.NewSysJobLog.Info,
)
sysJobLogGroup.DELETE("/:logId",
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:remove"}}),
middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:job:remove"}}),
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.sysJobLog", collectlogs.BUSINESS_TYPE_DELETE)),
controller.NewSysJobLog.Remove,
)
sysJobLogGroup.DELETE("/clean",
repeat.RepeatSubmit(5),
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:remove"}}),
middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:job:remove"}}),
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.sysJobLog", collectlogs.BUSINESS_TYPE_CLEAN)),
controller.NewSysJobLog.Clean,
)
sysJobLogGroup.GET("/export",
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:export"}}),
middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:job:export"}}),
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.sysJobLog", collectlogs.BUSINESS_TYPE_EXPORT)),
controller.NewSysJobLog.Export,
)
@@ -115,47 +113,47 @@ func Setup(router *gin.Engine) {
sysJobGroup := router.Group("/monitor/job")
{
sysJobGroup.GET("/list",
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:list"}}),
middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:job:list"}}),
controller.NewSysJob.List,
)
sysJobGroup.GET("/:jobId",
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:query"}}),
middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:job:query"}}),
controller.NewSysJob.Info,
)
sysJobGroup.POST("",
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:add"}}),
middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:job:add"}}),
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.sysJob", collectlogs.BUSINESS_TYPE_INSERT)),
controller.NewSysJob.Add,
)
sysJobGroup.PUT("",
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:edit"}}),
middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:job:edit"}}),
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.sysJob", collectlogs.BUSINESS_TYPE_UPDATE)),
controller.NewSysJob.Edit,
)
sysJobGroup.DELETE("/:jobId",
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:remove"}}),
middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:job:remove"}}),
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.sysJob", collectlogs.BUSINESS_TYPE_DELETE)),
controller.NewSysJob.Remove,
)
sysJobGroup.PUT("/status",
repeat.RepeatSubmit(5),
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:changeStatus"}}),
middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:job:changeStatus"}}),
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.sysJob", collectlogs.BUSINESS_TYPE_UPDATE)),
controller.NewSysJob.Status,
)
sysJobGroup.PUT("/run/:jobId",
repeat.RepeatSubmit(10),
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:changeStatus"}}),
middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:job:changeStatus"}}),
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.sysJob", collectlogs.BUSINESS_TYPE_UPDATE)),
controller.NewSysJob.Run,
)
sysJobGroup.PUT("/reset",
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:changeStatus"}}),
middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:job:changeStatus"}}),
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.sysJob", collectlogs.BUSINESS_TYPE_CLEAN)),
controller.NewSysJob.ResetQueueJob,
)
sysJobGroup.GET("/export",
middleware.PreAuthorize(map[string][]string{"hasPerms": {"monitor:job:export"}}),
middleware.AuthorizeUser(map[string][]string{"hasPerms": {"monitor:job:export"}}),
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.sysJob", collectlogs.BUSINESS_TYPE_EXPORT)),
controller.NewSysJob.Export,
)

View File

@@ -11,23 +11,23 @@ var NewSysUserOnline = &SysUserOnline{}
// SysUserOnline 在线用户 服务层处理
type SysUserOnline struct{}
// TokenInfoToUserOnline 在线用户信息
func (s SysUserOnline) TokenInfoToUserOnline(tokenInfo token.TokenInfo) model.SysUserOnline {
if tokenInfo.UserId <= 0 {
// UserInfoToUserOnline 在线用户信息
func (s SysUserOnline) UserInfoToUserOnline(info token.UserInfo) model.SysUserOnline {
if info.UserId <= 0 {
return model.SysUserOnline{}
}
sysUserOnline := model.SysUserOnline{
TokenID: tokenInfo.UUID,
UserName: tokenInfo.User.UserName,
LoginIp: tokenInfo.LoginIp,
LoginLocation: tokenInfo.LoginLocation,
Browser: tokenInfo.Browser,
OS: tokenInfo.OS,
LoginTime: tokenInfo.LoginTime,
TokenID: info.DeviceId,
UserName: info.User.UserName,
LoginIp: info.LoginIp,
LoginLocation: info.LoginLocation,
Browser: info.Browser,
OS: info.OS,
LoginTime: info.LoginTime,
}
if tokenInfo.User.DeptId > 0 {
sysUserOnline.DeptName = tokenInfo.User.Dept.DeptName
if info.User.DeptId > 0 {
sysUserOnline.DeptName = info.User.Dept.DeptName
}
return sysUserOnline
}