filter.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package routers
  2. import (
  3. "Cold_Api/conf"
  4. "Cold_Api/controllers/lib"
  5. "Cold_Api/logs"
  6. "Cold_Api/models/Account"
  7. "github.com/beego/beego/v2/server/web/context"
  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. //判断是否只验证登录的URL
  68. if _, ok := filterOnlyLoginCheckURLMap[ctx.Request.URL.Path]; ok {
  69. return
  70. }
  71. GetCookie := ctx.GetCookie("User_tokey")
  72. GetString := ctx.Input.Query("User_tokey")
  73. User_tokey := GetCookie
  74. if len(User_tokey) == 0 {
  75. User_tokey = GetString
  76. }
  77. T_pid := lib.Admin_r.T_pid
  78. if T_pid == 0 {
  79. T_pid, _ = Account.Redis_Tokey_T_pid_Get(User_tokey)
  80. }
  81. // fixme 如果逻辑正常,不需要做pid验证
  82. if T_pid == 0 {
  83. data := lib.JSONS{Code: 202, Msg: "T_pid Err!"}
  84. ctx.Output.JSON(data, true, false)
  85. }
  86. logs.Println("T_pid", T_pid)
  87. }