mirror of
https://github.com/bitwarden/server
synced 2025-12-24 20:23:21 +00:00
Revert filescoped (#2227)
* Revert "Add git blame entry (#2226)" This reverts commit239286737d. * Revert "Turn on file scoped namespaces (#2225)" This reverts commit34fb4cca2a.
This commit is contained in:
@@ -4,42 +4,43 @@ using Bit.Core.OrganizationFeatures.OrganizationApiKeys.Interfaces;
|
||||
using Bit.Core.Repositories;
|
||||
using Bit.Core.Utilities;
|
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationApiKeys;
|
||||
|
||||
public class GetOrganizationApiKeyCommand : IGetOrganizationApiKeyCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationApiKeys
|
||||
{
|
||||
private readonly IOrganizationApiKeyRepository _organizationApiKeyRepository;
|
||||
|
||||
public GetOrganizationApiKeyCommand(IOrganizationApiKeyRepository organizationApiKeyRepository)
|
||||
public class GetOrganizationApiKeyCommand : IGetOrganizationApiKeyCommand
|
||||
{
|
||||
_organizationApiKeyRepository = organizationApiKeyRepository;
|
||||
}
|
||||
private readonly IOrganizationApiKeyRepository _organizationApiKeyRepository;
|
||||
|
||||
public async Task<OrganizationApiKey> GetOrganizationApiKeyAsync(Guid organizationId, OrganizationApiKeyType organizationApiKeyType)
|
||||
{
|
||||
if (!Enum.IsDefined(organizationApiKeyType))
|
||||
public GetOrganizationApiKeyCommand(IOrganizationApiKeyRepository organizationApiKeyRepository)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(organizationApiKeyType), $"Invalid value for enum {nameof(OrganizationApiKeyType)}");
|
||||
_organizationApiKeyRepository = organizationApiKeyRepository;
|
||||
}
|
||||
|
||||
var apiKeys = await _organizationApiKeyRepository
|
||||
.GetManyByOrganizationIdTypeAsync(organizationId, organizationApiKeyType);
|
||||
|
||||
if (apiKeys == null || !apiKeys.Any())
|
||||
public async Task<OrganizationApiKey> GetOrganizationApiKeyAsync(Guid organizationId, OrganizationApiKeyType organizationApiKeyType)
|
||||
{
|
||||
var apiKey = new OrganizationApiKey
|
||||
if (!Enum.IsDefined(organizationApiKeyType))
|
||||
{
|
||||
OrganizationId = organizationId,
|
||||
Type = organizationApiKeyType,
|
||||
ApiKey = CoreHelpers.SecureRandomString(30),
|
||||
RevisionDate = DateTime.UtcNow,
|
||||
};
|
||||
throw new ArgumentOutOfRangeException(nameof(organizationApiKeyType), $"Invalid value for enum {nameof(OrganizationApiKeyType)}");
|
||||
}
|
||||
|
||||
await _organizationApiKeyRepository.CreateAsync(apiKey);
|
||||
return apiKey;
|
||||
var apiKeys = await _organizationApiKeyRepository
|
||||
.GetManyByOrganizationIdTypeAsync(organizationId, organizationApiKeyType);
|
||||
|
||||
if (apiKeys == null || !apiKeys.Any())
|
||||
{
|
||||
var apiKey = new OrganizationApiKey
|
||||
{
|
||||
OrganizationId = organizationId,
|
||||
Type = organizationApiKeyType,
|
||||
ApiKey = CoreHelpers.SecureRandomString(30),
|
||||
RevisionDate = DateTime.UtcNow,
|
||||
};
|
||||
|
||||
await _organizationApiKeyRepository.CreateAsync(apiKey);
|
||||
return apiKey;
|
||||
}
|
||||
|
||||
// NOTE: Currently we only allow one type of api key per organization
|
||||
return apiKeys.Single();
|
||||
}
|
||||
|
||||
// NOTE: Currently we only allow one type of api key per organization
|
||||
return apiKeys.Single();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Enums;
|
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationApiKeys.Interfaces;
|
||||
|
||||
public interface IGetOrganizationApiKeyCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationApiKeys.Interfaces
|
||||
{
|
||||
Task<OrganizationApiKey> GetOrganizationApiKeyAsync(Guid organizationId, OrganizationApiKeyType organizationApiKeyType);
|
||||
public interface IGetOrganizationApiKeyCommand
|
||||
{
|
||||
Task<OrganizationApiKey> GetOrganizationApiKeyAsync(Guid organizationId, OrganizationApiKeyType organizationApiKeyType);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
using Bit.Core.Entities;
|
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationApiKeys.Interfaces;
|
||||
|
||||
public interface IRotateOrganizationApiKeyCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationApiKeys.Interfaces
|
||||
{
|
||||
Task<OrganizationApiKey> RotateApiKeyAsync(OrganizationApiKey organizationApiKey);
|
||||
public interface IRotateOrganizationApiKeyCommand
|
||||
{
|
||||
Task<OrganizationApiKey> RotateApiKeyAsync(OrganizationApiKey organizationApiKey);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,22 +3,23 @@ using Bit.Core.OrganizationFeatures.OrganizationApiKeys.Interfaces;
|
||||
using Bit.Core.Repositories;
|
||||
using Bit.Core.Utilities;
|
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationApiKeys;
|
||||
|
||||
public class RotateOrganizationApiKeyCommand : IRotateOrganizationApiKeyCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationApiKeys
|
||||
{
|
||||
private readonly IOrganizationApiKeyRepository _organizationApiKeyRepository;
|
||||
|
||||
public RotateOrganizationApiKeyCommand(IOrganizationApiKeyRepository organizationApiKeyRepository)
|
||||
public class RotateOrganizationApiKeyCommand : IRotateOrganizationApiKeyCommand
|
||||
{
|
||||
_organizationApiKeyRepository = organizationApiKeyRepository;
|
||||
}
|
||||
private readonly IOrganizationApiKeyRepository _organizationApiKeyRepository;
|
||||
|
||||
public async Task<OrganizationApiKey> RotateApiKeyAsync(OrganizationApiKey organizationApiKey)
|
||||
{
|
||||
organizationApiKey.ApiKey = CoreHelpers.SecureRandomString(30);
|
||||
organizationApiKey.RevisionDate = DateTime.UtcNow;
|
||||
await _organizationApiKeyRepository.UpsertAsync(organizationApiKey);
|
||||
return organizationApiKey;
|
||||
public RotateOrganizationApiKeyCommand(IOrganizationApiKeyRepository organizationApiKeyRepository)
|
||||
{
|
||||
_organizationApiKeyRepository = organizationApiKeyRepository;
|
||||
}
|
||||
|
||||
public async Task<OrganizationApiKey> RotateApiKeyAsync(OrganizationApiKey organizationApiKey)
|
||||
{
|
||||
organizationApiKey.ApiKey = CoreHelpers.SecureRandomString(30);
|
||||
organizationApiKey.RevisionDate = DateTime.UtcNow;
|
||||
await _organizationApiKeyRepository.UpsertAsync(organizationApiKey);
|
||||
return organizationApiKey;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,19 +3,20 @@ using Bit.Core.Models.Data.Organizations.OrganizationConnections;
|
||||
using Bit.Core.OrganizationFeatures.OrganizationConnections.Interfaces;
|
||||
using Bit.Core.Repositories;
|
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationConnections;
|
||||
|
||||
public class CreateOrganizationConnectionCommand : ICreateOrganizationConnectionCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationConnections
|
||||
{
|
||||
private readonly IOrganizationConnectionRepository _organizationConnectionRepository;
|
||||
|
||||
public CreateOrganizationConnectionCommand(IOrganizationConnectionRepository organizationConnectionRepository)
|
||||
public class CreateOrganizationConnectionCommand : ICreateOrganizationConnectionCommand
|
||||
{
|
||||
_organizationConnectionRepository = organizationConnectionRepository;
|
||||
}
|
||||
private readonly IOrganizationConnectionRepository _organizationConnectionRepository;
|
||||
|
||||
public async Task<OrganizationConnection> CreateAsync<T>(OrganizationConnectionData<T> connectionData) where T : new()
|
||||
{
|
||||
return await _organizationConnectionRepository.CreateAsync(connectionData.ToEntity());
|
||||
public CreateOrganizationConnectionCommand(IOrganizationConnectionRepository organizationConnectionRepository)
|
||||
{
|
||||
_organizationConnectionRepository = organizationConnectionRepository;
|
||||
}
|
||||
|
||||
public async Task<OrganizationConnection> CreateAsync<T>(OrganizationConnectionData<T> connectionData) where T : new()
|
||||
{
|
||||
return await _organizationConnectionRepository.CreateAsync(connectionData.ToEntity());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,19 +2,20 @@
|
||||
using Bit.Core.OrganizationFeatures.OrganizationConnections.Interfaces;
|
||||
using Bit.Core.Repositories;
|
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationConnections;
|
||||
|
||||
public class DeleteOrganizationConnectionCommand : IDeleteOrganizationConnectionCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationConnections
|
||||
{
|
||||
private readonly IOrganizationConnectionRepository _organizationConnectionRepository;
|
||||
|
||||
public DeleteOrganizationConnectionCommand(IOrganizationConnectionRepository organizationConnectionRepository)
|
||||
public class DeleteOrganizationConnectionCommand : IDeleteOrganizationConnectionCommand
|
||||
{
|
||||
_organizationConnectionRepository = organizationConnectionRepository;
|
||||
}
|
||||
private readonly IOrganizationConnectionRepository _organizationConnectionRepository;
|
||||
|
||||
public async Task DeleteAsync(OrganizationConnection connection)
|
||||
{
|
||||
await _organizationConnectionRepository.DeleteAsync(connection);
|
||||
public DeleteOrganizationConnectionCommand(IOrganizationConnectionRepository organizationConnectionRepository)
|
||||
{
|
||||
_organizationConnectionRepository = organizationConnectionRepository;
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(OrganizationConnection connection)
|
||||
{
|
||||
await _organizationConnectionRepository.DeleteAsync(connection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Models.Data.Organizations.OrganizationConnections;
|
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationConnections.Interfaces;
|
||||
|
||||
public interface ICreateOrganizationConnectionCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationConnections.Interfaces
|
||||
{
|
||||
Task<OrganizationConnection> CreateAsync<T>(OrganizationConnectionData<T> connectionData) where T : new();
|
||||
public interface ICreateOrganizationConnectionCommand
|
||||
{
|
||||
Task<OrganizationConnection> CreateAsync<T>(OrganizationConnectionData<T> connectionData) where T : new();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
using Bit.Core.Entities;
|
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationConnections.Interfaces;
|
||||
|
||||
public interface IDeleteOrganizationConnectionCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationConnections.Interfaces
|
||||
{
|
||||
Task DeleteAsync(OrganizationConnection connection);
|
||||
public interface IDeleteOrganizationConnectionCommand
|
||||
{
|
||||
Task DeleteAsync(OrganizationConnection connection);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Models.Data.Organizations.OrganizationConnections;
|
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationConnections.Interfaces;
|
||||
|
||||
public interface IUpdateOrganizationConnectionCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationConnections.Interfaces
|
||||
{
|
||||
Task<OrganizationConnection> UpdateAsync<T>(OrganizationConnectionData<T> connectionData) where T : new();
|
||||
public interface IUpdateOrganizationConnectionCommand
|
||||
{
|
||||
Task<OrganizationConnection> UpdateAsync<T>(OrganizationConnectionData<T> connectionData) where T : new();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,33 +4,34 @@ using Bit.Core.Models.Data.Organizations.OrganizationConnections;
|
||||
using Bit.Core.OrganizationFeatures.OrganizationConnections.Interfaces;
|
||||
using Bit.Core.Repositories;
|
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationConnections;
|
||||
|
||||
public class UpdateOrganizationConnectionCommand : IUpdateOrganizationConnectionCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationConnections
|
||||
{
|
||||
private readonly IOrganizationConnectionRepository _organizationConnectionRepository;
|
||||
|
||||
public UpdateOrganizationConnectionCommand(IOrganizationConnectionRepository organizationConnectionRepository)
|
||||
public class UpdateOrganizationConnectionCommand : IUpdateOrganizationConnectionCommand
|
||||
{
|
||||
_organizationConnectionRepository = organizationConnectionRepository;
|
||||
}
|
||||
private readonly IOrganizationConnectionRepository _organizationConnectionRepository;
|
||||
|
||||
public async Task<OrganizationConnection> UpdateAsync<T>(OrganizationConnectionData<T> connectionData) where T : new()
|
||||
{
|
||||
if (!connectionData.Id.HasValue)
|
||||
public UpdateOrganizationConnectionCommand(IOrganizationConnectionRepository organizationConnectionRepository)
|
||||
{
|
||||
throw new Exception("Cannot update connection, Connection does not exist.");
|
||||
_organizationConnectionRepository = organizationConnectionRepository;
|
||||
}
|
||||
|
||||
var connection = await _organizationConnectionRepository.GetByIdAsync(connectionData.Id.Value);
|
||||
|
||||
if (connection == null)
|
||||
public async Task<OrganizationConnection> UpdateAsync<T>(OrganizationConnectionData<T> connectionData) where T : new()
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
if (!connectionData.Id.HasValue)
|
||||
{
|
||||
throw new Exception("Cannot update connection, Connection does not exist.");
|
||||
}
|
||||
|
||||
var entity = connectionData.ToEntity();
|
||||
await _organizationConnectionRepository.UpsertAsync(entity);
|
||||
return entity;
|
||||
var connection = await _organizationConnectionRepository.GetByIdAsync(connectionData.Id.Value);
|
||||
|
||||
if (connection == null)
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
var entity = connectionData.ToEntity();
|
||||
await _organizationConnectionRepository.UpsertAsync(entity);
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,64 +13,65 @@ using Bit.Core.Tokens;
|
||||
using Microsoft.AspNetCore.DataProtection;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.Core.OrganizationFeatures;
|
||||
|
||||
public static class OrganizationServiceCollectionExtensions
|
||||
namespace Bit.Core.OrganizationFeatures
|
||||
{
|
||||
public static void AddOrganizationServices(this IServiceCollection services, IGlobalSettings globalSettings)
|
||||
public static class OrganizationServiceCollectionExtensions
|
||||
{
|
||||
services.AddScoped<IOrganizationService, OrganizationService>();
|
||||
services.AddTokenizers();
|
||||
services.AddOrganizationConnectionCommands();
|
||||
services.AddOrganizationSponsorshipCommands(globalSettings);
|
||||
services.AddOrganizationApiKeyCommands();
|
||||
}
|
||||
|
||||
private static void AddOrganizationConnectionCommands(this IServiceCollection services)
|
||||
{
|
||||
services.AddScoped<ICreateOrganizationConnectionCommand, CreateOrganizationConnectionCommand>();
|
||||
services.AddScoped<IDeleteOrganizationConnectionCommand, DeleteOrganizationConnectionCommand>();
|
||||
services.AddScoped<IUpdateOrganizationConnectionCommand, UpdateOrganizationConnectionCommand>();
|
||||
}
|
||||
|
||||
private static void AddOrganizationSponsorshipCommands(this IServiceCollection services, IGlobalSettings globalSettings)
|
||||
{
|
||||
services.AddScoped<ICreateSponsorshipCommand, CreateSponsorshipCommand>();
|
||||
services.AddScoped<IRemoveSponsorshipCommand, RemoveSponsorshipCommand>();
|
||||
services.AddScoped<ISendSponsorshipOfferCommand, SendSponsorshipOfferCommand>();
|
||||
services.AddScoped<ISetUpSponsorshipCommand, SetUpSponsorshipCommand>();
|
||||
services.AddScoped<IValidateRedemptionTokenCommand, ValidateRedemptionTokenCommand>();
|
||||
services.AddScoped<IValidateSponsorshipCommand, ValidateSponsorshipCommand>();
|
||||
services.AddScoped<IValidateBillingSyncKeyCommand, ValidateBillingSyncKeyCommand>();
|
||||
services.AddScoped<IOrganizationSponsorshipRenewCommand, OrganizationSponsorshipRenewCommand>();
|
||||
services.AddScoped<ICloudSyncSponsorshipsCommand, CloudSyncSponsorshipsCommand>();
|
||||
services.AddScoped<ISelfHostedSyncSponsorshipsCommand, SelfHostedSyncSponsorshipsCommand>();
|
||||
services.AddScoped<ISelfHostedSyncSponsorshipsCommand, SelfHostedSyncSponsorshipsCommand>();
|
||||
services.AddScoped<ICloudSyncSponsorshipsCommand, CloudSyncSponsorshipsCommand>();
|
||||
services.AddScoped<IValidateBillingSyncKeyCommand, ValidateBillingSyncKeyCommand>();
|
||||
if (globalSettings.SelfHosted)
|
||||
public static void AddOrganizationServices(this IServiceCollection services, IGlobalSettings globalSettings)
|
||||
{
|
||||
services.AddScoped<IRevokeSponsorshipCommand, SelfHostedRevokeSponsorshipCommand>();
|
||||
services.AddScoped<IOrganizationService, OrganizationService>();
|
||||
services.AddTokenizers();
|
||||
services.AddOrganizationConnectionCommands();
|
||||
services.AddOrganizationSponsorshipCommands(globalSettings);
|
||||
services.AddOrganizationApiKeyCommands();
|
||||
}
|
||||
else
|
||||
|
||||
private static void AddOrganizationConnectionCommands(this IServiceCollection services)
|
||||
{
|
||||
services.AddScoped<IRevokeSponsorshipCommand, CloudRevokeSponsorshipCommand>();
|
||||
services.AddScoped<ICreateOrganizationConnectionCommand, CreateOrganizationConnectionCommand>();
|
||||
services.AddScoped<IDeleteOrganizationConnectionCommand, DeleteOrganizationConnectionCommand>();
|
||||
services.AddScoped<IUpdateOrganizationConnectionCommand, UpdateOrganizationConnectionCommand>();
|
||||
}
|
||||
}
|
||||
|
||||
private static void AddOrganizationApiKeyCommands(this IServiceCollection services)
|
||||
{
|
||||
services.AddScoped<IGetOrganizationApiKeyCommand, GetOrganizationApiKeyCommand>();
|
||||
services.AddScoped<IRotateOrganizationApiKeyCommand, RotateOrganizationApiKeyCommand>();
|
||||
}
|
||||
private static void AddOrganizationSponsorshipCommands(this IServiceCollection services, IGlobalSettings globalSettings)
|
||||
{
|
||||
services.AddScoped<ICreateSponsorshipCommand, CreateSponsorshipCommand>();
|
||||
services.AddScoped<IRemoveSponsorshipCommand, RemoveSponsorshipCommand>();
|
||||
services.AddScoped<ISendSponsorshipOfferCommand, SendSponsorshipOfferCommand>();
|
||||
services.AddScoped<ISetUpSponsorshipCommand, SetUpSponsorshipCommand>();
|
||||
services.AddScoped<IValidateRedemptionTokenCommand, ValidateRedemptionTokenCommand>();
|
||||
services.AddScoped<IValidateSponsorshipCommand, ValidateSponsorshipCommand>();
|
||||
services.AddScoped<IValidateBillingSyncKeyCommand, ValidateBillingSyncKeyCommand>();
|
||||
services.AddScoped<IOrganizationSponsorshipRenewCommand, OrganizationSponsorshipRenewCommand>();
|
||||
services.AddScoped<ICloudSyncSponsorshipsCommand, CloudSyncSponsorshipsCommand>();
|
||||
services.AddScoped<ISelfHostedSyncSponsorshipsCommand, SelfHostedSyncSponsorshipsCommand>();
|
||||
services.AddScoped<ISelfHostedSyncSponsorshipsCommand, SelfHostedSyncSponsorshipsCommand>();
|
||||
services.AddScoped<ICloudSyncSponsorshipsCommand, CloudSyncSponsorshipsCommand>();
|
||||
services.AddScoped<IValidateBillingSyncKeyCommand, ValidateBillingSyncKeyCommand>();
|
||||
if (globalSettings.SelfHosted)
|
||||
{
|
||||
services.AddScoped<IRevokeSponsorshipCommand, SelfHostedRevokeSponsorshipCommand>();
|
||||
}
|
||||
else
|
||||
{
|
||||
services.AddScoped<IRevokeSponsorshipCommand, CloudRevokeSponsorshipCommand>();
|
||||
}
|
||||
}
|
||||
|
||||
private static void AddTokenizers(this IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IDataProtectorTokenFactory<OrganizationSponsorshipOfferTokenable>>(serviceProvider =>
|
||||
new DataProtectorTokenFactory<OrganizationSponsorshipOfferTokenable>(
|
||||
OrganizationSponsorshipOfferTokenable.ClearTextPrefix,
|
||||
OrganizationSponsorshipOfferTokenable.DataProtectorPurpose,
|
||||
serviceProvider.GetDataProtectionProvider())
|
||||
);
|
||||
private static void AddOrganizationApiKeyCommands(this IServiceCollection services)
|
||||
{
|
||||
services.AddScoped<IGetOrganizationApiKeyCommand, GetOrganizationApiKeyCommand>();
|
||||
services.AddScoped<IRotateOrganizationApiKeyCommand, RotateOrganizationApiKeyCommand>();
|
||||
}
|
||||
|
||||
private static void AddTokenizers(this IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IDataProtectorTokenFactory<OrganizationSponsorshipOfferTokenable>>(serviceProvider =>
|
||||
new DataProtectorTokenFactory<OrganizationSponsorshipOfferTokenable>(
|
||||
OrganizationSponsorshipOfferTokenable.ClearTextPrefix,
|
||||
OrganizationSponsorshipOfferTokenable.DataProtectorPurpose,
|
||||
serviceProvider.GetDataProtectionProvider())
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,38 +2,39 @@
|
||||
using Bit.Core.Exceptions;
|
||||
using Bit.Core.Repositories;
|
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise;
|
||||
|
||||
public abstract class CancelSponsorshipCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise
|
||||
{
|
||||
protected readonly IOrganizationSponsorshipRepository _organizationSponsorshipRepository;
|
||||
protected readonly IOrganizationRepository _organizationRepository;
|
||||
|
||||
public CancelSponsorshipCommand(IOrganizationSponsorshipRepository organizationSponsorshipRepository,
|
||||
IOrganizationRepository organizationRepository)
|
||||
public abstract class CancelSponsorshipCommand
|
||||
{
|
||||
_organizationSponsorshipRepository = organizationSponsorshipRepository;
|
||||
_organizationRepository = organizationRepository;
|
||||
}
|
||||
protected readonly IOrganizationSponsorshipRepository _organizationSponsorshipRepository;
|
||||
protected readonly IOrganizationRepository _organizationRepository;
|
||||
|
||||
protected virtual async Task DeleteSponsorshipAsync(OrganizationSponsorship sponsorship = null)
|
||||
{
|
||||
if (sponsorship == null)
|
||||
public CancelSponsorshipCommand(IOrganizationSponsorshipRepository organizationSponsorshipRepository,
|
||||
IOrganizationRepository organizationRepository)
|
||||
{
|
||||
return;
|
||||
_organizationSponsorshipRepository = organizationSponsorshipRepository;
|
||||
_organizationRepository = organizationRepository;
|
||||
}
|
||||
|
||||
await _organizationSponsorshipRepository.DeleteAsync(sponsorship);
|
||||
}
|
||||
|
||||
protected async Task MarkToDeleteSponsorshipAsync(OrganizationSponsorship sponsorship)
|
||||
{
|
||||
if (sponsorship == null)
|
||||
protected virtual async Task DeleteSponsorshipAsync(OrganizationSponsorship sponsorship = null)
|
||||
{
|
||||
throw new BadRequestException("The sponsorship you are trying to cancel does not exist");
|
||||
if (sponsorship == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await _organizationSponsorshipRepository.DeleteAsync(sponsorship);
|
||||
}
|
||||
|
||||
sponsorship.ToDelete = true;
|
||||
await _organizationSponsorshipRepository.UpsertAsync(sponsorship);
|
||||
protected async Task MarkToDeleteSponsorshipAsync(OrganizationSponsorship sponsorship)
|
||||
{
|
||||
if (sponsorship == null)
|
||||
{
|
||||
throw new BadRequestException("The sponsorship you are trying to cancel does not exist");
|
||||
}
|
||||
|
||||
sponsorship.ToDelete = true;
|
||||
await _organizationSponsorshipRepository.UpsertAsync(sponsorship);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,30 +3,31 @@ using Bit.Core.Exceptions;
|
||||
using Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Interfaces;
|
||||
using Bit.Core.Repositories;
|
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Cloud;
|
||||
|
||||
public class CloudRevokeSponsorshipCommand : CancelSponsorshipCommand, IRevokeSponsorshipCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Cloud
|
||||
{
|
||||
public CloudRevokeSponsorshipCommand(
|
||||
IOrganizationSponsorshipRepository organizationSponsorshipRepository,
|
||||
IOrganizationRepository organizationRepository) : base(organizationSponsorshipRepository, organizationRepository)
|
||||
public class CloudRevokeSponsorshipCommand : CancelSponsorshipCommand, IRevokeSponsorshipCommand
|
||||
{
|
||||
}
|
||||
|
||||
public async Task RevokeSponsorshipAsync(OrganizationSponsorship sponsorship)
|
||||
{
|
||||
if (sponsorship == null)
|
||||
public CloudRevokeSponsorshipCommand(
|
||||
IOrganizationSponsorshipRepository organizationSponsorshipRepository,
|
||||
IOrganizationRepository organizationRepository) : base(organizationSponsorshipRepository, organizationRepository)
|
||||
{
|
||||
throw new BadRequestException("You are not currently sponsoring an organization.");
|
||||
}
|
||||
|
||||
if (sponsorship.SponsoredOrganizationId == null)
|
||||
public async Task RevokeSponsorshipAsync(OrganizationSponsorship sponsorship)
|
||||
{
|
||||
await base.DeleteSponsorshipAsync(sponsorship);
|
||||
}
|
||||
else
|
||||
{
|
||||
await MarkToDeleteSponsorshipAsync(sponsorship);
|
||||
if (sponsorship == null)
|
||||
{
|
||||
throw new BadRequestException("You are not currently sponsoring an organization.");
|
||||
}
|
||||
|
||||
if (sponsorship.SponsoredOrganizationId == null)
|
||||
{
|
||||
await base.DeleteSponsorshipAsync(sponsorship);
|
||||
}
|
||||
else
|
||||
{
|
||||
await MarkToDeleteSponsorshipAsync(sponsorship);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,127 +7,128 @@ using Bit.Core.Repositories;
|
||||
using Bit.Core.Services;
|
||||
using Bit.Core.Utilities;
|
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Cloud;
|
||||
|
||||
public class CloudSyncSponsorshipsCommand : ICloudSyncSponsorshipsCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Cloud
|
||||
{
|
||||
private readonly IOrganizationSponsorshipRepository _organizationSponsorshipRepository;
|
||||
private readonly IEventService _eventService;
|
||||
|
||||
public CloudSyncSponsorshipsCommand(
|
||||
IOrganizationSponsorshipRepository organizationSponsorshipRepository,
|
||||
IEventService eventService)
|
||||
public class CloudSyncSponsorshipsCommand : ICloudSyncSponsorshipsCommand
|
||||
{
|
||||
_organizationSponsorshipRepository = organizationSponsorshipRepository;
|
||||
_eventService = eventService;
|
||||
}
|
||||
private readonly IOrganizationSponsorshipRepository _organizationSponsorshipRepository;
|
||||
private readonly IEventService _eventService;
|
||||
|
||||
public async Task<(OrganizationSponsorshipSyncData, IEnumerable<OrganizationSponsorship>)> SyncOrganization(Organization sponsoringOrg, IEnumerable<OrganizationSponsorshipData> sponsorshipsData)
|
||||
{
|
||||
if (sponsoringOrg == null)
|
||||
public CloudSyncSponsorshipsCommand(
|
||||
IOrganizationSponsorshipRepository organizationSponsorshipRepository,
|
||||
IEventService eventService)
|
||||
{
|
||||
throw new BadRequestException("Failed to sync sponsorship - missing organization.");
|
||||
_organizationSponsorshipRepository = organizationSponsorshipRepository;
|
||||
_eventService = eventService;
|
||||
}
|
||||
|
||||
var (processedSponsorshipsData, sponsorshipsToEmailOffer) = sponsorshipsData.Any() ?
|
||||
await DoSyncAsync(sponsoringOrg, sponsorshipsData) :
|
||||
(sponsorshipsData, Array.Empty<OrganizationSponsorship>());
|
||||
|
||||
await RecordEvent(sponsoringOrg);
|
||||
|
||||
return (new OrganizationSponsorshipSyncData
|
||||
public async Task<(OrganizationSponsorshipSyncData, IEnumerable<OrganizationSponsorship>)> SyncOrganization(Organization sponsoringOrg, IEnumerable<OrganizationSponsorshipData> sponsorshipsData)
|
||||
{
|
||||
SponsorshipsBatch = processedSponsorshipsData
|
||||
}, sponsorshipsToEmailOffer);
|
||||
}
|
||||
if (sponsoringOrg == null)
|
||||
{
|
||||
throw new BadRequestException("Failed to sync sponsorship - missing organization.");
|
||||
}
|
||||
|
||||
private async Task<(IEnumerable<OrganizationSponsorshipData> data, IEnumerable<OrganizationSponsorship> toOffer)> DoSyncAsync(Organization sponsoringOrg, IEnumerable<OrganizationSponsorshipData> sponsorshipsData)
|
||||
{
|
||||
var existingSponsorshipsDict = (await _organizationSponsorshipRepository.GetManyBySponsoringOrganizationAsync(sponsoringOrg.Id))
|
||||
.ToDictionary(i => i.SponsoringOrganizationUserId);
|
||||
var (processedSponsorshipsData, sponsorshipsToEmailOffer) = sponsorshipsData.Any() ?
|
||||
await DoSyncAsync(sponsoringOrg, sponsorshipsData) :
|
||||
(sponsorshipsData, Array.Empty<OrganizationSponsorship>());
|
||||
|
||||
var sponsorshipsToUpsert = new List<OrganizationSponsorship>();
|
||||
var sponsorshipIdsToDelete = new List<Guid>();
|
||||
var sponsorshipsToReturn = new List<OrganizationSponsorshipData>();
|
||||
await RecordEvent(sponsoringOrg);
|
||||
|
||||
foreach (var selfHostedSponsorship in sponsorshipsData)
|
||||
return (new OrganizationSponsorshipSyncData
|
||||
{
|
||||
SponsorshipsBatch = processedSponsorshipsData
|
||||
}, sponsorshipsToEmailOffer);
|
||||
}
|
||||
|
||||
private async Task<(IEnumerable<OrganizationSponsorshipData> data, IEnumerable<OrganizationSponsorship> toOffer)> DoSyncAsync(Organization sponsoringOrg, IEnumerable<OrganizationSponsorshipData> sponsorshipsData)
|
||||
{
|
||||
var requiredSponsoringProductType = StaticStore.GetSponsoredPlan(selfHostedSponsorship.PlanSponsorshipType)?.SponsoringProductType;
|
||||
if (requiredSponsoringProductType == null
|
||||
|| StaticStore.GetPlan(sponsoringOrg.PlanType).Product != requiredSponsoringProductType.Value)
|
||||
{
|
||||
continue; // prevent unsupported sponsorships
|
||||
}
|
||||
var existingSponsorshipsDict = (await _organizationSponsorshipRepository.GetManyBySponsoringOrganizationAsync(sponsoringOrg.Id))
|
||||
.ToDictionary(i => i.SponsoringOrganizationUserId);
|
||||
|
||||
if (!existingSponsorshipsDict.TryGetValue(selfHostedSponsorship.SponsoringOrganizationUserId, out var cloudSponsorship))
|
||||
{
|
||||
if (selfHostedSponsorship.ToDelete && selfHostedSponsorship.LastSyncDate == null)
|
||||
{
|
||||
continue; // prevent invalid sponsorships in cloud. These should have been deleted by self hosted
|
||||
}
|
||||
if (OrgDisabledForMoreThanGracePeriod(sponsoringOrg))
|
||||
{
|
||||
continue; // prevent new sponsorships from disabled orgs
|
||||
}
|
||||
cloudSponsorship = new OrganizationSponsorship
|
||||
{
|
||||
SponsoringOrganizationId = sponsoringOrg.Id,
|
||||
SponsoringOrganizationUserId = selfHostedSponsorship.SponsoringOrganizationUserId,
|
||||
FriendlyName = selfHostedSponsorship.FriendlyName,
|
||||
OfferedToEmail = selfHostedSponsorship.OfferedToEmail,
|
||||
PlanSponsorshipType = selfHostedSponsorship.PlanSponsorshipType,
|
||||
LastSyncDate = DateTime.UtcNow,
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
cloudSponsorship.LastSyncDate = DateTime.UtcNow;
|
||||
}
|
||||
var sponsorshipsToUpsert = new List<OrganizationSponsorship>();
|
||||
var sponsorshipIdsToDelete = new List<Guid>();
|
||||
var sponsorshipsToReturn = new List<OrganizationSponsorshipData>();
|
||||
|
||||
if (selfHostedSponsorship.ToDelete)
|
||||
foreach (var selfHostedSponsorship in sponsorshipsData)
|
||||
{
|
||||
if (cloudSponsorship.SponsoredOrganizationId == null)
|
||||
var requiredSponsoringProductType = StaticStore.GetSponsoredPlan(selfHostedSponsorship.PlanSponsorshipType)?.SponsoringProductType;
|
||||
if (requiredSponsoringProductType == null
|
||||
|| StaticStore.GetPlan(sponsoringOrg.PlanType).Product != requiredSponsoringProductType.Value)
|
||||
{
|
||||
sponsorshipIdsToDelete.Add(cloudSponsorship.Id);
|
||||
selfHostedSponsorship.CloudSponsorshipRemoved = true;
|
||||
continue; // prevent unsupported sponsorships
|
||||
}
|
||||
|
||||
if (!existingSponsorshipsDict.TryGetValue(selfHostedSponsorship.SponsoringOrganizationUserId, out var cloudSponsorship))
|
||||
{
|
||||
if (selfHostedSponsorship.ToDelete && selfHostedSponsorship.LastSyncDate == null)
|
||||
{
|
||||
continue; // prevent invalid sponsorships in cloud. These should have been deleted by self hosted
|
||||
}
|
||||
if (OrgDisabledForMoreThanGracePeriod(sponsoringOrg))
|
||||
{
|
||||
continue; // prevent new sponsorships from disabled orgs
|
||||
}
|
||||
cloudSponsorship = new OrganizationSponsorship
|
||||
{
|
||||
SponsoringOrganizationId = sponsoringOrg.Id,
|
||||
SponsoringOrganizationUserId = selfHostedSponsorship.SponsoringOrganizationUserId,
|
||||
FriendlyName = selfHostedSponsorship.FriendlyName,
|
||||
OfferedToEmail = selfHostedSponsorship.OfferedToEmail,
|
||||
PlanSponsorshipType = selfHostedSponsorship.PlanSponsorshipType,
|
||||
LastSyncDate = DateTime.UtcNow,
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
cloudSponsorship.ToDelete = true;
|
||||
cloudSponsorship.LastSyncDate = DateTime.UtcNow;
|
||||
}
|
||||
|
||||
if (selfHostedSponsorship.ToDelete)
|
||||
{
|
||||
if (cloudSponsorship.SponsoredOrganizationId == null)
|
||||
{
|
||||
sponsorshipIdsToDelete.Add(cloudSponsorship.Id);
|
||||
selfHostedSponsorship.CloudSponsorshipRemoved = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
cloudSponsorship.ToDelete = true;
|
||||
}
|
||||
}
|
||||
sponsorshipsToUpsert.Add(cloudSponsorship);
|
||||
|
||||
selfHostedSponsorship.ValidUntil = cloudSponsorship.ValidUntil;
|
||||
selfHostedSponsorship.LastSyncDate = DateTime.UtcNow;
|
||||
sponsorshipsToReturn.Add(selfHostedSponsorship);
|
||||
}
|
||||
var sponsorshipsToEmailOffer = sponsorshipsToUpsert.Where(s => s.Id == default).ToArray();
|
||||
if (sponsorshipsToUpsert.Any())
|
||||
{
|
||||
await _organizationSponsorshipRepository.UpsertManyAsync(sponsorshipsToUpsert);
|
||||
}
|
||||
if (sponsorshipIdsToDelete.Any())
|
||||
{
|
||||
await _organizationSponsorshipRepository.DeleteManyAsync(sponsorshipIdsToDelete);
|
||||
}
|
||||
sponsorshipsToUpsert.Add(cloudSponsorship);
|
||||
|
||||
selfHostedSponsorship.ValidUntil = cloudSponsorship.ValidUntil;
|
||||
selfHostedSponsorship.LastSyncDate = DateTime.UtcNow;
|
||||
sponsorshipsToReturn.Add(selfHostedSponsorship);
|
||||
return (sponsorshipsToReturn, sponsorshipsToEmailOffer);
|
||||
}
|
||||
var sponsorshipsToEmailOffer = sponsorshipsToUpsert.Where(s => s.Id == default).ToArray();
|
||||
if (sponsorshipsToUpsert.Any())
|
||||
|
||||
/// <summary>
|
||||
/// True if Organization is disabled and the expiration date is more than three months ago
|
||||
/// </summary>
|
||||
/// <param name="organization"></param>
|
||||
private bool OrgDisabledForMoreThanGracePeriod(Organization organization) =>
|
||||
!organization.Enabled &&
|
||||
(
|
||||
!organization.ExpirationDate.HasValue ||
|
||||
DateTime.UtcNow.Subtract(organization.ExpirationDate.Value).TotalDays > 93
|
||||
);
|
||||
|
||||
private async Task RecordEvent(Organization organization)
|
||||
{
|
||||
await _organizationSponsorshipRepository.UpsertManyAsync(sponsorshipsToUpsert);
|
||||
await _eventService.LogOrganizationEventAsync(organization, EventType.Organization_SponsorshipsSynced);
|
||||
}
|
||||
if (sponsorshipIdsToDelete.Any())
|
||||
{
|
||||
await _organizationSponsorshipRepository.DeleteManyAsync(sponsorshipIdsToDelete);
|
||||
}
|
||||
|
||||
return (sponsorshipsToReturn, sponsorshipsToEmailOffer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// True if Organization is disabled and the expiration date is more than three months ago
|
||||
/// </summary>
|
||||
/// <param name="organization"></param>
|
||||
private bool OrgDisabledForMoreThanGracePeriod(Organization organization) =>
|
||||
!organization.Enabled &&
|
||||
(
|
||||
!organization.ExpirationDate.HasValue ||
|
||||
DateTime.UtcNow.Subtract(organization.ExpirationDate.Value).TotalDays > 93
|
||||
);
|
||||
|
||||
private async Task RecordEvent(Organization organization)
|
||||
{
|
||||
await _eventService.LogOrganizationEventAsync(organization, EventType.Organization_SponsorshipsSynced);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,27 +1,28 @@
|
||||
using Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Interfaces;
|
||||
using Bit.Core.Repositories;
|
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Cloud;
|
||||
|
||||
public class OrganizationSponsorshipRenewCommand : IOrganizationSponsorshipRenewCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Cloud
|
||||
{
|
||||
private readonly IOrganizationSponsorshipRepository _organizationSponsorshipRepository;
|
||||
|
||||
public OrganizationSponsorshipRenewCommand(IOrganizationSponsorshipRepository organizationSponsorshipRepository)
|
||||
public class OrganizationSponsorshipRenewCommand : IOrganizationSponsorshipRenewCommand
|
||||
{
|
||||
_organizationSponsorshipRepository = organizationSponsorshipRepository;
|
||||
}
|
||||
private readonly IOrganizationSponsorshipRepository _organizationSponsorshipRepository;
|
||||
|
||||
public async Task UpdateExpirationDateAsync(Guid organizationId, DateTime expireDate)
|
||||
{
|
||||
var sponsorship = await _organizationSponsorshipRepository.GetBySponsoredOrganizationIdAsync(organizationId);
|
||||
|
||||
if (sponsorship == null)
|
||||
public OrganizationSponsorshipRenewCommand(IOrganizationSponsorshipRepository organizationSponsorshipRepository)
|
||||
{
|
||||
return;
|
||||
_organizationSponsorshipRepository = organizationSponsorshipRepository;
|
||||
}
|
||||
|
||||
sponsorship.ValidUntil = expireDate;
|
||||
await _organizationSponsorshipRepository.UpsertAsync(sponsorship);
|
||||
public async Task UpdateExpirationDateAsync(Guid organizationId, DateTime expireDate)
|
||||
{
|
||||
var sponsorship = await _organizationSponsorshipRepository.GetBySponsoredOrganizationIdAsync(organizationId);
|
||||
|
||||
if (sponsorship == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
sponsorship.ValidUntil = expireDate;
|
||||
await _organizationSponsorshipRepository.UpsertAsync(sponsorship);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,23 +3,24 @@ using Bit.Core.Exceptions;
|
||||
using Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Interfaces;
|
||||
using Bit.Core.Repositories;
|
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Cloud;
|
||||
|
||||
public class RemoveSponsorshipCommand : CancelSponsorshipCommand, IRemoveSponsorshipCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Cloud
|
||||
{
|
||||
public RemoveSponsorshipCommand(
|
||||
IOrganizationSponsorshipRepository organizationSponsorshipRepository,
|
||||
IOrganizationRepository organizationRepository) : base(organizationSponsorshipRepository, organizationRepository)
|
||||
public class RemoveSponsorshipCommand : CancelSponsorshipCommand, IRemoveSponsorshipCommand
|
||||
{
|
||||
}
|
||||
|
||||
public async Task RemoveSponsorshipAsync(OrganizationSponsorship sponsorship)
|
||||
{
|
||||
if (sponsorship == null || sponsorship.SponsoredOrganizationId == null)
|
||||
public RemoveSponsorshipCommand(
|
||||
IOrganizationSponsorshipRepository organizationSponsorshipRepository,
|
||||
IOrganizationRepository organizationRepository) : base(organizationSponsorshipRepository, organizationRepository)
|
||||
{
|
||||
throw new BadRequestException("The requested organization is not currently being sponsored.");
|
||||
}
|
||||
|
||||
await MarkToDeleteSponsorshipAsync(sponsorship);
|
||||
public async Task RemoveSponsorshipAsync(OrganizationSponsorship sponsorship)
|
||||
{
|
||||
if (sponsorship == null || sponsorship.SponsoredOrganizationId == null)
|
||||
{
|
||||
throw new BadRequestException("The requested organization is not currently being sponsored.");
|
||||
}
|
||||
|
||||
await MarkToDeleteSponsorshipAsync(sponsorship);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,63 +7,64 @@ using Bit.Core.Repositories;
|
||||
using Bit.Core.Services;
|
||||
using Bit.Core.Tokens;
|
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Cloud;
|
||||
|
||||
public class SendSponsorshipOfferCommand : ISendSponsorshipOfferCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Cloud
|
||||
{
|
||||
private readonly IUserRepository _userRepository;
|
||||
private readonly IMailService _mailService;
|
||||
private readonly IDataProtectorTokenFactory<OrganizationSponsorshipOfferTokenable> _tokenFactory;
|
||||
|
||||
public SendSponsorshipOfferCommand(IUserRepository userRepository,
|
||||
IMailService mailService,
|
||||
IDataProtectorTokenFactory<OrganizationSponsorshipOfferTokenable> tokenFactory)
|
||||
public class SendSponsorshipOfferCommand : ISendSponsorshipOfferCommand
|
||||
{
|
||||
_userRepository = userRepository;
|
||||
_mailService = mailService;
|
||||
_tokenFactory = tokenFactory;
|
||||
}
|
||||
private readonly IUserRepository _userRepository;
|
||||
private readonly IMailService _mailService;
|
||||
private readonly IDataProtectorTokenFactory<OrganizationSponsorshipOfferTokenable> _tokenFactory;
|
||||
|
||||
public async Task BulkSendSponsorshipOfferAsync(string sponsoringOrgName, IEnumerable<OrganizationSponsorship> sponsorships)
|
||||
{
|
||||
var invites = new List<(string, bool, string)>();
|
||||
foreach (var sponsorship in sponsorships)
|
||||
public SendSponsorshipOfferCommand(IUserRepository userRepository,
|
||||
IMailService mailService,
|
||||
IDataProtectorTokenFactory<OrganizationSponsorshipOfferTokenable> tokenFactory)
|
||||
{
|
||||
_userRepository = userRepository;
|
||||
_mailService = mailService;
|
||||
_tokenFactory = tokenFactory;
|
||||
}
|
||||
|
||||
public async Task BulkSendSponsorshipOfferAsync(string sponsoringOrgName, IEnumerable<OrganizationSponsorship> sponsorships)
|
||||
{
|
||||
var invites = new List<(string, bool, string)>();
|
||||
foreach (var sponsorship in sponsorships)
|
||||
{
|
||||
var user = await _userRepository.GetByEmailAsync(sponsorship.OfferedToEmail);
|
||||
var isExistingAccount = user != null;
|
||||
invites.Add((sponsorship.OfferedToEmail, user != null, _tokenFactory.Protect(new OrganizationSponsorshipOfferTokenable(sponsorship))));
|
||||
}
|
||||
|
||||
await _mailService.BulkSendFamiliesForEnterpriseOfferEmailAsync(sponsoringOrgName, invites);
|
||||
}
|
||||
|
||||
public async Task SendSponsorshipOfferAsync(OrganizationSponsorship sponsorship, string sponsoringOrgName)
|
||||
{
|
||||
var user = await _userRepository.GetByEmailAsync(sponsorship.OfferedToEmail);
|
||||
var isExistingAccount = user != null;
|
||||
invites.Add((sponsorship.OfferedToEmail, user != null, _tokenFactory.Protect(new OrganizationSponsorshipOfferTokenable(sponsorship))));
|
||||
|
||||
await _mailService.SendFamiliesForEnterpriseOfferEmailAsync(sponsoringOrgName, sponsorship.OfferedToEmail,
|
||||
isExistingAccount, _tokenFactory.Protect(new OrganizationSponsorshipOfferTokenable(sponsorship)));
|
||||
}
|
||||
|
||||
await _mailService.BulkSendFamiliesForEnterpriseOfferEmailAsync(sponsoringOrgName, invites);
|
||||
}
|
||||
|
||||
public async Task SendSponsorshipOfferAsync(OrganizationSponsorship sponsorship, string sponsoringOrgName)
|
||||
{
|
||||
var user = await _userRepository.GetByEmailAsync(sponsorship.OfferedToEmail);
|
||||
var isExistingAccount = user != null;
|
||||
|
||||
await _mailService.SendFamiliesForEnterpriseOfferEmailAsync(sponsoringOrgName, sponsorship.OfferedToEmail,
|
||||
isExistingAccount, _tokenFactory.Protect(new OrganizationSponsorshipOfferTokenable(sponsorship)));
|
||||
}
|
||||
|
||||
public async Task SendSponsorshipOfferAsync(Organization sponsoringOrg, OrganizationUser sponsoringOrgUser,
|
||||
OrganizationSponsorship sponsorship)
|
||||
{
|
||||
if (sponsoringOrg == null)
|
||||
public async Task SendSponsorshipOfferAsync(Organization sponsoringOrg, OrganizationUser sponsoringOrgUser,
|
||||
OrganizationSponsorship sponsorship)
|
||||
{
|
||||
throw new BadRequestException("Cannot find the requested sponsoring organization.");
|
||||
}
|
||||
if (sponsoringOrg == null)
|
||||
{
|
||||
throw new BadRequestException("Cannot find the requested sponsoring organization.");
|
||||
}
|
||||
|
||||
if (sponsoringOrgUser == null || sponsoringOrgUser.Status != OrganizationUserStatusType.Confirmed)
|
||||
{
|
||||
throw new BadRequestException("Only confirmed users can sponsor other organizations.");
|
||||
}
|
||||
if (sponsoringOrgUser == null || sponsoringOrgUser.Status != OrganizationUserStatusType.Confirmed)
|
||||
{
|
||||
throw new BadRequestException("Only confirmed users can sponsor other organizations.");
|
||||
}
|
||||
|
||||
if (sponsorship == null || sponsorship.OfferedToEmail == null)
|
||||
{
|
||||
throw new BadRequestException("Cannot find an outstanding sponsorship offer for this organization.");
|
||||
}
|
||||
if (sponsorship == null || sponsorship.OfferedToEmail == null)
|
||||
{
|
||||
throw new BadRequestException("Cannot find an outstanding sponsorship offer for this organization.");
|
||||
}
|
||||
|
||||
await SendSponsorshipOfferAsync(sponsorship, sponsoringOrg.Name);
|
||||
await SendSponsorshipOfferAsync(sponsorship, sponsoringOrg.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,62 +5,63 @@ using Bit.Core.Repositories;
|
||||
using Bit.Core.Services;
|
||||
using Bit.Core.Utilities;
|
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Cloud;
|
||||
|
||||
public class SetUpSponsorshipCommand : ISetUpSponsorshipCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Cloud
|
||||
{
|
||||
private readonly IOrganizationSponsorshipRepository _organizationSponsorshipRepository;
|
||||
private readonly IOrganizationRepository _organizationRepository;
|
||||
private readonly IPaymentService _paymentService;
|
||||
|
||||
public SetUpSponsorshipCommand(IOrganizationSponsorshipRepository organizationSponsorshipRepository, IOrganizationRepository organizationRepository, IPaymentService paymentService)
|
||||
public class SetUpSponsorshipCommand : ISetUpSponsorshipCommand
|
||||
{
|
||||
_organizationSponsorshipRepository = organizationSponsorshipRepository;
|
||||
_organizationRepository = organizationRepository;
|
||||
_paymentService = paymentService;
|
||||
}
|
||||
private readonly IOrganizationSponsorshipRepository _organizationSponsorshipRepository;
|
||||
private readonly IOrganizationRepository _organizationRepository;
|
||||
private readonly IPaymentService _paymentService;
|
||||
|
||||
public async Task SetUpSponsorshipAsync(OrganizationSponsorship sponsorship,
|
||||
Organization sponsoredOrganization)
|
||||
{
|
||||
if (sponsorship == null)
|
||||
public SetUpSponsorshipCommand(IOrganizationSponsorshipRepository organizationSponsorshipRepository, IOrganizationRepository organizationRepository, IPaymentService paymentService)
|
||||
{
|
||||
throw new BadRequestException("No unredeemed sponsorship offer exists for you.");
|
||||
_organizationSponsorshipRepository = organizationSponsorshipRepository;
|
||||
_organizationRepository = organizationRepository;
|
||||
_paymentService = paymentService;
|
||||
}
|
||||
|
||||
var existingOrgSponsorship = await _organizationSponsorshipRepository
|
||||
.GetBySponsoredOrganizationIdAsync(sponsoredOrganization.Id);
|
||||
if (existingOrgSponsorship != null)
|
||||
public async Task SetUpSponsorshipAsync(OrganizationSponsorship sponsorship,
|
||||
Organization sponsoredOrganization)
|
||||
{
|
||||
throw new BadRequestException("Cannot redeem a sponsorship offer for an organization that is already sponsored. Revoke existing sponsorship first.");
|
||||
if (sponsorship == null)
|
||||
{
|
||||
throw new BadRequestException("No unredeemed sponsorship offer exists for you.");
|
||||
}
|
||||
|
||||
var existingOrgSponsorship = await _organizationSponsorshipRepository
|
||||
.GetBySponsoredOrganizationIdAsync(sponsoredOrganization.Id);
|
||||
if (existingOrgSponsorship != null)
|
||||
{
|
||||
throw new BadRequestException("Cannot redeem a sponsorship offer for an organization that is already sponsored. Revoke existing sponsorship first.");
|
||||
}
|
||||
|
||||
if (sponsorship.PlanSponsorshipType == null)
|
||||
{
|
||||
throw new BadRequestException("Cannot set up sponsorship without a known sponsorship type.");
|
||||
}
|
||||
|
||||
// Do not allow self-hosted sponsorships that haven't been synced for > 0.5 year
|
||||
if (sponsorship.LastSyncDate != null && DateTime.UtcNow.Subtract(sponsorship.LastSyncDate.Value).TotalDays > 182.5)
|
||||
{
|
||||
await _organizationSponsorshipRepository.DeleteAsync(sponsorship);
|
||||
throw new BadRequestException("This sponsorship offer is more than 6 months old and has expired.");
|
||||
}
|
||||
|
||||
// Check org to sponsor's product type
|
||||
var requiredSponsoredProductType = StaticStore.GetSponsoredPlan(sponsorship.PlanSponsorshipType.Value)?.SponsoredProductType;
|
||||
if (requiredSponsoredProductType == null ||
|
||||
sponsoredOrganization == null ||
|
||||
StaticStore.GetPlan(sponsoredOrganization.PlanType).Product != requiredSponsoredProductType.Value)
|
||||
{
|
||||
throw new BadRequestException("Can only redeem sponsorship offer on families organizations.");
|
||||
}
|
||||
|
||||
await _paymentService.SponsorOrganizationAsync(sponsoredOrganization, sponsorship);
|
||||
await _organizationRepository.UpsertAsync(sponsoredOrganization);
|
||||
|
||||
sponsorship.SponsoredOrganizationId = sponsoredOrganization.Id;
|
||||
sponsorship.OfferedToEmail = null;
|
||||
await _organizationSponsorshipRepository.UpsertAsync(sponsorship);
|
||||
}
|
||||
|
||||
if (sponsorship.PlanSponsorshipType == null)
|
||||
{
|
||||
throw new BadRequestException("Cannot set up sponsorship without a known sponsorship type.");
|
||||
}
|
||||
|
||||
// Do not allow self-hosted sponsorships that haven't been synced for > 0.5 year
|
||||
if (sponsorship.LastSyncDate != null && DateTime.UtcNow.Subtract(sponsorship.LastSyncDate.Value).TotalDays > 182.5)
|
||||
{
|
||||
await _organizationSponsorshipRepository.DeleteAsync(sponsorship);
|
||||
throw new BadRequestException("This sponsorship offer is more than 6 months old and has expired.");
|
||||
}
|
||||
|
||||
// Check org to sponsor's product type
|
||||
var requiredSponsoredProductType = StaticStore.GetSponsoredPlan(sponsorship.PlanSponsorshipType.Value)?.SponsoredProductType;
|
||||
if (requiredSponsoredProductType == null ||
|
||||
sponsoredOrganization == null ||
|
||||
StaticStore.GetPlan(sponsoredOrganization.PlanType).Product != requiredSponsoredProductType.Value)
|
||||
{
|
||||
throw new BadRequestException("Can only redeem sponsorship offer on families organizations.");
|
||||
}
|
||||
|
||||
await _paymentService.SponsorOrganizationAsync(sponsoredOrganization, sponsorship);
|
||||
await _organizationRepository.UpsertAsync(sponsoredOrganization);
|
||||
|
||||
sponsorship.SponsoredOrganizationId = sponsoredOrganization.Id;
|
||||
sponsorship.OfferedToEmail = null;
|
||||
await _organizationSponsorshipRepository.UpsertAsync(sponsorship);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,37 +3,38 @@ using Bit.Core.Exceptions;
|
||||
using Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Interfaces;
|
||||
using Bit.Core.Repositories;
|
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Cloud;
|
||||
|
||||
public class ValidateBillingSyncKeyCommand : IValidateBillingSyncKeyCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Cloud
|
||||
{
|
||||
private readonly IOrganizationSponsorshipRepository _organizationSponsorshipRepository;
|
||||
private readonly IOrganizationApiKeyRepository _apiKeyRepository;
|
||||
|
||||
public ValidateBillingSyncKeyCommand(
|
||||
IOrganizationSponsorshipRepository organizationSponsorshipRepository,
|
||||
IOrganizationApiKeyRepository organizationApiKeyRepository)
|
||||
public class ValidateBillingSyncKeyCommand : IValidateBillingSyncKeyCommand
|
||||
{
|
||||
_organizationSponsorshipRepository = organizationSponsorshipRepository;
|
||||
_apiKeyRepository = organizationApiKeyRepository;
|
||||
}
|
||||
private readonly IOrganizationSponsorshipRepository _organizationSponsorshipRepository;
|
||||
private readonly IOrganizationApiKeyRepository _apiKeyRepository;
|
||||
|
||||
public async Task<bool> ValidateBillingSyncKeyAsync(Organization organization, string billingSyncKey)
|
||||
{
|
||||
if (organization == null)
|
||||
public ValidateBillingSyncKeyCommand(
|
||||
IOrganizationSponsorshipRepository organizationSponsorshipRepository,
|
||||
IOrganizationApiKeyRepository organizationApiKeyRepository)
|
||||
{
|
||||
throw new BadRequestException("Invalid organization");
|
||||
_organizationSponsorshipRepository = organizationSponsorshipRepository;
|
||||
_apiKeyRepository = organizationApiKeyRepository;
|
||||
}
|
||||
if (string.IsNullOrWhiteSpace(billingSyncKey))
|
||||
|
||||
public async Task<bool> ValidateBillingSyncKeyAsync(Organization organization, string billingSyncKey)
|
||||
{
|
||||
if (organization == null)
|
||||
{
|
||||
throw new BadRequestException("Invalid organization");
|
||||
}
|
||||
if (string.IsNullOrWhiteSpace(billingSyncKey))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var orgApiKey = (await _apiKeyRepository.GetManyByOrganizationIdTypeAsync(organization.Id, Enums.OrganizationApiKeyType.BillingSync)).FirstOrDefault();
|
||||
if (string.Equals(orgApiKey.ApiKey, billingSyncKey))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
var orgApiKey = (await _apiKeyRepository.GetManyByOrganizationIdTypeAsync(organization.Id, Enums.OrganizationApiKeyType.BillingSync)).FirstOrDefault();
|
||||
if (string.Equals(orgApiKey.ApiKey, billingSyncKey))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,33 +4,34 @@ using Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterpri
|
||||
using Bit.Core.Repositories;
|
||||
using Bit.Core.Tokens;
|
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Cloud;
|
||||
|
||||
public class ValidateRedemptionTokenCommand : IValidateRedemptionTokenCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Cloud
|
||||
{
|
||||
private readonly IOrganizationSponsorshipRepository _organizationSponsorshipRepository;
|
||||
private readonly IDataProtectorTokenFactory<OrganizationSponsorshipOfferTokenable> _dataProtectorTokenFactory;
|
||||
|
||||
public ValidateRedemptionTokenCommand(IOrganizationSponsorshipRepository organizationSponsorshipRepository,
|
||||
IDataProtectorTokenFactory<OrganizationSponsorshipOfferTokenable> dataProtectorTokenFactory)
|
||||
public class ValidateRedemptionTokenCommand : IValidateRedemptionTokenCommand
|
||||
{
|
||||
_organizationSponsorshipRepository = organizationSponsorshipRepository;
|
||||
_dataProtectorTokenFactory = dataProtectorTokenFactory;
|
||||
}
|
||||
private readonly IOrganizationSponsorshipRepository _organizationSponsorshipRepository;
|
||||
private readonly IDataProtectorTokenFactory<OrganizationSponsorshipOfferTokenable> _dataProtectorTokenFactory;
|
||||
|
||||
public async Task<(bool valid, OrganizationSponsorship sponsorship)> ValidateRedemptionTokenAsync(string encryptedToken, string sponsoredUserEmail)
|
||||
{
|
||||
|
||||
if (!_dataProtectorTokenFactory.TryUnprotect(encryptedToken, out var tokenable))
|
||||
public ValidateRedemptionTokenCommand(IOrganizationSponsorshipRepository organizationSponsorshipRepository,
|
||||
IDataProtectorTokenFactory<OrganizationSponsorshipOfferTokenable> dataProtectorTokenFactory)
|
||||
{
|
||||
return (false, null);
|
||||
_organizationSponsorshipRepository = organizationSponsorshipRepository;
|
||||
_dataProtectorTokenFactory = dataProtectorTokenFactory;
|
||||
}
|
||||
|
||||
var sponsorship = await _organizationSponsorshipRepository.GetByIdAsync(tokenable.Id);
|
||||
if (!tokenable.IsValid(sponsorship, sponsoredUserEmail))
|
||||
public async Task<(bool valid, OrganizationSponsorship sponsorship)> ValidateRedemptionTokenAsync(string encryptedToken, string sponsoredUserEmail)
|
||||
{
|
||||
return (false, sponsorship);
|
||||
|
||||
if (!_dataProtectorTokenFactory.TryUnprotect(encryptedToken, out var tokenable))
|
||||
{
|
||||
return (false, null);
|
||||
}
|
||||
|
||||
var sponsorship = await _organizationSponsorshipRepository.GetByIdAsync(tokenable.Id);
|
||||
if (!tokenable.IsValid(sponsorship, sponsoredUserEmail))
|
||||
{
|
||||
return (false, sponsorship);
|
||||
}
|
||||
return (true, sponsorship);
|
||||
}
|
||||
return (true, sponsorship);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,111 +4,112 @@ using Bit.Core.Repositories;
|
||||
using Bit.Core.Services;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Cloud;
|
||||
|
||||
public class ValidateSponsorshipCommand : CancelSponsorshipCommand, IValidateSponsorshipCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Cloud
|
||||
{
|
||||
private readonly IPaymentService _paymentService;
|
||||
private readonly IMailService _mailService;
|
||||
private readonly ILogger<ValidateSponsorshipCommand> _logger;
|
||||
|
||||
public ValidateSponsorshipCommand(
|
||||
IOrganizationSponsorshipRepository organizationSponsorshipRepository,
|
||||
IOrganizationRepository organizationRepository,
|
||||
IPaymentService paymentService,
|
||||
IMailService mailService,
|
||||
ILogger<ValidateSponsorshipCommand> logger) : base(organizationSponsorshipRepository, organizationRepository)
|
||||
public class ValidateSponsorshipCommand : CancelSponsorshipCommand, IValidateSponsorshipCommand
|
||||
{
|
||||
_paymentService = paymentService;
|
||||
_mailService = mailService;
|
||||
_logger = logger;
|
||||
}
|
||||
private readonly IPaymentService _paymentService;
|
||||
private readonly IMailService _mailService;
|
||||
private readonly ILogger<ValidateSponsorshipCommand> _logger;
|
||||
|
||||
public async Task<bool> ValidateSponsorshipAsync(Guid sponsoredOrganizationId)
|
||||
{
|
||||
var sponsoredOrganization = await _organizationRepository.GetByIdAsync(sponsoredOrganizationId);
|
||||
if (sponsoredOrganization == null)
|
||||
public ValidateSponsorshipCommand(
|
||||
IOrganizationSponsorshipRepository organizationSponsorshipRepository,
|
||||
IOrganizationRepository organizationRepository,
|
||||
IPaymentService paymentService,
|
||||
IMailService mailService,
|
||||
ILogger<ValidateSponsorshipCommand> logger) : base(organizationSponsorshipRepository, organizationRepository)
|
||||
{
|
||||
return false;
|
||||
_paymentService = paymentService;
|
||||
_mailService = mailService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
var existingSponsorship = await _organizationSponsorshipRepository
|
||||
.GetBySponsoredOrganizationIdAsync(sponsoredOrganizationId);
|
||||
|
||||
if (existingSponsorship == null)
|
||||
public async Task<bool> ValidateSponsorshipAsync(Guid sponsoredOrganizationId)
|
||||
{
|
||||
await CancelSponsorshipAsync(sponsoredOrganization, null);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (existingSponsorship.SponsoringOrganizationId == null || existingSponsorship.SponsoringOrganizationUserId == default || existingSponsorship.PlanSponsorshipType == null)
|
||||
{
|
||||
await CancelSponsorshipAsync(sponsoredOrganization, existingSponsorship);
|
||||
return false;
|
||||
}
|
||||
var sponsoredPlan = Utilities.StaticStore.GetSponsoredPlan(existingSponsorship.PlanSponsorshipType.Value);
|
||||
|
||||
var sponsoringOrganization = await _organizationRepository
|
||||
.GetByIdAsync(existingSponsorship.SponsoringOrganizationId.Value);
|
||||
if (sponsoringOrganization == null)
|
||||
{
|
||||
await CancelSponsorshipAsync(sponsoredOrganization, existingSponsorship);
|
||||
return false;
|
||||
}
|
||||
|
||||
var sponsoringOrgPlan = Utilities.StaticStore.GetPlan(sponsoringOrganization.PlanType);
|
||||
if (OrgDisabledForMoreThanGracePeriod(sponsoringOrganization) ||
|
||||
sponsoredPlan.SponsoringProductType != sponsoringOrgPlan.Product ||
|
||||
existingSponsorship.ToDelete ||
|
||||
SponsorshipIsSelfHostedOutOfSync(existingSponsorship))
|
||||
{
|
||||
await CancelSponsorshipAsync(sponsoredOrganization, existingSponsorship);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected async Task CancelSponsorshipAsync(Organization sponsoredOrganization, OrganizationSponsorship sponsorship = null)
|
||||
{
|
||||
if (sponsoredOrganization != null)
|
||||
{
|
||||
await _paymentService.RemoveOrganizationSponsorshipAsync(sponsoredOrganization, sponsorship);
|
||||
await _organizationRepository.UpsertAsync(sponsoredOrganization);
|
||||
|
||||
try
|
||||
var sponsoredOrganization = await _organizationRepository.GetByIdAsync(sponsoredOrganizationId);
|
||||
if (sponsoredOrganization == null)
|
||||
{
|
||||
if (sponsorship != null)
|
||||
return false;
|
||||
}
|
||||
|
||||
var existingSponsorship = await _organizationSponsorshipRepository
|
||||
.GetBySponsoredOrganizationIdAsync(sponsoredOrganizationId);
|
||||
|
||||
if (existingSponsorship == null)
|
||||
{
|
||||
await CancelSponsorshipAsync(sponsoredOrganization, null);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (existingSponsorship.SponsoringOrganizationId == null || existingSponsorship.SponsoringOrganizationUserId == default || existingSponsorship.PlanSponsorshipType == null)
|
||||
{
|
||||
await CancelSponsorshipAsync(sponsoredOrganization, existingSponsorship);
|
||||
return false;
|
||||
}
|
||||
var sponsoredPlan = Utilities.StaticStore.GetSponsoredPlan(existingSponsorship.PlanSponsorshipType.Value);
|
||||
|
||||
var sponsoringOrganization = await _organizationRepository
|
||||
.GetByIdAsync(existingSponsorship.SponsoringOrganizationId.Value);
|
||||
if (sponsoringOrganization == null)
|
||||
{
|
||||
await CancelSponsorshipAsync(sponsoredOrganization, existingSponsorship);
|
||||
return false;
|
||||
}
|
||||
|
||||
var sponsoringOrgPlan = Utilities.StaticStore.GetPlan(sponsoringOrganization.PlanType);
|
||||
if (OrgDisabledForMoreThanGracePeriod(sponsoringOrganization) ||
|
||||
sponsoredPlan.SponsoringProductType != sponsoringOrgPlan.Product ||
|
||||
existingSponsorship.ToDelete ||
|
||||
SponsorshipIsSelfHostedOutOfSync(existingSponsorship))
|
||||
{
|
||||
await CancelSponsorshipAsync(sponsoredOrganization, existingSponsorship);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected async Task CancelSponsorshipAsync(Organization sponsoredOrganization, OrganizationSponsorship sponsorship = null)
|
||||
{
|
||||
if (sponsoredOrganization != null)
|
||||
{
|
||||
await _paymentService.RemoveOrganizationSponsorshipAsync(sponsoredOrganization, sponsorship);
|
||||
await _organizationRepository.UpsertAsync(sponsoredOrganization);
|
||||
|
||||
try
|
||||
{
|
||||
await _mailService.SendFamiliesForEnterpriseSponsorshipRevertingEmailAsync(
|
||||
sponsoredOrganization.BillingEmailAddress(),
|
||||
sponsorship.ValidUntil ?? DateTime.UtcNow.AddDays(15));
|
||||
if (sponsorship != null)
|
||||
{
|
||||
await _mailService.SendFamiliesForEnterpriseSponsorshipRevertingEmailAsync(
|
||||
sponsoredOrganization.BillingEmailAddress(),
|
||||
sponsorship.ValidUntil ?? DateTime.UtcNow.AddDays(15));
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError("Error sending Family sponsorship removed email.", e);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError("Error sending Family sponsorship removed email.", e);
|
||||
}
|
||||
await base.DeleteSponsorshipAsync(sponsorship);
|
||||
}
|
||||
await base.DeleteSponsorshipAsync(sponsorship);
|
||||
|
||||
/// <summary>
|
||||
/// True if Sponsorship is from a self-hosted instance that has failed to sync for more than 6 months
|
||||
/// </summary>
|
||||
/// <param name="sponsorship"></param>
|
||||
private bool SponsorshipIsSelfHostedOutOfSync(OrganizationSponsorship sponsorship) =>
|
||||
sponsorship.LastSyncDate.HasValue &&
|
||||
DateTime.UtcNow.Subtract(sponsorship.LastSyncDate.Value).TotalDays > 182.5;
|
||||
|
||||
/// <summary>
|
||||
/// True if Organization is disabled and the expiration date is more than three months ago
|
||||
/// </summary>
|
||||
/// <param name="organization"></param>
|
||||
private bool OrgDisabledForMoreThanGracePeriod(Organization organization) =>
|
||||
!organization.Enabled &&
|
||||
(
|
||||
!organization.ExpirationDate.HasValue ||
|
||||
DateTime.UtcNow.Subtract(organization.ExpirationDate.Value).TotalDays > 93
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// True if Sponsorship is from a self-hosted instance that has failed to sync for more than 6 months
|
||||
/// </summary>
|
||||
/// <param name="sponsorship"></param>
|
||||
private bool SponsorshipIsSelfHostedOutOfSync(OrganizationSponsorship sponsorship) =>
|
||||
sponsorship.LastSyncDate.HasValue &&
|
||||
DateTime.UtcNow.Subtract(sponsorship.LastSyncDate.Value).TotalDays > 182.5;
|
||||
|
||||
/// <summary>
|
||||
/// True if Organization is disabled and the expiration date is more than three months ago
|
||||
/// </summary>
|
||||
/// <param name="organization"></param>
|
||||
private bool OrgDisabledForMoreThanGracePeriod(Organization organization) =>
|
||||
!organization.Enabled &&
|
||||
(
|
||||
!organization.ExpirationDate.HasValue ||
|
||||
DateTime.UtcNow.Subtract(organization.ExpirationDate.Value).TotalDays > 93
|
||||
);
|
||||
}
|
||||
|
||||
@@ -6,76 +6,77 @@ using Bit.Core.Repositories;
|
||||
using Bit.Core.Services;
|
||||
using Bit.Core.Utilities;
|
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise;
|
||||
|
||||
public class CreateSponsorshipCommand : ICreateSponsorshipCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise
|
||||
{
|
||||
private readonly IOrganizationSponsorshipRepository _organizationSponsorshipRepository;
|
||||
private readonly IUserService _userService;
|
||||
|
||||
public CreateSponsorshipCommand(IOrganizationSponsorshipRepository organizationSponsorshipRepository,
|
||||
IUserService userService)
|
||||
public class CreateSponsorshipCommand : ICreateSponsorshipCommand
|
||||
{
|
||||
_organizationSponsorshipRepository = organizationSponsorshipRepository;
|
||||
_userService = userService;
|
||||
}
|
||||
private readonly IOrganizationSponsorshipRepository _organizationSponsorshipRepository;
|
||||
private readonly IUserService _userService;
|
||||
|
||||
public async Task<OrganizationSponsorship> CreateSponsorshipAsync(Organization sponsoringOrg, OrganizationUser sponsoringOrgUser,
|
||||
PlanSponsorshipType sponsorshipType, string sponsoredEmail, string friendlyName)
|
||||
{
|
||||
var sponsoringUser = await _userService.GetUserByIdAsync(sponsoringOrgUser.UserId.Value);
|
||||
if (sponsoringUser == null || string.Equals(sponsoringUser.Email, sponsoredEmail, System.StringComparison.InvariantCultureIgnoreCase))
|
||||
public CreateSponsorshipCommand(IOrganizationSponsorshipRepository organizationSponsorshipRepository,
|
||||
IUserService userService)
|
||||
{
|
||||
throw new BadRequestException("Cannot offer a Families Organization Sponsorship to yourself. Choose a different email.");
|
||||
_organizationSponsorshipRepository = organizationSponsorshipRepository;
|
||||
_userService = userService;
|
||||
}
|
||||
|
||||
var requiredSponsoringProductType = StaticStore.GetSponsoredPlan(sponsorshipType)?.SponsoringProductType;
|
||||
if (requiredSponsoringProductType == null ||
|
||||
sponsoringOrg == null ||
|
||||
StaticStore.GetPlan(sponsoringOrg.PlanType).Product != requiredSponsoringProductType.Value)
|
||||
public async Task<OrganizationSponsorship> CreateSponsorshipAsync(Organization sponsoringOrg, OrganizationUser sponsoringOrgUser,
|
||||
PlanSponsorshipType sponsorshipType, string sponsoredEmail, string friendlyName)
|
||||
{
|
||||
throw new BadRequestException("Specified Organization cannot sponsor other organizations.");
|
||||
}
|
||||
|
||||
if (sponsoringOrgUser == null || sponsoringOrgUser.Status != OrganizationUserStatusType.Confirmed)
|
||||
{
|
||||
throw new BadRequestException("Only confirmed users can sponsor other organizations.");
|
||||
}
|
||||
|
||||
var existingOrgSponsorship = await _organizationSponsorshipRepository
|
||||
.GetBySponsoringOrganizationUserIdAsync(sponsoringOrgUser.Id);
|
||||
if (existingOrgSponsorship?.SponsoredOrganizationId != null)
|
||||
{
|
||||
throw new BadRequestException("Can only sponsor one organization per Organization User.");
|
||||
}
|
||||
|
||||
var sponsorship = new OrganizationSponsorship
|
||||
{
|
||||
SponsoringOrganizationId = sponsoringOrg.Id,
|
||||
SponsoringOrganizationUserId = sponsoringOrgUser.Id,
|
||||
FriendlyName = friendlyName,
|
||||
OfferedToEmail = sponsoredEmail,
|
||||
PlanSponsorshipType = sponsorshipType,
|
||||
};
|
||||
|
||||
if (existingOrgSponsorship != null)
|
||||
{
|
||||
// Replace existing invalid offer with our new sponsorship offer
|
||||
sponsorship.Id = existingOrgSponsorship.Id;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await _organizationSponsorshipRepository.UpsertAsync(sponsorship);
|
||||
return sponsorship;
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (sponsorship.Id != default)
|
||||
var sponsoringUser = await _userService.GetUserByIdAsync(sponsoringOrgUser.UserId.Value);
|
||||
if (sponsoringUser == null || string.Equals(sponsoringUser.Email, sponsoredEmail, System.StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
await _organizationSponsorshipRepository.DeleteAsync(sponsorship);
|
||||
throw new BadRequestException("Cannot offer a Families Organization Sponsorship to yourself. Choose a different email.");
|
||||
}
|
||||
|
||||
var requiredSponsoringProductType = StaticStore.GetSponsoredPlan(sponsorshipType)?.SponsoringProductType;
|
||||
if (requiredSponsoringProductType == null ||
|
||||
sponsoringOrg == null ||
|
||||
StaticStore.GetPlan(sponsoringOrg.PlanType).Product != requiredSponsoringProductType.Value)
|
||||
{
|
||||
throw new BadRequestException("Specified Organization cannot sponsor other organizations.");
|
||||
}
|
||||
|
||||
if (sponsoringOrgUser == null || sponsoringOrgUser.Status != OrganizationUserStatusType.Confirmed)
|
||||
{
|
||||
throw new BadRequestException("Only confirmed users can sponsor other organizations.");
|
||||
}
|
||||
|
||||
var existingOrgSponsorship = await _organizationSponsorshipRepository
|
||||
.GetBySponsoringOrganizationUserIdAsync(sponsoringOrgUser.Id);
|
||||
if (existingOrgSponsorship?.SponsoredOrganizationId != null)
|
||||
{
|
||||
throw new BadRequestException("Can only sponsor one organization per Organization User.");
|
||||
}
|
||||
|
||||
var sponsorship = new OrganizationSponsorship
|
||||
{
|
||||
SponsoringOrganizationId = sponsoringOrg.Id,
|
||||
SponsoringOrganizationUserId = sponsoringOrgUser.Id,
|
||||
FriendlyName = friendlyName,
|
||||
OfferedToEmail = sponsoredEmail,
|
||||
PlanSponsorshipType = sponsorshipType,
|
||||
};
|
||||
|
||||
if (existingOrgSponsorship != null)
|
||||
{
|
||||
// Replace existing invalid offer with our new sponsorship offer
|
||||
sponsorship.Id = existingOrgSponsorship.Id;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await _organizationSponsorshipRepository.UpsertAsync(sponsorship);
|
||||
return sponsorship;
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (sponsorship.Id != default)
|
||||
{
|
||||
await _organizationSponsorshipRepository.DeleteAsync(sponsorship);
|
||||
}
|
||||
throw;
|
||||
}
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Enums;
|
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Interfaces;
|
||||
|
||||
public interface ICreateSponsorshipCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Interfaces
|
||||
{
|
||||
Task<OrganizationSponsorship> CreateSponsorshipAsync(Organization sponsoringOrg, OrganizationUser sponsoringOrgUser,
|
||||
PlanSponsorshipType sponsorshipType, string sponsoredEmail, string friendlyName);
|
||||
public interface ICreateSponsorshipCommand
|
||||
{
|
||||
Task<OrganizationSponsorship> CreateSponsorshipAsync(Organization sponsoringOrg, OrganizationUser sponsoringOrgUser,
|
||||
PlanSponsorshipType sponsorshipType, string sponsoredEmail, string friendlyName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Interfaces;
|
||||
|
||||
public interface IOrganizationSponsorshipRenewCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Interfaces
|
||||
{
|
||||
Task UpdateExpirationDateAsync(Guid organizationId, DateTime expireDate);
|
||||
public interface IOrganizationSponsorshipRenewCommand
|
||||
{
|
||||
Task UpdateExpirationDateAsync(Guid organizationId, DateTime expireDate);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
using Bit.Core.Entities;
|
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Interfaces;
|
||||
|
||||
public interface IRemoveSponsorshipCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Interfaces
|
||||
{
|
||||
Task RemoveSponsorshipAsync(OrganizationSponsorship sponsorship);
|
||||
public interface IRemoveSponsorshipCommand
|
||||
{
|
||||
Task RemoveSponsorshipAsync(OrganizationSponsorship sponsorship);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
using Bit.Core.Entities;
|
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Interfaces;
|
||||
|
||||
public interface IRevokeSponsorshipCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Interfaces
|
||||
{
|
||||
Task RevokeSponsorshipAsync(OrganizationSponsorship sponsorship);
|
||||
public interface IRevokeSponsorshipCommand
|
||||
{
|
||||
Task RevokeSponsorshipAsync(OrganizationSponsorship sponsorship);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
using Bit.Core.Entities;
|
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Interfaces;
|
||||
|
||||
public interface ISendSponsorshipOfferCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Interfaces
|
||||
{
|
||||
Task BulkSendSponsorshipOfferAsync(string sponsoringOrgName, IEnumerable<OrganizationSponsorship> invites);
|
||||
Task SendSponsorshipOfferAsync(OrganizationSponsorship sponsorship, string sponsoringOrgName);
|
||||
Task SendSponsorshipOfferAsync(Organization sponsoringOrg, OrganizationUser sponsoringOrgUser,
|
||||
OrganizationSponsorship sponsorship);
|
||||
public interface ISendSponsorshipOfferCommand
|
||||
{
|
||||
Task BulkSendSponsorshipOfferAsync(string sponsoringOrgName, IEnumerable<OrganizationSponsorship> invites);
|
||||
Task SendSponsorshipOfferAsync(OrganizationSponsorship sponsorship, string sponsoringOrgName);
|
||||
Task SendSponsorshipOfferAsync(Organization sponsoringOrg, OrganizationUser sponsoringOrgUser,
|
||||
OrganizationSponsorship sponsorship);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
using Bit.Core.Entities;
|
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Interfaces;
|
||||
|
||||
public interface ISetUpSponsorshipCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Interfaces
|
||||
{
|
||||
Task SetUpSponsorshipAsync(OrganizationSponsorship sponsorship,
|
||||
Organization sponsoredOrganization);
|
||||
public interface ISetUpSponsorshipCommand
|
||||
{
|
||||
Task SetUpSponsorshipAsync(OrganizationSponsorship sponsorship,
|
||||
Organization sponsoredOrganization);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Models.Data.Organizations.OrganizationSponsorships;
|
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Interfaces;
|
||||
|
||||
public interface ISelfHostedSyncSponsorshipsCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Interfaces
|
||||
{
|
||||
Task SyncOrganization(Guid organizationId, Guid cloudOrganizationId, OrganizationConnection billingSyncConnection);
|
||||
}
|
||||
public interface ISelfHostedSyncSponsorshipsCommand
|
||||
{
|
||||
Task SyncOrganization(Guid organizationId, Guid cloudOrganizationId, OrganizationConnection billingSyncConnection);
|
||||
}
|
||||
|
||||
public interface ICloudSyncSponsorshipsCommand
|
||||
{
|
||||
Task<(OrganizationSponsorshipSyncData, IEnumerable<OrganizationSponsorship>)> SyncOrganization(Organization sponsoringOrg, IEnumerable<OrganizationSponsorshipData> sponsorshipsData);
|
||||
public interface ICloudSyncSponsorshipsCommand
|
||||
{
|
||||
Task<(OrganizationSponsorshipSyncData, IEnumerable<OrganizationSponsorship>)> SyncOrganization(Organization sponsoringOrg, IEnumerable<OrganizationSponsorshipData> sponsorshipsData);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
using Bit.Core.Entities;
|
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Interfaces;
|
||||
|
||||
public interface IValidateBillingSyncKeyCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Interfaces
|
||||
{
|
||||
Task<bool> ValidateBillingSyncKeyAsync(Organization organization, string billingSyncKey);
|
||||
public interface IValidateBillingSyncKeyCommand
|
||||
{
|
||||
Task<bool> ValidateBillingSyncKeyAsync(Organization organization, string billingSyncKey);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
using Bit.Core.Entities;
|
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Interfaces;
|
||||
|
||||
public interface IValidateRedemptionTokenCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Interfaces
|
||||
{
|
||||
Task<(bool valid, OrganizationSponsorship sponsorship)> ValidateRedemptionTokenAsync(string encryptedToken, string sponsoredUserEmail);
|
||||
public interface IValidateRedemptionTokenCommand
|
||||
{
|
||||
Task<(bool valid, OrganizationSponsorship sponsorship)> ValidateRedemptionTokenAsync(string encryptedToken, string sponsoredUserEmail);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Interfaces;
|
||||
|
||||
public interface IValidateSponsorshipCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Interfaces
|
||||
{
|
||||
Task<bool> ValidateSponsorshipAsync(Guid sponsoredOrganizationId);
|
||||
public interface IValidateSponsorshipCommand
|
||||
{
|
||||
Task<bool> ValidateSponsorshipAsync(Guid sponsoredOrganizationId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,30 +3,31 @@ using Bit.Core.Exceptions;
|
||||
using Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.Interfaces;
|
||||
using Bit.Core.Repositories;
|
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.SelfHosted;
|
||||
|
||||
public class SelfHostedRevokeSponsorshipCommand : CancelSponsorshipCommand, IRevokeSponsorshipCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.SelfHosted
|
||||
{
|
||||
public SelfHostedRevokeSponsorshipCommand(
|
||||
IOrganizationSponsorshipRepository organizationSponsorshipRepository,
|
||||
IOrganizationRepository organizationRepository) : base(organizationSponsorshipRepository, organizationRepository)
|
||||
public class SelfHostedRevokeSponsorshipCommand : CancelSponsorshipCommand, IRevokeSponsorshipCommand
|
||||
{
|
||||
}
|
||||
|
||||
public async Task RevokeSponsorshipAsync(OrganizationSponsorship sponsorship)
|
||||
{
|
||||
if (sponsorship == null)
|
||||
public SelfHostedRevokeSponsorshipCommand(
|
||||
IOrganizationSponsorshipRepository organizationSponsorshipRepository,
|
||||
IOrganizationRepository organizationRepository) : base(organizationSponsorshipRepository, organizationRepository)
|
||||
{
|
||||
throw new BadRequestException("You are not currently sponsoring an organization.");
|
||||
}
|
||||
|
||||
if (sponsorship.LastSyncDate == null)
|
||||
public async Task RevokeSponsorshipAsync(OrganizationSponsorship sponsorship)
|
||||
{
|
||||
await base.DeleteSponsorshipAsync(sponsorship);
|
||||
}
|
||||
else
|
||||
{
|
||||
await MarkToDeleteSponsorshipAsync(sponsorship);
|
||||
if (sponsorship == null)
|
||||
{
|
||||
throw new BadRequestException("You are not currently sponsoring an organization.");
|
||||
}
|
||||
|
||||
if (sponsorship.LastSyncDate == null)
|
||||
{
|
||||
await base.DeleteSponsorshipAsync(sponsorship);
|
||||
}
|
||||
else
|
||||
{
|
||||
await MarkToDeleteSponsorshipAsync(sponsorship);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,120 +11,121 @@ using Bit.Core.Settings;
|
||||
using Bit.Core.Utilities;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.SelfHosted;
|
||||
|
||||
public class SelfHostedSyncSponsorshipsCommand : BaseIdentityClientService, ISelfHostedSyncSponsorshipsCommand
|
||||
namespace Bit.Core.OrganizationFeatures.OrganizationSponsorships.FamiliesForEnterprise.SelfHosted
|
||||
{
|
||||
private readonly IGlobalSettings _globalSettings;
|
||||
private readonly IOrganizationSponsorshipRepository _organizationSponsorshipRepository;
|
||||
private readonly IOrganizationUserRepository _organizationUserRepository;
|
||||
private readonly IOrganizationConnectionRepository _organizationConnectionRepository;
|
||||
|
||||
public SelfHostedSyncSponsorshipsCommand(
|
||||
IHttpClientFactory httpFactory,
|
||||
IOrganizationSponsorshipRepository organizationSponsorshipRepository,
|
||||
IOrganizationUserRepository organizationUserRepository,
|
||||
IOrganizationConnectionRepository organizationConnectionRepository,
|
||||
IGlobalSettings globalSettings,
|
||||
ILogger<SelfHostedSyncSponsorshipsCommand> logger)
|
||||
: base(
|
||||
httpFactory,
|
||||
globalSettings.Installation.ApiUri,
|
||||
globalSettings.Installation.IdentityUri,
|
||||
"api.installation",
|
||||
$"installation.{globalSettings.Installation.Id}",
|
||||
globalSettings.Installation.Key,
|
||||
logger)
|
||||
public class SelfHostedSyncSponsorshipsCommand : BaseIdentityClientService, ISelfHostedSyncSponsorshipsCommand
|
||||
{
|
||||
_globalSettings = globalSettings;
|
||||
_organizationUserRepository = organizationUserRepository;
|
||||
_organizationSponsorshipRepository = organizationSponsorshipRepository;
|
||||
_organizationConnectionRepository = organizationConnectionRepository;
|
||||
}
|
||||
private readonly IGlobalSettings _globalSettings;
|
||||
private readonly IOrganizationSponsorshipRepository _organizationSponsorshipRepository;
|
||||
private readonly IOrganizationUserRepository _organizationUserRepository;
|
||||
private readonly IOrganizationConnectionRepository _organizationConnectionRepository;
|
||||
|
||||
public async Task SyncOrganization(Guid organizationId, Guid cloudOrganizationId, OrganizationConnection billingSyncConnection)
|
||||
{
|
||||
if (!_globalSettings.EnableCloudCommunication)
|
||||
public SelfHostedSyncSponsorshipsCommand(
|
||||
IHttpClientFactory httpFactory,
|
||||
IOrganizationSponsorshipRepository organizationSponsorshipRepository,
|
||||
IOrganizationUserRepository organizationUserRepository,
|
||||
IOrganizationConnectionRepository organizationConnectionRepository,
|
||||
IGlobalSettings globalSettings,
|
||||
ILogger<SelfHostedSyncSponsorshipsCommand> logger)
|
||||
: base(
|
||||
httpFactory,
|
||||
globalSettings.Installation.ApiUri,
|
||||
globalSettings.Installation.IdentityUri,
|
||||
"api.installation",
|
||||
$"installation.{globalSettings.Installation.Id}",
|
||||
globalSettings.Installation.Key,
|
||||
logger)
|
||||
{
|
||||
throw new BadRequestException("Failed to sync instance with cloud - Cloud communication is disabled in global settings");
|
||||
}
|
||||
if (!billingSyncConnection.Enabled)
|
||||
{
|
||||
throw new BadRequestException($"Billing Sync Key disabled for organization {organizationId}");
|
||||
}
|
||||
if (string.IsNullOrWhiteSpace(billingSyncConnection.Config))
|
||||
{
|
||||
throw new BadRequestException($"No Billing Sync Key known for organization {organizationId}");
|
||||
}
|
||||
var billingSyncConfig = billingSyncConnection.GetConfig<BillingSyncConfig>();
|
||||
if (billingSyncConfig == null || string.IsNullOrWhiteSpace(billingSyncConfig.BillingSyncKey))
|
||||
{
|
||||
throw new BadRequestException($"Failed to get Billing Sync Key for organization {organizationId}");
|
||||
_globalSettings = globalSettings;
|
||||
_organizationUserRepository = organizationUserRepository;
|
||||
_organizationSponsorshipRepository = organizationSponsorshipRepository;
|
||||
_organizationConnectionRepository = organizationConnectionRepository;
|
||||
}
|
||||
|
||||
var organizationSponsorshipsDict = (await _organizationSponsorshipRepository.GetManyBySponsoringOrganizationAsync(organizationId))
|
||||
.ToDictionary(i => i.SponsoringOrganizationUserId);
|
||||
if (!organizationSponsorshipsDict.Any())
|
||||
public async Task SyncOrganization(Guid organizationId, Guid cloudOrganizationId, OrganizationConnection billingSyncConnection)
|
||||
{
|
||||
_logger.LogInformation($"No existing sponsorships to sync for organization {organizationId}");
|
||||
return;
|
||||
}
|
||||
var syncedSponsorships = new List<OrganizationSponsorshipData>();
|
||||
|
||||
foreach (var orgSponsorshipsBatch in CoreHelpers.Batch(organizationSponsorshipsDict.Values, 1000))
|
||||
{
|
||||
var response = await SendAsync<OrganizationSponsorshipSyncRequestModel, OrganizationSponsorshipSyncResponseModel>(HttpMethod.Post, "organization/sponsorship/sync", new OrganizationSponsorshipSyncRequestModel
|
||||
if (!_globalSettings.EnableCloudCommunication)
|
||||
{
|
||||
BillingSyncKey = billingSyncConfig.BillingSyncKey,
|
||||
SponsoringOrganizationCloudId = cloudOrganizationId,
|
||||
SponsorshipsBatch = orgSponsorshipsBatch.Select(s => new OrganizationSponsorshipRequestModel(s))
|
||||
throw new BadRequestException("Failed to sync instance with cloud - Cloud communication is disabled in global settings");
|
||||
}
|
||||
if (!billingSyncConnection.Enabled)
|
||||
{
|
||||
throw new BadRequestException($"Billing Sync Key disabled for organization {organizationId}");
|
||||
}
|
||||
if (string.IsNullOrWhiteSpace(billingSyncConnection.Config))
|
||||
{
|
||||
throw new BadRequestException($"No Billing Sync Key known for organization {organizationId}");
|
||||
}
|
||||
var billingSyncConfig = billingSyncConnection.GetConfig<BillingSyncConfig>();
|
||||
if (billingSyncConfig == null || string.IsNullOrWhiteSpace(billingSyncConfig.BillingSyncKey))
|
||||
{
|
||||
throw new BadRequestException($"Failed to get Billing Sync Key for organization {organizationId}");
|
||||
}
|
||||
|
||||
var organizationSponsorshipsDict = (await _organizationSponsorshipRepository.GetManyBySponsoringOrganizationAsync(organizationId))
|
||||
.ToDictionary(i => i.SponsoringOrganizationUserId);
|
||||
if (!organizationSponsorshipsDict.Any())
|
||||
{
|
||||
_logger.LogInformation($"No existing sponsorships to sync for organization {organizationId}");
|
||||
return;
|
||||
}
|
||||
var syncedSponsorships = new List<OrganizationSponsorshipData>();
|
||||
|
||||
foreach (var orgSponsorshipsBatch in CoreHelpers.Batch(organizationSponsorshipsDict.Values, 1000))
|
||||
{
|
||||
var response = await SendAsync<OrganizationSponsorshipSyncRequestModel, OrganizationSponsorshipSyncResponseModel>(HttpMethod.Post, "organization/sponsorship/sync", new OrganizationSponsorshipSyncRequestModel
|
||||
{
|
||||
BillingSyncKey = billingSyncConfig.BillingSyncKey,
|
||||
SponsoringOrganizationCloudId = cloudOrganizationId,
|
||||
SponsorshipsBatch = orgSponsorshipsBatch.Select(s => new OrganizationSponsorshipRequestModel(s))
|
||||
});
|
||||
|
||||
if (response == null)
|
||||
{
|
||||
_logger.LogDebug("Organization sync failed for '{OrgId}'", organizationId);
|
||||
throw new BadRequestException("Organization sync failed");
|
||||
}
|
||||
|
||||
syncedSponsorships.AddRange(response.ToOrganizationSponsorshipSync().SponsorshipsBatch);
|
||||
}
|
||||
|
||||
var sponsorshipsToDelete = syncedSponsorships.Where(s => s.CloudSponsorshipRemoved).Select(i => organizationSponsorshipsDict[i.SponsoringOrganizationUserId].Id);
|
||||
var sponsorshipsToUpsert = syncedSponsorships.Where(s => !s.CloudSponsorshipRemoved).Select(i =>
|
||||
{
|
||||
var existingSponsorship = organizationSponsorshipsDict[i.SponsoringOrganizationUserId];
|
||||
if (existingSponsorship != null)
|
||||
{
|
||||
existingSponsorship.LastSyncDate = i.LastSyncDate;
|
||||
existingSponsorship.ValidUntil = i.ValidUntil;
|
||||
existingSponsorship.ToDelete = i.ToDelete;
|
||||
}
|
||||
else
|
||||
{
|
||||
// shouldn't occur, added in case self hosted loses a sponsorship
|
||||
existingSponsorship = new OrganizationSponsorship
|
||||
{
|
||||
SponsoringOrganizationId = organizationId,
|
||||
SponsoringOrganizationUserId = i.SponsoringOrganizationUserId,
|
||||
FriendlyName = i.FriendlyName,
|
||||
OfferedToEmail = i.OfferedToEmail,
|
||||
PlanSponsorshipType = i.PlanSponsorshipType,
|
||||
LastSyncDate = i.LastSyncDate,
|
||||
ValidUntil = i.ValidUntil,
|
||||
ToDelete = i.ToDelete
|
||||
};
|
||||
}
|
||||
return existingSponsorship;
|
||||
});
|
||||
|
||||
if (response == null)
|
||||
if (sponsorshipsToDelete.Any())
|
||||
{
|
||||
_logger.LogDebug("Organization sync failed for '{OrgId}'", organizationId);
|
||||
throw new BadRequestException("Organization sync failed");
|
||||
await _organizationSponsorshipRepository.DeleteManyAsync(sponsorshipsToDelete);
|
||||
}
|
||||
if (sponsorshipsToUpsert.Any())
|
||||
{
|
||||
await _organizationSponsorshipRepository.UpsertManyAsync(sponsorshipsToUpsert);
|
||||
}
|
||||
|
||||
syncedSponsorships.AddRange(response.ToOrganizationSponsorshipSync().SponsorshipsBatch);
|
||||
}
|
||||
|
||||
var sponsorshipsToDelete = syncedSponsorships.Where(s => s.CloudSponsorshipRemoved).Select(i => organizationSponsorshipsDict[i.SponsoringOrganizationUserId].Id);
|
||||
var sponsorshipsToUpsert = syncedSponsorships.Where(s => !s.CloudSponsorshipRemoved).Select(i =>
|
||||
{
|
||||
var existingSponsorship = organizationSponsorshipsDict[i.SponsoringOrganizationUserId];
|
||||
if (existingSponsorship != null)
|
||||
{
|
||||
existingSponsorship.LastSyncDate = i.LastSyncDate;
|
||||
existingSponsorship.ValidUntil = i.ValidUntil;
|
||||
existingSponsorship.ToDelete = i.ToDelete;
|
||||
}
|
||||
else
|
||||
{
|
||||
// shouldn't occur, added in case self hosted loses a sponsorship
|
||||
existingSponsorship = new OrganizationSponsorship
|
||||
{
|
||||
SponsoringOrganizationId = organizationId,
|
||||
SponsoringOrganizationUserId = i.SponsoringOrganizationUserId,
|
||||
FriendlyName = i.FriendlyName,
|
||||
OfferedToEmail = i.OfferedToEmail,
|
||||
PlanSponsorshipType = i.PlanSponsorshipType,
|
||||
LastSyncDate = i.LastSyncDate,
|
||||
ValidUntil = i.ValidUntil,
|
||||
ToDelete = i.ToDelete
|
||||
};
|
||||
}
|
||||
return existingSponsorship;
|
||||
});
|
||||
|
||||
if (sponsorshipsToDelete.Any())
|
||||
{
|
||||
await _organizationSponsorshipRepository.DeleteManyAsync(sponsorshipsToDelete);
|
||||
}
|
||||
if (sponsorshipsToUpsert.Any())
|
||||
{
|
||||
await _organizationSponsorshipRepository.UpsertManyAsync(sponsorshipsToUpsert);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user