del_group.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package rbac
  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. "github.com/jinzhu/gorm"
  14. "go.uber.org/zap"
  15. "google.golang.org/grpc/status"
  16. )
  17. func RbacGroupDel(ctx context.Context, req *pb_v1.RbacGroupDelRequest) (reply *pb_v1.RbacGroupDelReply, err error) {
  18. reply = &pb_v1.RbacGroupDelReply{}
  19. // 捕获各个task中的异常并返回给调用者
  20. defer func() {
  21. if r := recover(); r != nil {
  22. err = fmt.Errorf("%+v", r)
  23. e := &status.Status{}
  24. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  25. logger.Error("err",
  26. zap.String("system_err", err.Error()),
  27. zap.Stack("stacktrace"))
  28. }
  29. }
  30. }()
  31. if req.OrganizationCode == "" || req.Id < 1 || req.Uid < 1{
  32. return nil, errors.ParamsError
  33. }
  34. dbname := utils.GetDbName(req.OrganizationCode)
  35. loginUser, err := common.GetUserBaseInfo(req.Uid, dbname)
  36. if err != nil {
  37. return nil, err
  38. }
  39. superGroupId, err := common.GetSuperGroup(dbname)
  40. if err != nil {
  41. return nil, err
  42. }
  43. if superGroupId != loginUser.GroupId {
  44. return nil, errors.NotSuperGroupError
  45. }
  46. // 检查原始值
  47. p := model.NewRbacGroup(dbname)
  48. where := map[string]interface{}{
  49. "id":req.Id,
  50. }
  51. err = p.Find(database.DB(), where)
  52. if err != nil {
  53. if err == gorm.ErrRecordNotFound {
  54. return nil, errors.ErrRecordNotFound
  55. }
  56. return nil, errors.DataBaseError
  57. }
  58. if p.IsSuperGroup {
  59. return nil, status.Error(10003, "超级用户组不能更改")
  60. }
  61. // 检查是否有用户在使用
  62. user := model.NewRbacUser(dbname)
  63. where = map[string]interface{}{
  64. "group_id":req.Id,
  65. }
  66. count, err := user.Count(database.DB(), where)
  67. if err != nil {
  68. return nil, errors.DataBaseError
  69. }
  70. if count > 0 {
  71. return nil, status.Error(10003, "有账户绑定该角色")
  72. }
  73. reply.Origin = &pb_v1.RbacGroupUpdateRequest{}
  74. // 原始值用于记录操作日志
  75. reply.Origin.Id = req.Id
  76. reply.Origin.Name = p.Name
  77. reply.Origin.NodeList = p.NodeList
  78. reply.Origin.OrganizationCode = req.OrganizationCode
  79. // 更新数据
  80. err = p.Delete(database.DB(), where)
  81. if err != nil {
  82. return nil, errors.DataBaseError
  83. }
  84. return reply, nil
  85. }