permission.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package actions
  2. import (
  3. "Medical_OAuth/app/admin/model"
  4. "errors"
  5. "github.com/gin-gonic/gin"
  6. "gogs.baozhida.cn/zoie/OAuth-core/sdk"
  7. "gorm.io/gorm"
  8. "strconv"
  9. log "gogs.baozhida.cn/zoie/OAuth-core/logger"
  10. "gogs.baozhida.cn/zoie/OAuth-core/pkg"
  11. "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/user"
  12. "gogs.baozhida.cn/zoie/OAuth-core/pkg/response"
  13. "gogs.baozhida.cn/zoie/OAuth-core/sdk/config"
  14. )
  15. type DataPermission struct {
  16. DataScope string
  17. UserId int
  18. DeptId int
  19. RoleId int
  20. }
  21. func PermissionAction() gin.HandlerFunc {
  22. return func(c *gin.Context) {
  23. ormDB, err := pkg.GetOrm(c)
  24. if err != nil {
  25. log.Error(err)
  26. return
  27. }
  28. msgID := pkg.GenerateMsgIDFromContext(c)
  29. var p = new(DataPermission)
  30. if userId := user.GetUserIdStr(c); userId != "" {
  31. p, err = newDataPermission(ormDB, userId)
  32. if err != nil {
  33. log.Errorf("MsgID[%s] PermissionAction error: %s", msgID, err)
  34. response.Error(c, 500, err, "权限范围鉴定错误")
  35. c.Abort()
  36. return
  37. }
  38. }
  39. c.Set(PermissionKey, p)
  40. c.Next()
  41. }
  42. }
  43. func newDataPermission(tx *gorm.DB, userId interface{}) (*DataPermission, error) {
  44. var err error
  45. p := &DataPermission{}
  46. err = tx.Table("sys_user").
  47. Select("sys_user.id as user_id", "sys_role.id as role_id", "sys_user.dept_id", "sys_role.data_scope").
  48. Joins("left join sys_role on sys_role.id = sys_user.role_id").
  49. Where("sys_user.id = ?", userId).
  50. Scan(p).Error
  51. if err != nil {
  52. err = errors.New("获取用户数据出错 msg:" + err.Error())
  53. return nil, err
  54. }
  55. return p, nil
  56. }
  57. func UserPermission(tableName string, p *DataPermission) func(db *gorm.DB) *gorm.DB {
  58. return func(db *gorm.DB) *gorm.DB {
  59. if !config.ApplicationConfig.EnableDP {
  60. return db
  61. }
  62. if p == nil {
  63. return db
  64. }
  65. switch p.DataScope {
  66. //case "2":
  67. // return db.Where(tableName+".create_by in (select sys_user.id from sys_role_dept left join sys_user on sys_user.dept_id=sys_role_dept.dept_id where sys_role_dept.role_id = ?) id = ?", p.RoleId, p.UserId)
  68. case "3":
  69. return db.Where(tableName+".dept_id = ? ", p.DeptId)
  70. case "4":
  71. return db.Where(tableName+".create_by in (SELECT id from sys_user where sys_user.dept_id in(select id from sys_dept where dept_path like ? )) or id = ?", "%/"+pkg.IntToString(p.DeptId)+"/%", p.UserId)
  72. case "5":
  73. return db.Where(tableName+".id = ?", p.UserId)
  74. default:
  75. return db
  76. }
  77. }
  78. }
  79. func Permission(tableName string, p *DataPermission) func(db *gorm.DB) *gorm.DB {
  80. return func(db *gorm.DB) *gorm.DB {
  81. if !config.ApplicationConfig.EnableDP {
  82. return db
  83. }
  84. if p == nil {
  85. return db
  86. }
  87. switch p.DataScope {
  88. //case "2":
  89. // return db.Where(tableName+".create_by in (select sys_user.id from sys_role_dept left join sys_user on sys_user.dept_id=sys_role_dept.dept_id where sys_role_dept.role_id = ?)", p.RoleId)
  90. case "3":
  91. return db.Where(tableName+".create_by in (SELECT id from sys_user where dept_id = ? )", p.DeptId)
  92. case "4":
  93. return db.Where(tableName+".create_by in (SELECT id from sys_user where sys_user.dept_id in (select id from sys_dept where dept_path like ? ))", "%/"+pkg.IntToString(p.DeptId)+"/%")
  94. case "5":
  95. return db.Where(tableName+".create_by = ?", p.UserId)
  96. default:
  97. return db
  98. }
  99. }
  100. }
  101. func getPermissionFromContext(c *gin.Context) *DataPermission {
  102. p := new(DataPermission)
  103. if pm, ok := c.Get(PermissionKey); ok {
  104. switch pm.(type) {
  105. case *DataPermission:
  106. p = pm.(*DataPermission)
  107. }
  108. }
  109. if deptIdStr, err := sdk.Runtime.GetCacheAdapter().Get(model.GetEnterDeptCacheKey(c)); err == nil {
  110. p.DeptId, _ = strconv.Atoi(deptIdStr)
  111. }
  112. return p
  113. }
  114. // GetPermissionFromContext 提供非action写法数据范围约束
  115. func GetPermissionFromContext(c *gin.Context) *DataPermission {
  116. return getPermissionFromContext(c)
  117. }