permission.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package actions
  2. import (
  3. "errors"
  4. "github.com/gin-gonic/gin"
  5. log "github.com/go-admin-team/go-admin-core/logger"
  6. "github.com/go-admin-team/go-admin-core/sdk/config"
  7. "github.com/go-admin-team/go-admin-core/sdk/pkg"
  8. "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
  9. "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
  10. "gorm.io/gorm"
  11. )
  12. type DataPermission struct {
  13. DataScope string
  14. UserId int
  15. DeptId int
  16. RoleId int
  17. }
  18. func PermissionAction() gin.HandlerFunc {
  19. return func(c *gin.Context) {
  20. db, err := pkg.GetOrm(c)
  21. if err != nil {
  22. log.Error(err)
  23. return
  24. }
  25. msgID := pkg.GenerateMsgIDFromContext(c)
  26. var p = new(DataPermission)
  27. if userId := user.GetUserIdStr(c); userId != "" {
  28. p, err = newDataPermission(db, userId)
  29. if err != nil {
  30. log.Errorf("MsgID[%s] PermissionAction error: %s", msgID, err)
  31. response.Error(c, 500, err, "权限范围鉴定错误")
  32. c.Abort()
  33. return
  34. }
  35. }
  36. c.Set(PermissionKey, p)
  37. c.Next()
  38. }
  39. }
  40. func newDataPermission(tx *gorm.DB, userId interface{}) (*DataPermission, error) {
  41. var err error
  42. p := &DataPermission{}
  43. err = tx.Table("sys_user").
  44. Select("sys_user.user_id", "sys_role.role_id", "sys_user.dept_id", "sys_role.data_scope").
  45. Joins("left join sys_role on sys_role.role_id = sys_user.role_id").
  46. Where("sys_user.user_id = ?", userId).
  47. Scan(p).Error
  48. if err != nil {
  49. err = errors.New("获取用户数据出错 msg:" + err.Error())
  50. return nil, err
  51. }
  52. return p, nil
  53. }
  54. func Permission(tableName string, p *DataPermission) func(db *gorm.DB) *gorm.DB {
  55. return func(db *gorm.DB) *gorm.DB {
  56. if !config.ApplicationConfig.EnableDP {
  57. return db
  58. }
  59. switch p.DataScope {
  60. case "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 = ?)", p.RoleId)
  62. case "3":
  63. return db.Where(tableName+".create_by in (SELECT user_id from sys_user where dept_id = ? )", p.DeptId)
  64. case "4":
  65. 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(p.DeptId)+"/%")
  66. case "5":
  67. return db.Where(tableName+".create_by = ?", p.UserId)
  68. default:
  69. return db
  70. }
  71. }
  72. }
  73. func getPermissionFromContext(c *gin.Context) *DataPermission {
  74. p := new(DataPermission)
  75. if pm, ok := c.Get(PermissionKey); ok {
  76. switch pm.(type) {
  77. case *DataPermission:
  78. p = pm.(*DataPermission)
  79. }
  80. }
  81. return p
  82. }
  83. // GetPermissionFromContext 提供非action写法数据范围约束
  84. func GetPermissionFromContext(c *gin.Context) *DataPermission {
  85. return getPermissionFromContext(c)
  86. }