role.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package middleware
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "icloudapp.cn/tools/entity"
  5. "icloudapp.cn/tools/model"
  6. utString "icloudapp.cn/tools/util/string"
  7. "strings"
  8. )
  9. func RoleMiddleWare() func(c *gin.Context) {
  10. return func(ctx *gin.Context) {
  11. //获取用户的所有分组
  12. uid := utString.ConvertInt64(ctx.GetString("uid"))
  13. if uid == 0 {
  14. accessDenied(ctx, "请先登录后再试")
  15. ctx.Abort()
  16. return
  17. }
  18. _, err := model.NewMRoles(ctx).Verify(uid, ctx.Request.URL)
  19. if err != nil {
  20. accessDenied(ctx, err.Error())
  21. ctx.Abort()
  22. return
  23. }
  24. ctx.Next()
  25. /*
  26. url := ctx.Request.URL.String()
  27. path := ctx.Request.URL.Path
  28. userGroups := service.NewUserGroup(ctx).UserGroups(uid)
  29. splitGroups := strings.Split(userGroups.Groups, ",")
  30. var groupsID []int64
  31. for _, v := range splitGroups {
  32. groupsID = append(groupsID, utString.ConvertInt64(v))
  33. }
  34. if len(groupsID) == 0 {
  35. accessDenied(ctx, "无访问组权限")
  36. ctx.Abort()
  37. return
  38. }
  39. //获取分组对应的权限
  40. groupsField, _ := service.NewGroup(ctx).Infos(groupsID...)
  41. var rolesID strings.Builder
  42. for _, v := range groupsField {
  43. if v == nil {
  44. continue
  45. }
  46. rolesID.WriteString(v.Roles + ",")
  47. }
  48. //获取权限对应的model
  49. roles := uniqueToInt64(rolesID.String(), ",")
  50. if len(roles) == 0 {
  51. accessDenied(ctx, "无权限访问")
  52. ctx.Abort()
  53. return
  54. }
  55. mRole := service.NewRoles(ctx)
  56. modelsField, _ := mRole.Infos(roles...)
  57. var modelIds strings.Builder
  58. for _, v := range modelsField {
  59. modelIds.WriteString(v.ModelIds + ",")
  60. }
  61. //获取对应的model
  62. modulesId := uniqueToInt64(modelIds.String(), ",")
  63. modules := service.NewModule(ctx)
  64. moduleField, _ := modules.Infos(modulesId...)
  65. //验证request是否有权限
  66. matchedURL := make([]string, 0)
  67. for _, module := range moduleField {
  68. moduleURL := module.ModelURL
  69. //设置的url和请求的path一样直接通过,如果是带参数或*需要额外处理
  70. if moduleURL == path {
  71. ctx.Next()
  72. return
  73. } else if strings.Contains(moduleURL, path) {
  74. matchedURL = append(matchedURL, moduleURL)
  75. } else if strings.Contains(moduleURL, ".*") {
  76. //如果是以.*结尾的,将modelURL才分成两部分,前半部分能匹配上当前的url就可以
  77. urlSplit := strings.Split(moduleURL, ",")
  78. if strings.Contains(url, urlSplit[0]) {
  79. ctx.Next()
  80. return
  81. }
  82. }
  83. }
  84. if len(matchedURL) == 0 {
  85. accessDenied(ctx, "无模块访问权限")
  86. ctx.Abort()
  87. return
  88. }
  89. for _, singleURL := range matchedURL {
  90. //这里需要区分url中有没有参数,没有参数就直接过,有参数就再验证参数
  91. parseString, _ := url2.ParseString(singleURL)
  92. queries := parseString.Query()
  93. for name, _ := range queries {
  94. if queries.Get(name) != ctx.Request.URL.Query().Get(name) {
  95. accessDenied(ctx, "权限无匹配")
  96. ctx.Abort()
  97. return
  98. }
  99. }
  100. }
  101. ctx.Next()*/
  102. }
  103. }
  104. func accessDenied(ctx *gin.Context, msg string) {
  105. entity.ResponseNormal(ctx, entity.CodeDenied, msg, []interface{}{})
  106. }
  107. func splitStrToInt64(str, sep string) []int64 {
  108. splitSlice := strings.Split(str, sep)
  109. var res []int64
  110. for _, v := range splitSlice {
  111. res = append(res, utString.ConvertInt64(v))
  112. }
  113. return res
  114. }
  115. func uniqueToInt64(str, sep string) []int64 {
  116. var res []int64
  117. splitSlice := splitStrToInt64(str, sep)
  118. splitMap := make(map[int64]bool, 0)
  119. for _, v := range splitSlice {
  120. if v == 0 {
  121. continue
  122. }
  123. if _, ok := splitMap[v]; ok {
  124. continue
  125. }
  126. splitMap[v] = true
  127. res = append(res, v)
  128. }
  129. return res
  130. }