accessAuth.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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/models"
  18. "go.mod/web/utils"
  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 --没有标段ID 默认为0
  55. bidsectionIdHeader := ctx.GetHeader("bidsectionId")
  56. bidsectionId, _ := utils.GetDecryptId(bidsectionIdHeader)
  57. // if bidsectionIdHeader == "" {
  58. // ctx.JSON(iris.Map{"code": 2, "msg": "标段解析出错"})
  59. // return
  60. // } else {
  61. // bidsectionId, err := utils.GetDecryptId(bidsectionIdHeader)
  62. // if err != nil {
  63. // ctx.JSON(iris.Map{"code": 2, "msg": "标段解析出错"})
  64. // return
  65. // }
  66. // }
  67. // key := fmt.Sprintf("pm_%d_%d", account.ProjectId, account.Id)
  68. // bidsectionId := lib.NewRedis().GetBidsectionIdByCache(key)
  69. // 1-2 获得账号权限
  70. permissionAccountDao := dao.NewPermissionAccountDao(datasource.InstanceDbMaster())
  71. permissionData := permissionAccountDao.GetBidsectionIdAccountId(bidsectionId, account.Id)
  72. // permissionData := s.permissionAccountDao.GetBidsectionId(bidsectionId)
  73. // fmt.Println(bidsectionId, account.Id)
  74. // fmt.Println(permissionData)
  75. // 1-1.是管理员- 拥有所有权限
  76. if account.IsAdmin != 1 {
  77. // if account.IsAdmin == 1 {
  78. // 2.获得员工可访问的权限
  79. contractPermission := permission{}
  80. if permissionData.ContractPermission != "" {
  81. err := json.Unmarshal([]byte(permissionData.ContractPermission), &contractPermission)
  82. // 错误后 全部权限默认为0
  83. if err != nil {
  84. log.Println("合同权限解析错误:err=", err)
  85. }
  86. }
  87. safePermission := permission{}
  88. if permissionData.SafePermission != "" {
  89. err := json.Unmarshal([]byte(permissionData.SafePermission), &safePermission)
  90. if err != nil {
  91. log.Println("安全权限解析错误:err=", err)
  92. }
  93. }
  94. qualityPermission := permission{}
  95. if permissionData.QualityPermission != "" {
  96. err := json.Unmarshal([]byte(permissionData.QualityPermission), &qualityPermission)
  97. if err != nil {
  98. log.Println("质量权限解析错误:err=", err)
  99. }
  100. }
  101. // 2-1获得权限列表
  102. permissionPath := permissionPath{}
  103. permissionPathData, err := ioutil.ReadFile("../lib/permission.json")
  104. err = json.Unmarshal(permissionPathData, &permissionPath)
  105. if err != nil {
  106. log.Println("权限解析错误:err=", err)
  107. ctx.JSON(iris.Map{"code": 2, "msg": "权限解析错误"})
  108. return
  109. }
  110. // 2-2 不容许访问的权限--比对访问路径
  111. requestPath := ctx.Path()
  112. // 合同权限
  113. err = verifyAuth(contractPermission, permissionPath.Contract, requestPath)
  114. if err != nil {
  115. ctx.JSON(iris.Map{"code": 2, "msg": fmt.Sprintf("%s", err)})
  116. return
  117. }
  118. // 安全权限
  119. err = verifyAuth(safePermission, permissionPath.Safe, requestPath)
  120. if err != nil {
  121. ctx.JSON(iris.Map{"code": 2, "msg": fmt.Sprintf("%s", err)})
  122. return
  123. }
  124. // 质量权限
  125. err = verifyAuth(qualityPermission, permissionPath.Quality, requestPath)
  126. if err != nil {
  127. ctx.JSON(iris.Map{"code": 2, "msg": fmt.Sprintf("%s", err)})
  128. return
  129. }
  130. // 项目设置 -只有管理员才能访问
  131. for _, path := range permissionPath.ProjectSetting {
  132. if path == requestPath {
  133. ctx.JSON(iris.Map{"code": 2, "msg": "无权访问"})
  134. return
  135. }
  136. }
  137. }
  138. ctx.Next()
  139. }
  140. // 验证权限路径
  141. func verifyAuth(permission permission, pathList path, requestPath string) error {
  142. if permission.Add == 0 {
  143. for _, path := range pathList.Add {
  144. if path == requestPath {
  145. return errors.New("无权访问")
  146. }
  147. }
  148. }
  149. if permission.Access == 0 {
  150. fmt.Println(requestPath)
  151. for _, path := range pathList.Access {
  152. if path == requestPath {
  153. return errors.New("无权访问")
  154. }
  155. }
  156. }
  157. if permission.Delete == 0 {
  158. for _, path := range pathList.Delete {
  159. if path == requestPath {
  160. return errors.New("无权访问")
  161. }
  162. }
  163. }
  164. return nil
  165. }