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" "strings" ) func RbacGroupAdd(ctx context.Context, req *pb_v1.RbacGroupAddRequest) (reply *pb_v1.RbacGroupAddReply, err error) { reply = &pb_v1.RbacGroupAddReply{} // 捕获各个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.NodeList == "" || req.Name == "" || req.Uid == 0{ 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{}{ "name":req.Name, } err = p.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } if p.Id > 0 { return nil, status.Error(10003, "角色名已存在") } // 检查node node := model.NewRbacNode(dbname) nodeList := []model.RbacNode{} err = node.QueryAll(database.DB(), nil, &nodeList) if err != nil { return nil, errors.DataBaseError } m := map[string]bool{} for _, v := range nodeList { m[fmt.Sprintf("%d", v.Id)] = true } array := strings.Split(req.NodeList, ",") for _, v := range array { if _, ok := m[v]; !ok { return nil, status.Error(10003, fmt.Sprintf("节点(%s)不存在", v)) } } // 插入数据 p.NodeList = req.NodeList p.Name = req.Name err = p.Insert(database.DB()) if err != nil { return nil, errors.DataBaseError } reply.Id = p.Id return reply, nil }