1
0
mirror of https://github.com/bitwarden/server.git synced 2025-06-30 15:42:48 -05:00

Check canScale when scaling for sso (#1661)

* Check canScale when scaling for sso

* PR review

Use AutoAddSeats to add seats in a consistent way.
This requires moving user check out of that method.

* User logic moved out of method
This commit is contained in:
Matt Gibson
2021-10-25 10:19:37 -05:00
committed by GitHub
parent c5d5601464
commit 8f0115e62f
4 changed files with 14 additions and 31 deletions

View File

@ -18,6 +18,7 @@ namespace Bit.Core.Services
Task<Tuple<bool, string>> UpgradePlanAsync(Guid organizationId, OrganizationUpgrade upgrade);
Task<string> AdjustStorageAsync(Guid organizationId, short storageAdjustmentGb);
Task UpdateSubscription(Guid organizationId, int seatAdjustment, int? maxAutoscaleSeats);
Task AutoAddSeatsAsync(Organization organization, int seatsToAdd, DateTime? prorationDate = null);
Task<string> AdjustSeatsAsync(Guid organizationId, int seatAdjustment, DateTime? prorationDate = null);
Task VerifyBankAsync(Guid organizationId, int amount1, int amount2);
Task<Tuple<Organization, OrganizationUser>> SignUpAsync(OrganizationSignup organizationSignup, bool provider = false);

View File

@ -1074,7 +1074,7 @@ namespace Bit.Core.Services
if (newSeatsRequired > 0)
{
var (canScale, failureReason) = await CanScaleAsync(organization, newSeatsRequired);
var (canScale, failureReason) = CanScale(organization, newSeatsRequired);
if (!canScale)
{
throw new BadRequestException(failureReason);
@ -1160,6 +1160,11 @@ namespace Bit.Core.Services
await _organizationUserRepository.CreateAsync(orgUser, collections);
}
if (!await _currentContext.ManageUsers(organization.Id))
{
throw new BadRequestException("Cannot add seats. Cannot manage organization users.");
}
await AutoAddSeatsAsync(organization, newSeatsRequired, prorationDate);
await SendInvitesAsync(orgUsers, organization);
await _eventService.LogOrganizationUserEventsAsync(events);
@ -1454,7 +1459,8 @@ namespace Bit.Core.Services
return result;
}
internal async Task<(bool canScale, string failureReason)> CanScaleAsync(Organization organization, int seatsToAdd)
internal (bool canScale, string failureReason) CanScale(Organization organization,
int seatsToAdd)
{
var failureReason = "";
if (_globalSettings.SelfHosted)
@ -1463,12 +1469,6 @@ namespace Bit.Core.Services
return (false, failureReason);
}
if (!await _currentContext.ManageUsers(organization.Id))
{
failureReason = "Cannot manage organization users.";
return (false, failureReason);
}
if (seatsToAdd < 1)
{
return (true, failureReason);
@ -1484,14 +1484,14 @@ namespace Bit.Core.Services
return (true, failureReason);
}
private async Task AutoAddSeatsAsync(Organization organization, int seatsToAdd, DateTime? prorationDate = null)
public async Task AutoAddSeatsAsync(Organization organization, int seatsToAdd, DateTime? prorationDate = null)
{
if (seatsToAdd < 1 || !organization.Seats.HasValue)
{
return;
}
var (canScale, failureMessage) = await CanScaleAsync(organization, seatsToAdd);
var (canScale, failureMessage) = CanScale(organization, seatsToAdd);
if (!canScale)
{
throw new BadRequestException(failureMessage);