package rbac import ( "context" "cp-organization-management/errors" "cp-organization-management/impl/v1/common" "cp-organization-management/model" pb_v1 "cp-organization-management/pb/v1" "cp-organization-management/utils" "encoding/json" "fmt" "github.com/jaryhe/gopkgs/database" "github.com/jaryhe/gopkgs/logger" "github.com/jinzhu/gorm" "go.uber.org/zap" "google.golang.org/grpc/status" ) func RbacGroupDel(ctx context.Context, req *pb_v1.RbacGroupDelRequest) (reply *pb_v1.RbacGroupDelReply, err error) { reply = &pb_v1.RbacGroupDelReply{} // 捕获各个task中的异常并返回给调用者 defer func() { if r := recover(); r != nil { err = fmt.Errorf("%+v", r) e := &status.Status{} if er := json.Unmarshal([]byte(err.Error()), e); er != nil { logger.Error("err", zap.String("system_err", err.Error()), zap.Stack("stacktrace")) } } }() if req.OrganizationCode == "" || req.Id < 1 || req.Uid < 1{ return nil, errors.ParamsError } dbname := utils.GetDbName(req.OrganizationCode) loginUser, err := common.GetUserBaseInfo(req.Uid, dbname) if err != nil { return nil, err } superGroupId, err := common.GetSuperGroup(dbname) if err != nil { return nil, err } if superGroupId != loginUser.GroupId { return nil, errors.NotSuperGroupError } // 检查原始值 p := model.NewRbacGroup(dbname) where := map[string]interface{}{ "id":req.Id, } err = p.Find(database.DB(), where) if err != nil { if err == gorm.ErrRecordNotFound { return nil, errors.ErrRecordNotFound } return nil, errors.DataBaseError } if p.IsSuperGroup { return nil, status.Error(10003, "超级用户组不能更改") } // 检查是否有用户在使用 user := model.NewRbacUser(dbname) where = map[string]interface{}{ "group_id":req.Id, } count, err := user.Count(database.DB(), where) if err != nil { return nil, errors.DataBaseError } if count > 0 { return nil, status.Error(10003, "有账户绑定该角色") } reply.Origin = &pb_v1.RbacGroupUpdateRequest{} // 原始值用于记录操作日志 reply.Origin.Id = req.Id reply.Origin.Name = p.Name reply.Origin.NodeList = p.NodeList reply.Origin.OrganizationCode = req.OrganizationCode // 更新数据 err = p.Delete(database.DB(), where) if err != nil { return nil, errors.DataBaseError } return reply, nil }