accessAuth.go 3.5 KB

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