user_del.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package user
  2. import (
  3. "context"
  4. "cp-organization-management/errors"
  5. "cp-organization-management/impl/v1/common"
  6. "cp-organization-management/model"
  7. pb_v1 "cp-organization-management/pb/v1"
  8. "cp-organization-management/utils"
  9. "encoding/json"
  10. "fmt"
  11. "github.com/jaryhe/gopkgs/database"
  12. "github.com/jaryhe/gopkgs/logger"
  13. "go.uber.org/zap"
  14. "google.golang.org/grpc/status"
  15. )
  16. func UserPermissionCheck(loginUserInfo, targetUserInfo *model.RbacUser, dbname string) error {
  17. // 获取登录用户的区域
  18. loginUserIsSuper, loginTopLevel, loginUserSubZone, _, err := common.GetUserTopSubZone(loginUserInfo.Id, dbname)
  19. if err != nil {
  20. return err
  21. }
  22. // 超级用户可以任意操作
  23. if loginUserIsSuper {
  24. return nil
  25. }
  26. // 获取目标用户的区域
  27. targetUserIsSuper, targetUserTopLevel, _, targetUserAllZone, err := common.GetUserTopSubZone(targetUserInfo.Id, dbname)
  28. if err != nil {
  29. return err
  30. }
  31. // 不能对超级用户操作
  32. if targetUserIsSuper {
  33. return errors.SuperError
  34. }
  35. // 有父子关系
  36. pids := fmt.Sprintf("%s%d,", loginUserInfo.Pids, loginUserInfo.Id)
  37. if len(targetUserInfo.Pids) >= len(pids) && targetUserInfo.Pids[:len(pids)] == pids {
  38. return nil
  39. }
  40. // 检查目标用户是否在登录用户的管辖区域内
  41. if loginTopLevel >= targetUserTopLevel {
  42. return errors.UserNotInRightZone
  43. }
  44. for k, _ := range targetUserAllZone {
  45. if _, ok := loginUserSubZone[k]; ok {
  46. return nil
  47. }
  48. }
  49. return errors.UserNotInRightZone
  50. }
  51. func UserDel(ctx context.Context, req *pb_v1.UserDelRequest)(reply *pb_v1.UserDelReply, err error) {
  52. reply = &pb_v1.UserDelReply{}
  53. defer func() {
  54. if r := recover(); r != nil {
  55. err = fmt.Errorf("%+v", r)
  56. e := &status.Status{}
  57. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  58. logger.Error("err",
  59. zap.String("system_err", err.Error()),
  60. zap.Stack("stacktrace"))
  61. }
  62. }
  63. }()
  64. if req.Uid == 0 || req.Id == 0 || req.OrganizationCode == "" {
  65. return nil, errors.ParamsError
  66. }
  67. dbname := utils.GetDbName(req.OrganizationCode)
  68. loginUserInfo, err := common.GetUserBaseInfo(req.Uid, dbname)
  69. if err != nil {
  70. return nil, err
  71. }
  72. targetUserInfo, err := common.GetUserBaseInfo(req.Id, dbname)
  73. if err != nil {
  74. return nil, err
  75. }
  76. err = UserPermissionCheck(loginUserInfo, targetUserInfo, dbname)
  77. if err != nil {
  78. return nil, err
  79. }
  80. db := database.DB().Begin()
  81. p := model.NewRbacUser(dbname)
  82. where := map[string]interface{}{
  83. "id":req.Id,
  84. }
  85. err = p.Delete(db, where)
  86. if err != nil {
  87. db.Rollback()
  88. return nil, errors.DataBaseError
  89. }
  90. ug := model.NewUserZone(dbname)
  91. where = map[string]interface{}{
  92. "user_id":req.Id,
  93. }
  94. err = ug.Delete(database.DB(), where)
  95. if err != nil {
  96. db.Rollback()
  97. return nil, errors.DataBaseError
  98. }
  99. if err := common.DelUserBaseInfo(req.Id, dbname); err != nil {
  100. db.Rollback()
  101. return nil, err
  102. }
  103. if err := common.DelUserZone(req.Id, dbname); err != nil {
  104. db.Rollback()
  105. return nil, err
  106. }
  107. db.Commit()
  108. reply.Id = targetUserInfo.Id
  109. reply.Email = targetUserInfo.Email
  110. reply.Username = targetUserInfo.Username
  111. reply.Name = targetUserInfo.Name
  112. reply.Phone = targetUserInfo.Phone
  113. return reply, nil
  114. }