filter.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package routers
  2. import (
  3. "Cold_Api/conf"
  4. "Cold_Api/controllers/lib"
  5. "Cold_Api/models/Account"
  6. "github.com/beego/beego/v2/server/web/context"
  7. "log"
  8. "strings"
  9. )
  10. var (
  11. version = conf.Version
  12. filterExcludeURLMap = make(map[string]int)
  13. filterOnlyLoginCheckURLMap = make(map[string]int)
  14. )
  15. var InitSetFilterUrl = func() {
  16. excludeUrl := conf.FilterExcludeURL
  17. if len(excludeUrl) > 0 {
  18. excludeUrlSlice := strings.Split(excludeUrl, ",")
  19. if len(excludeUrlSlice) > 0 {
  20. for _, v := range excludeUrlSlice {
  21. filterExcludeURLMap[version+v] = 1
  22. }
  23. }
  24. }
  25. checkLoginUrl := conf.FilterOnlyLoginCheckURL
  26. if len(checkLoginUrl) > 0 {
  27. checkLoginUrlSlice := strings.Split(checkLoginUrl, ",")
  28. if len(checkLoginUrlSlice) > 0 {
  29. for _, v := range checkLoginUrlSlice {
  30. filterOnlyLoginCheckURLMap[version+v] = 1
  31. }
  32. }
  33. }
  34. }
  35. func RBACFilter(ctx *context.Context) {
  36. //判断URL是否排除
  37. if _, ok := filterExcludeURLMap[ctx.Request.URL.Path]; ok {
  38. return
  39. }
  40. b_, admin_r := lib.Verification(ctx.GetCookie("User_tokey"), ctx.Input.Query("User_tokey"))
  41. if !b_ {
  42. ctx.Output.JSON(lib.JSONS{Code: 201, Msg: "请重新登陆!"}, true, false)
  43. }
  44. //判断是否只验证登录的URL
  45. if _, ok := filterOnlyLoginCheckURLMap[ctx.Request.URL.Path]; b_ && ok {
  46. return
  47. }
  48. power, _ := Account.Read_Power_ById(admin_r.T_power)
  49. if power.T_menu == "*" {
  50. return
  51. }
  52. api := Account.Read_API_List_ByPower_Id(power.Id, power.T_menu)
  53. flag := false
  54. for _, v := range api {
  55. if version+v.T_uri == ctx.Request.URL.Path {
  56. flag = true
  57. break
  58. }
  59. }
  60. if !flag {
  61. data := lib.JSONS{Code: 202, Msg: "无权访问!"}
  62. ctx.Output.JSON(data, true, false)
  63. }
  64. }
  65. // 验证需要T_pid访问的接口
  66. func T_pidFilter(ctx *context.Context) {
  67. GetCookie := ctx.GetCookie("User_tokey")
  68. GetString := ctx.Input.Query("User_tokey")
  69. User_tokey := GetCookie
  70. if len(User_tokey) == 0 {
  71. User_tokey = GetString
  72. }
  73. T_pid := lib.Admin_r.T_pid
  74. if T_pid == 0 {
  75. T_pid, _ = Account.Redis_Tokey_T_pid_Get(User_tokey)
  76. }
  77. // fixme 如果逻辑正常,不需要做pid验证
  78. //if T_pid == 0 {
  79. // data := lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  80. // ctx.Output.JSON(data, true, false)
  81. //}
  82. log.Println("T_pid", T_pid)
  83. }