MCPcopy
hub / github.com/dgraph-io/dgraph / userMod

Function userMod

acl/acl.go:320–391  ·  view source on GitHub ↗
(conf *viper.Viper, userId string, groups string)

Source from the content-addressed store, hash-verified

318}
319
320func userMod(conf *viper.Viper, userId string, groups string) error {
321 dc, cancel, err := getClientWithAdminCtx(conf)
322 if err != nil {
323 return fmt.Errorf("unable to get admin context: %w", err)
324 }
325 defer cancel()
326
327 ctx, ctxCancel := context.WithTimeout(context.Background(), 10*time.Second)
328 defer ctxCancel()
329 txn := dc.NewTxn()
330 defer func() {
331 if err := txn.Discard(ctx); err != nil {
332 fmt.Printf("Unable to discard transaction: %v\n", err)
333 }
334 }()
335
336 user, err := queryUser(ctx, txn, userId)
337 if err != nil {
338 return fmt.Errorf("while querying user: %w", err)
339 }
340 if user == nil {
341 return fmt.Errorf("user %q does not exist", userId)
342 }
343
344 targetGroupsMap := make(map[string]struct{})
345 if len(groups) > 0 {
346 for _, g := range strings.Split(groups, ",") {
347 targetGroupsMap[g] = struct{}{}
348 }
349 }
350
351 existingGroupsMap := make(map[string]struct{})
352 for _, g := range user.Groups {
353 existingGroupsMap[g.GroupID] = struct{}{}
354 }
355 newGroups, groupsToBeDeleted := x.Diff(targetGroupsMap, existingGroupsMap)
356
357 mu := &api.Mutation{
358 CommitNow: true,
359 Set: []*api.NQuad{},
360 Del: []*api.NQuad{},
361 }
362
363 for _, g := range newGroups {
364 fmt.Printf("Adding user %v to group %v\n", userId, g)
365 nquad, err := getUserModNQuad(ctx, txn, user.Uid, g)
366 if err != nil {
367 return err
368 }
369 mu.Set = append(mu.Set, nquad)
370 }
371
372 for _, g := range groupsToBeDeleted {
373 fmt.Printf("Deleting user %v from group %v\n", userId, g)
374 nquad, err := getUserModNQuad(ctx, txn, user.Uid, g)
375 if err != nil {
376 return err
377 }

Callers 1

modFunction · 0.85

Calls 7

DiffFunction · 0.92
getClientWithAdminCtxFunction · 0.85
queryUserFunction · 0.85
getUserModNQuadFunction · 0.85
queryAndPrintUserFunction · 0.85
MutateMethod · 0.65
ErrorfMethod · 0.45

Tested by

no test coverage detected