accessAuth.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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(bidsectionId, account.Id)
  75. // fmt.Println(permissionData)
  76. // 1-1.是管理员- 拥有所有权限
  77. if account.IsAdmin != 1 {
  78. // if account.IsAdmin == 1 {
  79. // 2.获得员工可访问的权限
  80. contractPermission := permission{}
  81. if permissionData.ContractPermission != "" {
  82. err := json.Unmarshal([]byte(permissionData.ContractPermission), &contractPermission)
  83. // 错误后 全部权限默认为0
  84. if err != nil {
  85. log.Println("合同权限解析错误:err=", err)
  86. }
  87. }
  88. safePermission := permission{}
  89. if permissionData.SafePermission != "" {
  90. err := json.Unmarshal([]byte(permissionData.SafePermission), &safePermission)
  91. if err != nil {
  92. log.Println("安全权限解析错误:err=", err)
  93. }
  94. }
  95. qualityPermission := permission{}
  96. if permissionData.QualityPermission != "" {
  97. err := json.Unmarshal([]byte(permissionData.QualityPermission), &qualityPermission)
  98. if err != nil {
  99. log.Println("质量权限解析错误:err=", err)
  100. }
  101. }
  102. // 2-1获得权限列表
  103. permissionPath := permissionPath{}
  104. permissionPathData, err := ioutil.ReadFile("../lib/permission.json")
  105. err = json.Unmarshal(permissionPathData, &permissionPath)
  106. if err != nil {
  107. log.Println("权限解析错误:err=", err)
  108. ctx.JSON(iris.Map{"code": 2, "msg": "权限解析错误"})
  109. return
  110. }
  111. // 2-2 不容许访问的权限--比对访问路径
  112. requestPath := ctx.Path()
  113. // 2-3 请求的类型
  114. method := ctx.Request().Method
  115. // 合同权限
  116. err = verifyAuth(contractPermission, permissionPath.Contract, requestPath, method)
  117. if err != nil {
  118. ctx.JSON(iris.Map{"code": 2, "msg": fmt.Sprintf("%s", err)})
  119. return
  120. }
  121. // 安全权限
  122. err = verifyAuth(safePermission, permissionPath.Safe, requestPath, method)
  123. if err != nil {
  124. ctx.JSON(iris.Map{"code": 2, "msg": fmt.Sprintf("%s", err)})
  125. return
  126. }
  127. // 质量权限
  128. err = verifyAuth(qualityPermission, permissionPath.Quality, requestPath, method)
  129. if err != nil {
  130. ctx.JSON(iris.Map{"code": 2, "msg": fmt.Sprintf("%s", err)})
  131. return
  132. }
  133. // 项目设置 -只有管理员才能访问
  134. for _, path := range permissionPath.ProjectSetting {
  135. if path == requestPath {
  136. ctx.JSON(iris.Map{"code": 2, "msg": "无权访问"})
  137. return
  138. }
  139. }
  140. }
  141. ctx.Next()
  142. }
  143. // 验证权限路径
  144. func verifyAuth(permission permission, pathList path, requestPath string, method string) error {
  145. if permission.Add == 0 {
  146. for _, path := range pathList.Add {
  147. if path == requestPath && method == "POST" {
  148. return errors.New("无权访问")
  149. }
  150. }
  151. }
  152. if permission.Access == 0 {
  153. for _, path := range pathList.Access {
  154. if path == requestPath && method == "GET" {
  155. return errors.New("无权访问")
  156. }
  157. }
  158. }
  159. if permission.Delete == 0 {
  160. for _, path := range pathList.Delete {
  161. if path == requestPath && (method == "POST" || method == "DELETE") {
  162. return errors.New("无权访问")
  163. }
  164. }
  165. }
  166. return nil
  167. }