upsertGroot must be called after setting the namespace in the context.
(ctx context.Context, passwd string)
| 529 | |
| 530 | // upsertGroot must be called after setting the namespace in the context. |
| 531 | func upsertGroot(ctx context.Context, passwd string) error { |
| 532 | // groot is the default user of guardians group. |
| 533 | query := fmt.Sprintf(` |
| 534 | { |
| 535 | grootid as grootUser(func: eq(dgraph.xid, "%s")) @filter(type(dgraph.type.User)) { |
| 536 | uid |
| 537 | } |
| 538 | guid as var(func: eq(dgraph.xid, "%s")) @filter(type(dgraph.type.Group)) |
| 539 | } |
| 540 | `, x.GrootId, x.SuperAdminId) |
| 541 | userNQuads := acl.CreateUserNQuads(x.GrootId, passwd) |
| 542 | userNQuads = append(userNQuads, &api.NQuad{ |
| 543 | Subject: "_:newuser", |
| 544 | Predicate: "dgraph.user.group", |
| 545 | ObjectId: "uid(guid)", |
| 546 | }) |
| 547 | req := &Request{ |
| 548 | req: &api.Request{ |
| 549 | CommitNow: true, |
| 550 | Query: query, |
| 551 | Mutations: []*api.Mutation{ |
| 552 | { |
| 553 | Set: userNQuads, |
| 554 | // Assuming that if groot exists, it is in guardian group |
| 555 | Cond: "@if(eq(len(grootid), 0) and gt(len(guid), 0))", |
| 556 | }, |
| 557 | }, |
| 558 | }, |
| 559 | doAuth: NoAuthorize, |
| 560 | } |
| 561 | |
| 562 | resp, err := (&Server{}).doQuery(ctx, req) |
| 563 | if err != nil { |
| 564 | return errors.Wrapf(err, "while upserting user with id %s", x.GrootId) |
| 565 | } |
| 566 | |
| 567 | // Structs to parse groot user uid from query response |
| 568 | type userNode struct { |
| 569 | Uid string `json:"uid"` |
| 570 | } |
| 571 | |
| 572 | type userQryResp struct { |
| 573 | GrootUser []userNode `json:"grootUser"` |
| 574 | } |
| 575 | |
| 576 | var grootUserUid string |
| 577 | var userResp userQryResp |
| 578 | if err := json.Unmarshal(resp.GetJson(), &userResp); err != nil { |
| 579 | return errors.Wrap(err, "Couldn't unmarshal response from groot user query") |
| 580 | } |
| 581 | if len(userResp.GrootUser) == 0 { |
| 582 | // no groot user found from query |
| 583 | // Extract uid of created groot user from mutation |
| 584 | newUserUidMap := resp.GetUids() |
| 585 | grootUserUid = newUserUidMap["newuser"] |
| 586 | } else if len(userResp.GrootUser) == 1 { |
| 587 | // we found a groot user |
| 588 | grootUserUid = userResp.GrootUser[0].Uid |
no test coverage detected