permission.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package actions
  2. import (
  3. "errors"
  4. "net/http"
  5. "github.com/gin-gonic/gin"
  6. "gorm.io/gorm"
  7. "device-manage/common/log"
  8. "device-manage/tools"
  9. "device-manage/tools/app"
  10. "device-manage/tools/config"
  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 := tools.GetOrm(c)
  21. if err != nil {
  22. log.Error(err)
  23. return
  24. }
  25. msgID := tools.GenerateMsgIDFromContext(c)
  26. var p = new(DataPermission)
  27. if userId := tools.GetUserIdStr(c); userId != "" {
  28. p, err = newDataPermission(db, userId)
  29. if err != nil {
  30. log.Errorf("MsgID[%s] PermissionAction error: %s", msgID, err)
  31. app.Error(c, http.StatusInternalServerError, 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 ? ))", "%"+tools.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. // PermissionForNoAction 提供非action写法数据范围约束
  84. func GetPermissionFromContext(c *gin.Context) *DataPermission {
  85. return getPermissionFromContext(c)
  86. }