accessAuth.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. // 2-3 请求的类型
  113. method := ctx.Request().Method
  114. // 合同权限
  115. err = verifyAuth(contractPermission, permissionPath.Contract, requestPath, method)
  116. if err != nil {
  117. ctx.JSON(iris.Map{"code": 2, "msg": fmt.Sprintf("%s", err)})
  118. return
  119. }
  120. // 安全权限
  121. err = verifyAuth(safePermission, permissionPath.Safe, requestPath, method)
  122. if err != nil {
  123. ctx.JSON(iris.Map{"code": 2, "msg": fmt.Sprintf("%s", err)})
  124. return
  125. }
  126. // 质量权限
  127. err = verifyAuth(qualityPermission, permissionPath.Quality, requestPath, method)
  128. if err != nil {
  129. ctx.JSON(iris.Map{"code": 2, "msg": fmt.Sprintf("%s", err)})
  130. return
  131. }
  132. // 项目设置 -只有管理员才能访问
  133. for _, path := range permissionPath.ProjectSetting {
  134. if path == requestPath {
  135. ctx.JSON(iris.Map{"code": 2, "msg": "无权访问"})
  136. return
  137. }
  138. }
  139. }
  140. ctx.Next()
  141. }
  142. // 验证权限路径
  143. func verifyAuth(permission permission, pathList path, requestPath string, method string) error {
  144. if permission.Add == 0 {
  145. for _, path := range pathList.Add {
  146. if path == requestPath && method == "POST" {
  147. return errors.New("无权访问")
  148. }
  149. }
  150. }
  151. if permission.Access == 0 {
  152. for _, path := range pathList.Access {
  153. if path == requestPath && method == "GET" {
  154. return errors.New("无权访问")
  155. }
  156. }
  157. }
  158. if permission.Delete == 0 {
  159. for _, path := range pathList.Delete {
  160. if path == requestPath && (method == "POST" || method == "DELETE") {
  161. return errors.New("无权访问")
  162. }
  163. }
  164. }
  165. return nil
  166. }