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. b_, admin := lib.Verification(ctx.GetCookie("User_tokey"), ctx.Input.Query("User_tokey"))
  40. if !b_ {
  41. ctx.Output.JSON(lib.JSONS{Code: 201, Msg: "请重新登陆!"}, true, false)
  42. }
  43. //判断是否只验证登录的URL
  44. if _, ok := filterOnlyLoginCheckURLMap[ctx.Request.URL.Path]; b_ && ok {
  45. return
  46. }
  47. power, _ := Account.Read_Power_ById(admin.T_power)
  48. if power.T_Menu_Bind == "*" {
  49. return
  50. }
  51. api := Account.Read_API_List_ByPower_Id(power.Id, power.T_Menu_Bind)
  52. flag := false
  53. for _, v := range api {
  54. if version+v.T_uri == ctx.Request.URL.Path {
  55. flag = true
  56. break
  57. }
  58. }
  59. if !flag {
  60. data := lib.JSONS{Code: 201, Msg: "无权访问!"}
  61. ctx.Output.JSON(data, true, false)
  62. }
  63. }