accessAuth.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * @description: 访问权限认证
  3. * @Author: CP
  4. * @Date: 2020-10-09 10:43:39
  5. * @FilePath: \construction_management\web\middleware\accessAuth.go
  6. */
  7. package middleware
  8. import (
  9. "errors"
  10. "github.com/kataras/iris/v12"
  11. )
  12. // 员工表权限解析
  13. type permission struct {
  14. Add int ` json:"add" `
  15. Delete int ` json:"delete" `
  16. Access int ` json:"access" `
  17. }
  18. // 权限JSON对象
  19. type permissionPath struct {
  20. Contract path `json:"contract" `
  21. Safe path `json:"safe" `
  22. Quality path `json:"quality" `
  23. ProjectSetting []string `json:"projectSetting" `
  24. }
  25. // 对象中地址列表
  26. type path struct {
  27. Add []string `json:"add" `
  28. Access []string `json:"access" `
  29. Delete []string `json:"delete" `
  30. }
  31. // 权限验证中间件
  32. func AccessAuth(ctx iris.Context) {
  33. // // 1.获得成员信息
  34. // account := ctx.Values().Get("account").(*models.CmProjectAccount)
  35. // // 1-1.是管理员- 拥有所有权限
  36. // if account.IsAdmin != 1 {
  37. // // if account.IsAdmin == 1 {
  38. // // 2.获得员工可访问的权限
  39. // contractPermission := permission{}
  40. // if account.ContractPermission != "" {
  41. // err := json.Unmarshal([]byte(account.ContractPermission), &contractPermission)
  42. // // 错误后 全部权限默认为0
  43. // if err != nil {
  44. // log.Println("合同权限解析错误:err=", err)
  45. // }
  46. // }
  47. // safePermission := permission{}
  48. // if account.SafePermission != "" {
  49. // err := json.Unmarshal([]byte(account.SafePermission), &safePermission)
  50. // if err != nil {
  51. // log.Println("安全权限解析错误:err=", err)
  52. // }
  53. // }
  54. // qualityPermission := permission{}
  55. // if account.QualityPermission != "" {
  56. // err := json.Unmarshal([]byte(account.QualityPermission), &qualityPermission)
  57. // if err != nil {
  58. // log.Println("质量权限解析错误:err=", err)
  59. // }
  60. // }
  61. // // 2-1获得权限列表
  62. // permissionPath := permissionPath{}
  63. // permissionPathData, err := ioutil.ReadFile("../lib/permission.json")
  64. // err = json.Unmarshal(permissionPathData, &permissionPath)
  65. // if err != nil {
  66. // log.Println("权限解析错误:err=", err)
  67. // ctx.JSON(iris.Map{"code": 2, "msg": "权限解析错误"})
  68. // return
  69. // }
  70. // // 2-2 不容许访问的权限--比对访问路径
  71. // requestPath := ctx.Path()
  72. // // 合同权限
  73. // err = verifyAuth(contractPermission, permissionPath.Contract, requestPath)
  74. // if err != nil {
  75. // ctx.JSON(iris.Map{"code": 2, "msg": fmt.Sprintf("%s", err)})
  76. // return
  77. // }
  78. // // 安全权限
  79. // err = verifyAuth(contractPermission, permissionPath.Safe, requestPath)
  80. // if err != nil {
  81. // ctx.JSON(iris.Map{"code": 2, "msg": fmt.Sprintf("%s", err)})
  82. // return
  83. // }
  84. // // 质量权限
  85. // err = verifyAuth(contractPermission, permissionPath.Quality, requestPath)
  86. // if err != nil {
  87. // ctx.JSON(iris.Map{"code": 2, "msg": fmt.Sprintf("%s", err)})
  88. // return
  89. // }
  90. // // 项目设置 -只有管理员才能访问
  91. // for _, path := range permissionPath.ProjectSetting {
  92. // if path == requestPath {
  93. // ctx.JSON(iris.Map{"code": 2, "msg": "无权访问"})
  94. // return
  95. // }
  96. // }
  97. // }
  98. ctx.Next()
  99. }
  100. // 验证权限路径
  101. func verifyAuth(permission permission, pathList path, requestPath string) error {
  102. if permission.Add == 0 {
  103. for _, path := range pathList.Add {
  104. if path == requestPath {
  105. return errors.New("无权访问")
  106. }
  107. }
  108. }
  109. if permission.Access == 0 {
  110. for _, path := range pathList.Access {
  111. if path == requestPath {
  112. return errors.New("无权访问")
  113. }
  114. }
  115. }
  116. if permission.Delete == 0 {
  117. for _, path := range pathList.Delete {
  118. if path == requestPath {
  119. return errors.New("无权访问")
  120. }
  121. }
  122. }
  123. return nil
  124. }