|
| 1 | +#nullable enable |
| 2 | +using Bit.Admin.Billing.Models; |
| 3 | +using Bit.Admin.Enums; |
| 4 | +using Bit.Admin.Utilities; |
| 5 | +using Bit.Core.AdminConsole.Entities; |
| 6 | +using Bit.Core.AdminConsole.Entities.Provider; |
| 7 | +using Bit.Core.AdminConsole.Enums.Provider; |
| 8 | +using Bit.Core.AdminConsole.Repositories; |
| 9 | +using Bit.Core.Billing.Services; |
| 10 | +using Bit.Core.Exceptions; |
| 11 | +using Bit.Core.Repositories; |
| 12 | +using Bit.Core.Utilities; |
| 13 | +using Microsoft.AspNetCore.Authorization; |
| 14 | +using Microsoft.AspNetCore.Mvc; |
| 15 | + |
| 16 | +namespace Bit.Admin.Billing.Controllers; |
| 17 | + |
| 18 | +[Authorize] |
| 19 | +[Route("organizations/billing/{organizationId:guid}/business-unit")] |
| 20 | +public class BusinessUnitConversionController( |
| 21 | + IBusinessUnitConverter businessUnitConverter, |
| 22 | + IOrganizationRepository organizationRepository, |
| 23 | + IProviderRepository providerRepository, |
| 24 | + IProviderUserRepository providerUserRepository) : Controller |
| 25 | +{ |
| 26 | + [HttpGet] |
| 27 | + [RequirePermission(Permission.Org_Billing_ConvertToBusinessUnit)] |
| 28 | + [SelfHosted(NotSelfHostedOnly = true)] |
| 29 | + public async Task<IActionResult> IndexAsync([FromRoute] Guid organizationId) |
| 30 | + { |
| 31 | + var organization = await organizationRepository.GetByIdAsync(organizationId); |
| 32 | + |
| 33 | + if (organization == null) |
| 34 | + { |
| 35 | + throw new NotFoundException(); |
| 36 | + } |
| 37 | + |
| 38 | + var model = new BusinessUnitConversionModel { Organization = organization }; |
| 39 | + |
| 40 | + var invitedProviderAdmin = await GetInvitedProviderAdminAsync(organization); |
| 41 | + |
| 42 | + if (invitedProviderAdmin != null) |
| 43 | + { |
| 44 | + model.ProviderAdminEmail = invitedProviderAdmin.Email; |
| 45 | + model.ProviderId = invitedProviderAdmin.ProviderId; |
| 46 | + } |
| 47 | + |
| 48 | + var success = ReadSuccessMessage(); |
| 49 | + |
| 50 | + if (!string.IsNullOrEmpty(success)) |
| 51 | + { |
| 52 | + model.Success = success; |
| 53 | + } |
| 54 | + |
| 55 | + var errors = ReadErrorMessages(); |
| 56 | + |
| 57 | + if (errors is { Count: > 0 }) |
| 58 | + { |
| 59 | + model.Errors = errors; |
| 60 | + } |
| 61 | + |
| 62 | + return View(model); |
| 63 | + } |
| 64 | + |
| 65 | + [HttpPost] |
| 66 | + [RequirePermission(Permission.Org_Billing_ConvertToBusinessUnit)] |
| 67 | + [SelfHosted(NotSelfHostedOnly = true)] |
| 68 | + public async Task<IActionResult> InitiateAsync( |
| 69 | + [FromRoute] Guid organizationId, |
| 70 | + BusinessUnitConversionModel model) |
| 71 | + { |
| 72 | + var organization = await organizationRepository.GetByIdAsync(organizationId); |
| 73 | + |
| 74 | + if (organization == null) |
| 75 | + { |
| 76 | + throw new NotFoundException(); |
| 77 | + } |
| 78 | + |
| 79 | + var result = await businessUnitConverter.InitiateConversion( |
| 80 | + organization, |
| 81 | + model.ProviderAdminEmail!); |
| 82 | + |
| 83 | + return result.Match( |
| 84 | + providerId => RedirectToAction("Edit", "Providers", new { id = providerId }), |
| 85 | + errors => |
| 86 | + { |
| 87 | + PersistErrorMessages(errors); |
| 88 | + return RedirectToAction("Index", new { organizationId }); |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + [HttpPost("reset")] |
| 93 | + [RequirePermission(Permission.Org_Billing_ConvertToBusinessUnit)] |
| 94 | + [SelfHosted(NotSelfHostedOnly = true)] |
| 95 | + public async Task<IActionResult> ResetAsync( |
| 96 | + [FromRoute] Guid organizationId, |
| 97 | + BusinessUnitConversionModel model) |
| 98 | + { |
| 99 | + var organization = await organizationRepository.GetByIdAsync(organizationId); |
| 100 | + |
| 101 | + if (organization == null) |
| 102 | + { |
| 103 | + throw new NotFoundException(); |
| 104 | + } |
| 105 | + |
| 106 | + await businessUnitConverter.ResetConversion(organization, model.ProviderAdminEmail!); |
| 107 | + |
| 108 | + PersistSuccessMessage("Business unit conversion was successfully reset."); |
| 109 | + |
| 110 | + return RedirectToAction("Index", new { organizationId }); |
| 111 | + } |
| 112 | + |
| 113 | + [HttpPost("resend-invite")] |
| 114 | + [RequirePermission(Permission.Org_Billing_ConvertToBusinessUnit)] |
| 115 | + [SelfHosted(NotSelfHostedOnly = true)] |
| 116 | + public async Task<IActionResult> ResendInviteAsync( |
| 117 | + [FromRoute] Guid organizationId, |
| 118 | + BusinessUnitConversionModel model) |
| 119 | + { |
| 120 | + var organization = await organizationRepository.GetByIdAsync(organizationId); |
| 121 | + |
| 122 | + if (organization == null) |
| 123 | + { |
| 124 | + throw new NotFoundException(); |
| 125 | + } |
| 126 | + |
| 127 | + await businessUnitConverter.ResendConversionInvite(organization, model.ProviderAdminEmail!); |
| 128 | + |
| 129 | + PersistSuccessMessage($"Invite was successfully resent to {model.ProviderAdminEmail}."); |
| 130 | + |
| 131 | + return RedirectToAction("Index", new { organizationId }); |
| 132 | + } |
| 133 | + |
| 134 | + private async Task<ProviderUser?> GetInvitedProviderAdminAsync( |
| 135 | + Organization organization) |
| 136 | + { |
| 137 | + var provider = await providerRepository.GetByOrganizationIdAsync(organization.Id); |
| 138 | + |
| 139 | + if (provider is not |
| 140 | + { |
| 141 | + Type: ProviderType.BusinessUnit, |
| 142 | + Status: ProviderStatusType.Pending |
| 143 | + }) |
| 144 | + { |
| 145 | + return null; |
| 146 | + } |
| 147 | + |
| 148 | + var providerUsers = |
| 149 | + await providerUserRepository.GetManyByProviderAsync(provider.Id, ProviderUserType.ProviderAdmin); |
| 150 | + |
| 151 | + if (providerUsers.Count != 1) |
| 152 | + { |
| 153 | + return null; |
| 154 | + } |
| 155 | + |
| 156 | + var providerUser = providerUsers.First(); |
| 157 | + |
| 158 | + return providerUser is |
| 159 | + { |
| 160 | + Type: ProviderUserType.ProviderAdmin, |
| 161 | + Status: ProviderUserStatusType.Invited, |
| 162 | + UserId: not null |
| 163 | + } ? providerUser : null; |
| 164 | + } |
| 165 | + |
| 166 | + private const string _errors = "errors"; |
| 167 | + private const string _success = "Success"; |
| 168 | + |
| 169 | + private void PersistSuccessMessage(string message) => TempData[_success] = message; |
| 170 | + private void PersistErrorMessages(List<string> errors) |
| 171 | + { |
| 172 | + var input = string.Join("|", errors); |
| 173 | + TempData[_errors] = input; |
| 174 | + } |
| 175 | + private string? ReadSuccessMessage() => ReadTempData<string>(_success); |
| 176 | + private List<string>? ReadErrorMessages() |
| 177 | + { |
| 178 | + var output = ReadTempData<string>(_errors); |
| 179 | + return string.IsNullOrEmpty(output) ? null : output.Split('|').ToList(); |
| 180 | + } |
| 181 | + |
| 182 | + private T? ReadTempData<T>(string key) => TempData.TryGetValue(key, out var obj) && obj is T value ? value : default; |
| 183 | +} |
0 commit comments