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

123 lines
3.8 KiB
C#
Raw Normal View History

2015-12-08 22:57:38 -05:00
using System;
using Bit.Core.Enums;
using Bit.Core.Utilities;
using System.Collections.Generic;
using Newtonsoft.Json;
2017-06-19 22:25:19 -04:00
using System.Linq;
2015-12-08 22:57:38 -05:00
2017-03-08 21:45:08 -05:00
namespace Bit.Core.Models.Table
2015-12-08 22:57:38 -05:00
{
public class User : IDataObject<Guid>
2015-12-08 22:57:38 -05:00
{
private Dictionary<TwoFactorProviderType, TwoFactorProvider> _twoFactorProviders;
public Guid Id { get; set; }
2015-12-08 22:57:38 -05:00
public string Name { get; set; }
public string Email { get; set; }
2016-02-21 00:50:53 -05:00
public bool EmailVerified { get; set; }
2015-12-08 22:57:38 -05:00
public string MasterPassword { get; set; }
public string MasterPasswordHint { get; set; }
2016-01-07 23:01:01 -05:00
public string Culture { get; set; } = "en-US";
2015-12-08 22:57:38 -05:00
public string SecurityStamp { get; set; }
public string TwoFactorProviders { get; set; }
public string TwoFactorRecoveryCode { get; set; }
public string EquivalentDomains { get; set; }
public string ExcludedGlobalEquivalentDomains { get; set; }
public DateTime AccountRevisionDate { get; internal set; } = DateTime.UtcNow;
2017-05-31 09:54:32 -04:00
public string Key { get; set; }
public string PublicKey { get; set; }
public string PrivateKey { get; set; }
public bool Premium { get; set; }
public long? Storage { get; set; }
public short? MaxStorageGb { get; set; }
2015-12-08 22:57:38 -05:00
public DateTime CreationDate { get; internal set; } = DateTime.UtcNow;
public DateTime RevisionDate { get; internal set; } = DateTime.UtcNow;
public void SetNewId()
{
Id = CoreHelpers.GenerateComb();
}
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;
}
public bool TwoFactorIsEnabled()
{
2017-06-19 22:25:19 -04:00
var providers = GetTwoFactorProviders();
if(providers == null)
{
return false;
}
return providers.Any(p => p.Value?.Enabled ?? false);
}
public TwoFactorProvider GetTwoFactorProvider(TwoFactorProviderType provider)
{
var providers = GetTwoFactorProviders();
if(providers == null || !providers.ContainsKey(provider))
{
return null;
}
return providers[provider];
}
public long StorageBytesRemaining()
{
if(!MaxStorageGb.HasValue)
{
return 0;
}
var maxStorageBytes = MaxStorageGb.Value * 1073741824L;
if(!Storage.HasValue)
{
return maxStorageBytes;
}
return maxStorageBytes - Storage.Value;
}
2015-12-08 22:57:38 -05:00
}
}