ApplyGroupDifference will add and remove the user from the specified groups.
(ctx context.Context, tx database.Store, user database.User, add []uuid.UUID, removeIDs []uuid.UUID)
| 261 | |
| 262 | // ApplyGroupDifference will add and remove the user from the specified groups. |
| 263 | func (s AGPLIDPSync) ApplyGroupDifference(ctx context.Context, tx database.Store, user database.User, add []uuid.UUID, removeIDs []uuid.UUID) error { |
| 264 | if len(removeIDs) > 0 { |
| 265 | removedGroupIDs, err := tx.RemoveUserFromGroups(ctx, database.RemoveUserFromGroupsParams{ |
| 266 | UserID: user.ID, |
| 267 | GroupIds: removeIDs, |
| 268 | }) |
| 269 | if err != nil { |
| 270 | return xerrors.Errorf("remove user from %d groups: %w", len(removeIDs), err) |
| 271 | } |
| 272 | if len(removedGroupIDs) != len(removeIDs) { |
| 273 | s.Logger.Debug(ctx, "user not removed from expected number of groups", |
| 274 | slog.F("user_id", user.ID), |
| 275 | slog.F("groups_removed_count", len(removedGroupIDs)), |
| 276 | slog.F("expected_count", len(removeIDs)), |
| 277 | ) |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | if len(add) > 0 { |
| 282 | add = slice.Unique(add) |
| 283 | // Defensive programming to only insert uniques. |
| 284 | assignedGroupIDs, err := tx.InsertUserGroupsByID(ctx, database.InsertUserGroupsByIDParams{ |
| 285 | UserID: user.ID, |
| 286 | GroupIds: add, |
| 287 | }) |
| 288 | if err != nil { |
| 289 | return xerrors.Errorf("insert user into %d groups: %w", len(add), err) |
| 290 | } |
| 291 | if len(assignedGroupIDs) != len(add) { |
| 292 | s.Logger.Debug(ctx, "user not assigned to expected number of groups", |
| 293 | slog.F("user_id", user.ID), |
| 294 | slog.F("groups_assigned_count", len(assignedGroupIDs)), |
| 295 | slog.F("expected_count", len(add)), |
| 296 | ) |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | return nil |
| 301 | } |
| 302 | |
| 303 | type GroupSyncSettings codersdk.GroupSyncSettings |
| 304 |