accessAuth.go 4.5 KB

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