mirror of
https://github.com/bitwarden/server
synced 2025-12-27 21:53:24 +00:00
[PM-14476] Avoid multiple lookups in dictionaries (#4973)
* Avoid multiple lookups in dictionaries * Consistency in fallback to empty CollectionIds * Readability at the cost of lines changed * Readability * Changes after running dotnet format
This commit is contained in:
@@ -50,14 +50,7 @@ public class InMemoryApplicationCacheService : IApplicationCacheService
|
||||
await InitProviderAbilitiesAsync();
|
||||
var newAbility = new ProviderAbility(provider);
|
||||
|
||||
if (_providerAbilities.ContainsKey(provider.Id))
|
||||
{
|
||||
_providerAbilities[provider.Id] = newAbility;
|
||||
}
|
||||
else
|
||||
{
|
||||
_providerAbilities.Add(provider.Id, newAbility);
|
||||
}
|
||||
_providerAbilities[provider.Id] = newAbility;
|
||||
}
|
||||
|
||||
public virtual async Task UpsertOrganizationAbilityAsync(Organization organization)
|
||||
@@ -65,32 +58,19 @@ public class InMemoryApplicationCacheService : IApplicationCacheService
|
||||
await InitOrganizationAbilitiesAsync();
|
||||
var newAbility = new OrganizationAbility(organization);
|
||||
|
||||
if (_orgAbilities.ContainsKey(organization.Id))
|
||||
{
|
||||
_orgAbilities[organization.Id] = newAbility;
|
||||
}
|
||||
else
|
||||
{
|
||||
_orgAbilities.Add(organization.Id, newAbility);
|
||||
}
|
||||
_orgAbilities[organization.Id] = newAbility;
|
||||
}
|
||||
|
||||
public virtual Task DeleteOrganizationAbilityAsync(Guid organizationId)
|
||||
{
|
||||
if (_orgAbilities != null && _orgAbilities.ContainsKey(organizationId))
|
||||
{
|
||||
_orgAbilities.Remove(organizationId);
|
||||
}
|
||||
_orgAbilities?.Remove(organizationId);
|
||||
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public virtual Task DeleteProviderAbilityAsync(Guid providerId)
|
||||
{
|
||||
if (_providerAbilities != null && _providerAbilities.ContainsKey(providerId))
|
||||
{
|
||||
_providerAbilities.Remove(providerId);
|
||||
}
|
||||
_providerAbilities?.Remove(providerId);
|
||||
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
@@ -182,9 +182,8 @@ public class LicensingService : ILicensingService
|
||||
|
||||
// Only check once per day
|
||||
var now = DateTime.UtcNow;
|
||||
if (_userCheckCache.ContainsKey(user.Id))
|
||||
if (_userCheckCache.TryGetValue(user.Id, out var lastCheck))
|
||||
{
|
||||
var lastCheck = _userCheckCache[user.Id];
|
||||
if (lastCheck < now && now - lastCheck < TimeSpan.FromDays(1))
|
||||
{
|
||||
return user.Premium;
|
||||
|
||||
@@ -72,8 +72,8 @@ public class SendGridMailDeliveryService : IMailDeliveryService, IDisposable
|
||||
msg.SetOpenTracking(false);
|
||||
|
||||
if (message.MetaData != null &&
|
||||
message.MetaData.ContainsKey("SendGridBypassListManagement") &&
|
||||
Convert.ToBoolean(message.MetaData["SendGridBypassListManagement"]))
|
||||
message.MetaData.TryGetValue("SendGridBypassListManagement", out var sendGridBypassListManagement) &&
|
||||
Convert.ToBoolean(sendGridBypassListManagement))
|
||||
{
|
||||
msg.SetBypassListManagement(true);
|
||||
}
|
||||
|
||||
@@ -357,12 +357,12 @@ public class UserService : UserManager<User>, IUserService, IDisposable
|
||||
public async Task SendTwoFactorEmailAsync(User user, bool authentication = true)
|
||||
{
|
||||
var provider = user.GetTwoFactorProvider(TwoFactorProviderType.Email);
|
||||
if (provider == null || provider.MetaData == null || !provider.MetaData.ContainsKey("Email"))
|
||||
if (provider == null || provider.MetaData == null || !provider.MetaData.TryGetValue("Email", out var emailValue))
|
||||
{
|
||||
throw new ArgumentNullException("No email.");
|
||||
}
|
||||
|
||||
var email = ((string)provider.MetaData["Email"]).ToLowerInvariant();
|
||||
var email = ((string)emailValue).ToLowerInvariant();
|
||||
var token = await base.GenerateTwoFactorTokenAsync(user,
|
||||
CoreHelpers.CustomProviderName(TwoFactorProviderType.Email));
|
||||
|
||||
@@ -390,12 +390,12 @@ public class UserService : UserManager<User>, IUserService, IDisposable
|
||||
public async Task<bool> VerifyTwoFactorEmailAsync(User user, string token)
|
||||
{
|
||||
var provider = user.GetTwoFactorProvider(TwoFactorProviderType.Email);
|
||||
if (provider == null || provider.MetaData == null || !provider.MetaData.ContainsKey("Email"))
|
||||
if (provider == null || provider.MetaData == null || !provider.MetaData.TryGetValue("Email", out var emailValue))
|
||||
{
|
||||
throw new ArgumentNullException("No email.");
|
||||
}
|
||||
|
||||
var email = ((string)provider.MetaData["Email"]).ToLowerInvariant();
|
||||
var email = ((string)emailValue).ToLowerInvariant();
|
||||
return await base.VerifyTwoFactorTokenAsync(user,
|
||||
CoreHelpers.CustomProviderName(TwoFactorProviderType.Email), token);
|
||||
}
|
||||
@@ -453,12 +453,12 @@ public class UserService : UserManager<User>, IUserService, IDisposable
|
||||
var keyId = $"Key{id}";
|
||||
|
||||
var provider = user.GetTwoFactorProvider(TwoFactorProviderType.WebAuthn);
|
||||
if (!provider?.MetaData?.ContainsKey("pending") ?? true)
|
||||
if (provider?.MetaData is null || !provider.MetaData.TryGetValue("pending", out var pendingValue))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var options = CredentialCreateOptions.FromJson((string)provider.MetaData["pending"]);
|
||||
var options = CredentialCreateOptions.FromJson((string)pendingValue);
|
||||
|
||||
// Callback to ensure credential ID is unique. Always return true since we don't care if another
|
||||
// account uses the same 2FA key.
|
||||
@@ -1353,14 +1353,14 @@ public class UserService : UserManager<User>, IUserService, IDisposable
|
||||
public void SetTwoFactorProvider(User user, TwoFactorProviderType type, bool setEnabled = true)
|
||||
{
|
||||
var providers = user.GetTwoFactorProviders();
|
||||
if (!providers?.ContainsKey(type) ?? true)
|
||||
if (providers is null || !providers.TryGetValue(type, out var provider))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (setEnabled)
|
||||
{
|
||||
providers[type].Enabled = true;
|
||||
provider.Enabled = true;
|
||||
}
|
||||
user.SetTwoFactorProviders(providers);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user