datascope.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package models
  2. import (
  3. "errors"
  4. "github.com/go-admin-team/go-admin-core/sdk/pkg"
  5. "gorm.io/gorm"
  6. log "github.com/go-admin-team/go-admin-core/logger"
  7. "github.com/go-admin-team/go-admin-core/sdk/config"
  8. )
  9. type DataPermission struct {
  10. DataScope string
  11. UserId int
  12. DeptId int
  13. RoleId int
  14. }
  15. func (e *DataPermission) GetDataScope(tableName string, db *gorm.DB) (*gorm.DB, error) {
  16. if !config.ApplicationConfig.EnableDP {
  17. usageStr := `数据权限已经为您` + pkg.Green(`关闭`) + `,如需开启请参考配置文件字段说明`
  18. log.Debug("%s\n", usageStr)
  19. return db, nil
  20. }
  21. user := new(SysUser)
  22. role := new(SysRole)
  23. err := db.Find(user, e.UserId).Error
  24. if err != nil {
  25. return nil, errors.New("获取用户数据出错 msg:" + err.Error())
  26. }
  27. err = db.Find(role, user.RoleId).Error
  28. if err != nil {
  29. return nil, errors.New("获取用户数据出错 msg:" + err.Error())
  30. }
  31. if role.DataScope == "2" {
  32. db = db.Where(tableName+".create_by in (select sys_user.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 = ?)", user.RoleId)
  33. }
  34. if role.DataScope == "3" {
  35. db = db.Where(tableName+".create_by in (SELECT user_id from sys_user where dept_id = ? )", user.DeptId)
  36. }
  37. if role.DataScope == "4" {
  38. db = db.Where(tableName+".create_by in (SELECT user_id from sys_user where sys_user.dept_id in(select dept_id from sys_dept where dept_path like ? ))", "%"+pkg.IntToString(user.DeptId)+"%")
  39. }
  40. if role.DataScope == "5" || role.DataScope == "" {
  41. db = db.Where(tableName+".create_by = ?", e.UserId)
  42. }
  43. return db, nil
  44. }
  45. //func DataScopes(tableName string, userId int) func(db *gorm.DB) *gorm.DB {
  46. // return func(db *gorm.DB) *gorm.DB {
  47. // user := new(SysUser)
  48. // role := new(SysRole)
  49. // user.UserId = userId
  50. // err := db.Find(user, userId).Error
  51. // if err != nil {
  52. // db.Error = errors.New("获取用户数据出错 msg:" + err.Error())
  53. // return db
  54. // }
  55. // err = db.Find(role, user.RoleId).Error
  56. // if err != nil {
  57. // db.Error = errors.New("获取用户数据出错 msg:" + err.Error())
  58. // return db
  59. // }
  60. // if role.DataScope == "2" {
  61. // return db.Where(tableName+".create_by in (select sys_user.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 = ?)", user.RoleId)
  62. // }
  63. // if role.DataScope == "3" {
  64. // return db.Where(tableName+".create_by in (SELECT user_id from sys_user where dept_id = ? )", user.DeptId)
  65. // }
  66. // if role.DataScope == "4" {
  67. // return db.Where(tableName+".create_by in (SELECT user_id from sys_user where sys_user.dept_id in(select dept_id from sys_dept where dept_path like ? ))", "%"+pkg.IntToString(user.DeptId)+"%")
  68. // }
  69. // if role.DataScope == "5" || role.DataScope == "" {
  70. // return db.Where(tableName+".create_by = ?", userId)
  71. // }
  72. // return db
  73. // }
  74. //}