mirror of
https://github.com/bitwarden/server
synced 2025-12-28 14:13:48 +00:00
* Add feature flag * Promoted the new Entiy Framework properties * Deprecate the old property * Update references * Fix mispelling * Re-add contextual comment regarding dropped license properties * Add back deleted assertion for deprecated property * Add back removed fixture property assignment * Improve feature toggling scenerios for self hosted org creation/update * Unblock `PutCollectionManagement` for self host * Simplify logic of a couple of conditionals * Feature toggle route unblocking * Adjust logic collection creation/deletion authorization handler * Create tests * Fix bug caught by tests * Fix bugs caught during manual testing * Remove remark about license
71 lines
2.7 KiB
C#
71 lines
2.7 KiB
C#
#nullable enable
|
|
|
|
using System.Text.Json;
|
|
using Bit.Core.AdminConsole.Entities;
|
|
using Bit.Core.Exceptions;
|
|
using Bit.Core.Models.Business;
|
|
using Bit.Core.Models.Data.Organizations;
|
|
using Bit.Core.OrganizationFeatures.OrganizationLicenses.Interfaces;
|
|
using Bit.Core.Services;
|
|
using Bit.Core.Settings;
|
|
using Bit.Core.Utilities;
|
|
|
|
namespace Bit.Core.OrganizationFeatures.OrganizationLicenses;
|
|
|
|
public class UpdateOrganizationLicenseCommand : IUpdateOrganizationLicenseCommand
|
|
{
|
|
private readonly ILicensingService _licensingService;
|
|
private readonly IGlobalSettings _globalSettings;
|
|
private readonly IOrganizationService _organizationService;
|
|
private readonly IFeatureService _featureService;
|
|
|
|
public UpdateOrganizationLicenseCommand(
|
|
ILicensingService licensingService,
|
|
IGlobalSettings globalSettings,
|
|
IOrganizationService organizationService,
|
|
IFeatureService featureService)
|
|
{
|
|
_licensingService = licensingService;
|
|
_globalSettings = globalSettings;
|
|
_organizationService = organizationService;
|
|
_featureService = featureService;
|
|
}
|
|
|
|
public async Task UpdateLicenseAsync(SelfHostedOrganizationDetails selfHostedOrganization,
|
|
OrganizationLicense license, Organization? currentOrganizationUsingLicenseKey)
|
|
{
|
|
if (currentOrganizationUsingLicenseKey != null && currentOrganizationUsingLicenseKey.Id != selfHostedOrganization.Id)
|
|
{
|
|
throw new BadRequestException("License is already in use by another organization.");
|
|
}
|
|
|
|
var canUse = license.CanUse(_globalSettings, _licensingService, out var exception) &&
|
|
selfHostedOrganization.CanUseLicense(license, out exception);
|
|
|
|
if (!canUse)
|
|
{
|
|
throw new BadRequestException(exception);
|
|
}
|
|
|
|
await WriteLicenseFileAsync(selfHostedOrganization, license);
|
|
await UpdateOrganizationAsync(selfHostedOrganization, license);
|
|
}
|
|
|
|
private async Task WriteLicenseFileAsync(Organization organization, OrganizationLicense license)
|
|
{
|
|
var dir = $"{_globalSettings.LicenseDirectory}/organization";
|
|
Directory.CreateDirectory(dir);
|
|
await using var fs = new FileStream(Path.Combine(dir, $"{organization.Id}.json"), FileMode.Create);
|
|
await JsonSerializer.SerializeAsync(fs, license, JsonHelpers.Indented);
|
|
}
|
|
|
|
private async Task UpdateOrganizationAsync(SelfHostedOrganizationDetails selfHostedOrganizationDetails, OrganizationLicense license)
|
|
{
|
|
var organization = selfHostedOrganizationDetails.ToOrganization();
|
|
|
|
organization.UpdateFromLicense(license, _featureService);
|
|
|
|
await _organizationService.ReplaceAndUpdateCacheAsync(organization);
|
|
}
|
|
}
|