2017-03-02 00:15:05 -05:00
|
|
|
|
using System;
|
|
|
|
|
|
using Bit.Core.Utilities;
|
|
|
|
|
|
using Bit.Core.Enums;
|
2017-07-28 14:24:07 -04:00
|
|
|
|
using Bit.Core.Services;
|
|
|
|
|
|
using Bit.Core.Exceptions;
|
2018-04-02 14:53:19 -04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
using System.Linq;
|
2017-03-02 00:15:05 -05:00
|
|
|
|
|
2017-03-08 21:45:08 -05:00
|
|
|
|
namespace Bit.Core.Models.Table
|
2017-03-02 00:15:05 -05:00
|
|
|
|
{
|
2017-07-14 09:05:15 -04:00
|
|
|
|
public class Organization : ITableObject<Guid>, ISubscriber, IStorable, IStorableSubscriber, IRevisable
|
2017-03-02 00:15:05 -05:00
|
|
|
|
{
|
2018-04-02 14:53:19 -04:00
|
|
|
|
private Dictionary<TwoFactorProviderType, TwoFactorProvider> _twoFactorProviders;
|
|
|
|
|
|
|
2017-03-02 00:15:05 -05:00
|
|
|
|
public Guid Id { get; set; }
|
|
|
|
|
|
public string Name { get; set; }
|
2017-04-04 12:57:50 -04:00
|
|
|
|
public string BusinessName { get; set; }
|
2017-10-25 12:41:07 -04:00
|
|
|
|
public string BusinessAddress1 { get; set; }
|
|
|
|
|
|
public string BusinessAddress2 { get; set; }
|
|
|
|
|
|
public string BusinessAddress3 { get; set; }
|
|
|
|
|
|
public string BusinessCountry { get; set; }
|
|
|
|
|
|
public string BusinessTaxNumber { get; set; }
|
2017-04-04 12:57:50 -04:00
|
|
|
|
public string BillingEmail { get; set; }
|
2017-03-03 00:07:11 -05:00
|
|
|
|
public string Plan { get; set; }
|
|
|
|
|
|
public PlanType PlanType { get; set; }
|
2017-04-10 10:44:27 -04:00
|
|
|
|
public short? Seats { get; set; }
|
2017-04-27 09:19:30 -04:00
|
|
|
|
public short? MaxCollections { get; set; }
|
2017-05-08 14:22:40 -04:00
|
|
|
|
public bool UseGroups { get; set; }
|
2017-05-20 15:31:16 -04:00
|
|
|
|
public bool UseDirectory { get; set; }
|
2017-12-14 15:48:44 -05:00
|
|
|
|
public bool UseEvents { get; set; }
|
2017-07-07 14:08:30 -04:00
|
|
|
|
public bool UseTotp { get; set; }
|
2018-04-02 14:53:19 -04:00
|
|
|
|
public bool Use2fa { get; set; }
|
2017-08-09 17:01:37 -04:00
|
|
|
|
public bool SelfHost { get; set; }
|
2017-11-06 16:01:58 -05:00
|
|
|
|
public bool UsersGetPremium { get; set; }
|
2017-06-30 14:41:57 -04:00
|
|
|
|
public long? Storage { get; set; }
|
|
|
|
|
|
public short? MaxStorageGb { get; set; }
|
2017-07-28 14:24:07 -04:00
|
|
|
|
public GatewayType? Gateway { get; set; }
|
|
|
|
|
|
public string GatewayCustomerId { get; set; }
|
|
|
|
|
|
public string GatewaySubscriptionId { get; set; }
|
2017-04-10 22:33:39 -04:00
|
|
|
|
public bool Enabled { get; set; } = true;
|
2017-08-09 17:01:37 -04:00
|
|
|
|
public string LicenseKey { get; set; }
|
2018-04-02 14:53:19 -04:00
|
|
|
|
public string TwoFactorProviders { get; set; }
|
2017-08-12 10:43:52 -04:00
|
|
|
|
public DateTime? ExpirationDate { get; set; }
|
2017-03-02 00:15:05 -05:00
|
|
|
|
public DateTime CreationDate { get; internal set; } = DateTime.UtcNow;
|
|
|
|
|
|
public DateTime RevisionDate { get; internal set; } = DateTime.UtcNow;
|
|
|
|
|
|
|
|
|
|
|
|
public void SetNewId()
|
|
|
|
|
|
{
|
2017-04-26 16:14:15 -04:00
|
|
|
|
if(Id == default(Guid))
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = CoreHelpers.GenerateComb();
|
|
|
|
|
|
}
|
2017-03-02 00:15:05 -05:00
|
|
|
|
}
|
2017-06-30 14:41:57 -04:00
|
|
|
|
|
2017-07-06 14:55:58 -04:00
|
|
|
|
public string BillingEmailAddress()
|
|
|
|
|
|
{
|
|
|
|
|
|
return BillingEmail;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string BillingName()
|
|
|
|
|
|
{
|
|
|
|
|
|
return BusinessName;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-06-30 14:41:57 -04:00
|
|
|
|
public long StorageBytesRemaining()
|
|
|
|
|
|
{
|
|
|
|
|
|
if(!MaxStorageGb.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-07-06 14:55:58 -04:00
|
|
|
|
return StorageBytesRemaining(MaxStorageGb.Value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public long StorageBytesRemaining(short maxStorageGb)
|
|
|
|
|
|
{
|
|
|
|
|
|
var maxStorageBytes = maxStorageGb * 1073741824L;
|
2017-06-30 14:41:57 -04:00
|
|
|
|
if(!Storage.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
return maxStorageBytes;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return maxStorageBytes - Storage.Value;
|
|
|
|
|
|
}
|
2017-07-28 14:24:07 -04:00
|
|
|
|
|
|
|
|
|
|
public IPaymentService GetPaymentService(GlobalSettings globalSettings)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(Gateway == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("No gateway.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
IPaymentService paymentService = null;
|
|
|
|
|
|
switch(Gateway)
|
|
|
|
|
|
{
|
|
|
|
|
|
case GatewayType.Stripe:
|
|
|
|
|
|
paymentService = new StripePaymentService();
|
|
|
|
|
|
break;
|
|
|
|
|
|
case GatewayType.Braintree:
|
|
|
|
|
|
paymentService = new BraintreePaymentService(globalSettings);
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
throw new NotSupportedException("Unsupported gateway.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return paymentService;
|
|
|
|
|
|
}
|
2018-04-02 14:53:19 -04:00
|
|
|
|
|
|
|
|
|
|
public Dictionary<TwoFactorProviderType, TwoFactorProvider> GetTwoFactorProviders()
|
|
|
|
|
|
{
|
|
|
|
|
|
if(string.IsNullOrWhiteSpace(TwoFactorProviders))
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
if(_twoFactorProviders == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_twoFactorProviders =
|
|
|
|
|
|
JsonConvert.DeserializeObject<Dictionary<TwoFactorProviderType, TwoFactorProvider>>(
|
|
|
|
|
|
TwoFactorProviders);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return _twoFactorProviders;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch(JsonSerializationException)
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void SetTwoFactorProviders(Dictionary<TwoFactorProviderType, TwoFactorProvider> providers)
|
|
|
|
|
|
{
|
|
|
|
|
|
TwoFactorProviders = JsonConvert.SerializeObject(providers, new JsonSerializerSettings
|
|
|
|
|
|
{
|
|
|
|
|
|
ContractResolver = new EnumKeyResolver<byte>()
|
|
|
|
|
|
});
|
|
|
|
|
|
_twoFactorProviders = providers;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool TwoFactorProviderIsEnabled(TwoFactorProviderType provider)
|
|
|
|
|
|
{
|
|
|
|
|
|
var providers = GetTwoFactorProviders();
|
|
|
|
|
|
if(providers == null || !providers.ContainsKey(provider))
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return providers[provider].Enabled && Use2fa;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool TwoFactorIsEnabled()
|
|
|
|
|
|
{
|
|
|
|
|
|
var providers = GetTwoFactorProviders();
|
|
|
|
|
|
if(providers == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return providers.Any(p => (p.Value?.Enabled ?? false) && Use2fa);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public TwoFactorProvider GetTwoFactorProvider(TwoFactorProviderType provider)
|
|
|
|
|
|
{
|
|
|
|
|
|
var providers = GetTwoFactorProviders();
|
|
|
|
|
|
if(providers == null || !providers.ContainsKey(provider))
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return providers[provider];
|
|
|
|
|
|
}
|
2017-03-02 00:15:05 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|