filter.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. "strings"
  8. )
  9. var (
  10. version = conf.Version
  11. filterExcludeURLMap = make(map[string]int)
  12. filterOnlyLoginCheckURLMap = make(map[string]int)
  13. )
  14. var InitSetFilterUrl = func() {
  15. excludeUrl := conf.FilterExcludeURL
  16. if len(excludeUrl) > 0 {
  17. excludeUrlSlice := strings.Split(excludeUrl, ",")
  18. if len(excludeUrlSlice) > 0 {
  19. for _, v := range excludeUrlSlice {
  20. filterExcludeURLMap[version+v] = 1
  21. }
  22. }
  23. }
  24. checkLoginUrl := conf.FilterOnlyLoginCheckURL
  25. if len(checkLoginUrl) > 0 {
  26. checkLoginUrlSlice := strings.Split(checkLoginUrl, ",")
  27. if len(checkLoginUrlSlice) > 0 {
  28. for _, v := range checkLoginUrlSlice {
  29. filterOnlyLoginCheckURLMap[version+v] = 1
  30. }
  31. }
  32. }
  33. }
  34. func FilterRBAC(ctx *context.Context) {
  35. //判断URL是否排除
  36. if _, ok := filterExcludeURLMap[ctx.Request.URL.Path]; ok {
  37. return
  38. }
  39. var b_ bool
  40. b_, lib.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(lib.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. }