Files
server/src/Core/Models/Table/Organization.cs

188 lines
5.7 KiB
C#
Raw Normal View History

using System;
using Bit.Core.Utilities;
using Bit.Core.Enums;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Linq;
2017-03-08 21:45:08 -05:00
namespace Bit.Core.Models.Table
{
public class Organization : ITableObject<Guid>, ISubscriber, IStorable, IStorableSubscriber, IRevisable, IReferenceable
{
private Dictionary<TwoFactorProviderType, TwoFactorProvider> _twoFactorProviders;
public Guid Id { get; set; }
2020-06-25 16:42:29 -04:00
public string Identifier { 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; }
public string Plan { get; set; }
public PlanType PlanType { get; set; }
Support large organization sync (#1311) * Increase organization max seat size from 30k to 2b (#1274) * Increase organization max seat size from 30k to 2b * PR review. Do not modify unless state matches expected * Organization sync simultaneous event reporting (#1275) * Split up azure messages according to max size * Allow simultaneous login of organization user events * Early resolve small event lists * Clarify logic Co-authored-by: Chad Scharf <3904944+cscharf@users.noreply.github.com> * Improve readability This comes at the cost of multiple serializations, but the improvement in wire-time should more than make up for this on message where serialization time matters Co-authored-by: Chad Scharf <3904944+cscharf@users.noreply.github.com> * Queue emails (#1286) * Extract common Azure queue methods * Do not use internal entity framework namespace * Prefer IEnumerable to IList unless needed All of these implementations were just using `Count == 1`, which is easily replicated. This will be used when abstracting Azure queues * Add model for azure queue message * Abstract Azure queue for reuse * Creat service to enqueue mail messages for later processing Azure queue mail service uses Azure queues. Blocking just blocks until all the work is done -- This is how emailing works today * Provide mail queue service to DI * Queue organization invite emails for later processing All emails can later be added to this queue * Create Admin hosted service to process enqueued mail messages * Prefer constructors to static generators * Mass delete organization users (#1287) * Add delete many to Organization Users * Correct formatting * Remove erroneous migration * Clarify parameter name * Formatting fixes * Simplify bump account revision sproc * Formatting fixes * Match file names to objects * Indicate if large import is expected * Early pull all existing users we were planning on inviting (#1290) * Early pull all existing users we were planning on inviting * Improve sproc name * Batch upsert org users (#1289) * Add UpsertMany sprocs to OrganizationUser * Add method to create TVPs from any object. Uses DbOrder attribute to generate. Sproc will fail unless TVP column order matches that of the db type * Combine migrations * Correct formatting * Include sql objects in sql project * Keep consisten parameter names * Batch deletes for performance * Correct formatting * consolidate migrations * Use batch methods in OrganizationImport * Declare @BatchSize * Transaction names limited to 32 chars Drop sproc before creating it if it exists * Update import tests * Allow for more users in org upgrades * Fix formatting * Improve class hierarchy structure * Use name tuple types * Fix formatting * Front load all reflection * Format constructor * Simplify ToTvp as class-specific extension Co-authored-by: Chad Scharf <3904944+cscharf@users.noreply.github.com>
2021-05-17 09:43:02 -05:00
public int? Seats { get; set; }
2017-04-27 09:19:30 -04:00
public short? MaxCollections { get; set; }
2020-01-15 15:00:54 -05:00
public bool UsePolicies { get; set; }
public bool UseSso { 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; }
public bool Use2fa { get; set; }
2019-03-02 15:09:33 -05:00
public bool UseApi { get; set; }
public bool UseResetPassword { get; set; }
public bool SelfHost { get; set; }
public bool UsersGetPremium { get; set; }
public long? Storage { get; set; }
public short? MaxStorageGb { get; set; }
public GatewayType? Gateway { get; set; }
public string GatewayCustomerId { get; set; }
public string GatewaySubscriptionId { get; set; }
public string ReferenceData { get; set; }
2017-04-10 22:33:39 -04:00
public bool Enabled { get; set; } = true;
public string LicenseKey { get; set; }
2019-03-02 15:09:33 -05:00
public string ApiKey { get; set; }
public string PublicKey { get; set; }
public string PrivateKey { get; set; }
public string TwoFactorProviders { get; set; }
public DateTime? ExpirationDate { get; set; }
public DateTime CreationDate { get; internal set; } = DateTime.UtcNow;
public DateTime RevisionDate { get; internal set; } = DateTime.UtcNow;
public void SetNewId()
{
if (Id == default(Guid))
{
Id = CoreHelpers.GenerateComb();
}
}
2017-07-06 14:55:58 -04:00
public string BillingEmailAddress()
{
2019-06-12 22:08:53 -04:00
return BillingEmail?.ToLowerInvariant()?.Trim();
2017-07-06 14:55:58 -04:00
}
public string BillingName()
{
return BusinessName;
}
public string BraintreeCustomerIdPrefix()
{
return "o";
}
2019-02-01 09:18:34 -05:00
public string BraintreeIdField()
{
return "organization_id";
}
public string GatewayIdField()
{
return "organizationId";
}
2019-09-17 16:55:40 -04:00
public bool IsUser()
{
return false;
}
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;
if (!Storage.HasValue)
{
return maxStorageBytes;
}
return maxStorageBytes - Storage.Value;
}
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)
{
if (!providers.Any())
2018-04-03 14:31:33 -04:00
{
TwoFactorProviders = null;
_twoFactorProviders = null;
return;
}
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];
}
}
}