{"record":{"id":"7e7ebe683787ea19","repo":"bitwarden/server","slug":"organization-already-belongs-to-a-provider","errorCode":null,"errorMessage":"Organization already belongs to a provider.","messagePattern":"Organization already belongs to a provider\\.","errorType":"exception","errorClass":"BadRequestException","httpStatus":400,"severity":"error","filePath":"bitwarden_license/src/Commercial.Core/AdminConsole/Services/ProviderService.cs","lineNumber":438,"sourceCode":"            catch (BadRequestException e)\n            {\n                result.Add(Tuple.Create(providerUser, e.Message));\n            }\n\n            await _providerUserRepository.DeleteManyAsync(deletedUserIds);\n        }\n\n        await _eventService.LogProviderUsersEventAsync(events);\n\n        return result;\n    }\n\n    public async Task AddOrganization(Guid providerId, Guid organizationId, string key)\n    {\n        var po = await _providerOrganizationRepository.GetByOrganizationId(organizationId);\n        if (po != null)\n        {\n            throw new BadRequestException(\"Organization already belongs to a provider.\");\n        }\n\n        var organization = await _organizationRepository.GetByIdAsync(organizationId);\n\n        var provider = await _providerRepository.GetByIdAsync(providerId);\n\n        ThrowOnInvalidPlanType(provider.Type, organization.PlanType);\n\n        if (organization.UseSecretsManager)\n        {\n            throw new BadRequestException(\n                \"The organization is subscribed to Secrets Manager. Please contact Customer Support to manage the subscription.\");\n        }\n\n        var providerOrganization = new ProviderOrganization\n        {\n            ProviderId = providerId,\n            OrganizationId = organizationId,","sourceCodeStart":420,"sourceCodeEnd":456,"githubUrl":"https://github.com/bitwarden/server/blob/e93b962371d80964556f5590c6615f5160a437a1/bitwarden_license/src/Commercial.Core/AdminConsole/Services/ProviderService.cs#L420-L456","documentation":"Thrown by ProviderService.AddOrganization when a ProviderOrganization already exists for the given organizationId (GetByOrganizationId returns non-null). An organization can only belong to one provider at a time. BadRequestException (HTTP 400).","triggerScenarios":"Calling AddOrganization(providerId, organizationId, key) for an organization that is already linked to any provider (possibly the same or a different one).","commonSituations":"Re-adding an org that was moved but whose prior link wasn't cleared; an admin retrying a partially-completed add; an org already managed by a reseller being added to an MSP.","solutions":["Before adding, call GetByOrganizationId(organizationId) and confirm it is null.","If reassigning providers, remove the existing ProviderOrganization first, then add.","Check the UI: if the org shows a current provider, route the user to transfer/flow rather than a plain add."],"exampleFix":"// before\nawait providerService.AddOrganization(providerId, orgId, key);\n\n// after\nvar existing = await _providerOrganizationRepository.GetByOrganizationId(orgId);\nif (existing != null)\n{\n    throw new ConflictException($\"Org already on provider {existing.ProviderId}.\");\n}\nawait providerService.AddOrganization(providerId, orgId, key);","handlingStrategy":"validation","validationCode":"var existing = await providerOrganizationRepository.GetByOrganizationId(organizationId);\nif (existing != null) throw new ConflictException(\"Already linked.\");","typeGuard":null,"tryCatchPattern":"try { await providerService.AddOrganization(providerId, orgId, key); }\ncatch (BadRequestException ex) when (ex.Message.Contains(\"already belongs\")) { /* prompt transfer flow */ }","preventionTips":["Check GetByOrganizationId before adding.","Remove the existing provider link when transferring providers."],"tags":["provider","organization","uniqueness","bad-request"],"backgroundTag":null,"analyzedSha":"e93b962371d80964556f5590c6615f5160a437a1","analyzedAt":"2026-08-13T14:22:19.382Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}