(conf *viper.Viper, userId string, groups string)
| 318 | } |
| 319 | |
| 320 | func 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 | } |
no test coverage detected