accessAuth.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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/dao"
  16. "go.mod/datasource"
  17. "go.mod/lib"
  18. "go.mod/models"
  19. )
  20. // 员工表权限解析
  21. type permission struct {
  22. Add int ` json:"add" `
  23. Delete int ` json:"delete" `
  24. Access int ` json:"access" `
  25. }
  26. // 权限JSON对象
  27. type permissionPath struct {
  28. Contract path `json:"contract" `
  29. Safe path `json:"safe" `
  30. Quality path `json:"quality" `
  31. ProjectSetting []string `json:"projectSetting" `
  32. }
  33. // 对象中地址列表
  34. type path struct {
  35. Add []string `json:"add" `
  36. Access []string `json:"access" `
  37. Delete []string `json:"delete" `
  38. }
  39. // 权限验证中间件
  40. func AccessAuth(ctx iris.Context) {
  41. // 需要先设置标段ID,在获得权限进行判断
  42. // 账号活动状态下,入口必须是 /api/contract/survey,
  43. path := ctx.Path()
  44. if path == "/api/contract/survey" || path == "/api/quality/survey" || path == "/api/safe/survey" {
  45. ctx.Next()
  46. return
  47. }
  48. // 1.获得成员信息
  49. account := ctx.Values().Get("account").(*models.CmProjectAccount)
  50. // 1-1获得标段ID
  51. key := fmt.Sprintf("pm_%d_%d", account.ProjectId, account.Id)
  52. bidsectionId := lib.NewRedis().GetBidsectionIdByCache(key)
  53. // 1-2 获得账号权限
  54. permissionAccountDao := dao.NewPermissionAccountDao(datasource.InstanceDbMaster())
  55. permissionData := permissionAccountDao.GetBidsectionIdAccountId(bidsectionId, account.Id)
  56. // permissionData := s.permissionAccountDao.GetBidsectionId(bidsectionId)
  57. fmt.Println(bidsectionId, account.Id)
  58. fmt.Println(permissionData)
  59. // 1-1.是管理员- 拥有所有权限
  60. if account.IsAdmin != 1 {
  61. // if account.IsAdmin == 1 {
  62. // 2.获得员工可访问的权限
  63. contractPermission := permission{}
  64. if permissionData.ContractPermission != "" {
  65. err := json.Unmarshal([]byte(permissionData.ContractPermission), &contractPermission)
  66. // 错误后 全部权限默认为0
  67. if err != nil {
  68. log.Println("合同权限解析错误:err=", err)
  69. }
  70. }
  71. safePermission := permission{}
  72. if permissionData.SafePermission != "" {
  73. err := json.Unmarshal([]byte(permissionData.SafePermission), &safePermission)
  74. if err != nil {
  75. log.Println("安全权限解析错误:err=", err)
  76. }
  77. }
  78. qualityPermission := permission{}
  79. if permissionData.QualityPermission != "" {
  80. err := json.Unmarshal([]byte(permissionData.QualityPermission), &qualityPermission)
  81. if err != nil {
  82. log.Println("质量权限解析错误:err=", err)
  83. }
  84. }
  85. // 2-1获得权限列表
  86. permissionPath := permissionPath{}
  87. permissionPathData, err := ioutil.ReadFile("../lib/permission.json")
  88. err = json.Unmarshal(permissionPathData, &permissionPath)
  89. if err != nil {
  90. log.Println("权限解析错误:err=", err)
  91. ctx.JSON(iris.Map{"code": 2, "msg": "权限解析错误"})
  92. return
  93. }
  94. // 2-2 不容许访问的权限--比对访问路径
  95. requestPath := ctx.Path()
  96. // 合同权限
  97. err = verifyAuth(contractPermission, permissionPath.Contract, requestPath)
  98. if err != nil {
  99. ctx.JSON(iris.Map{"code": 2, "msg": fmt.Sprintf("%s", err)})
  100. return
  101. }
  102. // 安全权限
  103. err = verifyAuth(contractPermission, permissionPath.Safe, requestPath)
  104. if err != nil {
  105. ctx.JSON(iris.Map{"code": 2, "msg": fmt.Sprintf("%s", err)})
  106. return
  107. }
  108. // 质量权限
  109. err = verifyAuth(contractPermission, permissionPath.Quality, requestPath)
  110. if err != nil {
  111. ctx.JSON(iris.Map{"code": 2, "msg": fmt.Sprintf("%s", err)})
  112. return
  113. }
  114. // 项目设置 -只有管理员才能访问
  115. for _, path := range permissionPath.ProjectSetting {
  116. if path == requestPath {
  117. ctx.JSON(iris.Map{"code": 2, "msg": "无权访问"})
  118. return
  119. }
  120. }
  121. }
  122. ctx.Next()
  123. }
  124. // 验证权限路径
  125. func verifyAuth(permission permission, pathList path, requestPath string) error {
  126. if permission.Add == 0 {
  127. for _, path := range pathList.Add {
  128. if path == requestPath {
  129. return errors.New("无权访问")
  130. }
  131. }
  132. }
  133. if permission.Access == 0 {
  134. for _, path := range pathList.Access {
  135. if path == requestPath {
  136. return errors.New("无权访问")
  137. }
  138. }
  139. }
  140. if permission.Delete == 0 {
  141. for _, path := range pathList.Delete {
  142. if path == requestPath {
  143. return errors.New("无权访问")
  144. }
  145. }
  146. }
  147. return nil
  148. }