mirror of
https://github.com/bitwarden/server
synced 2026-01-06 02:23:51 +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:
@@ -257,12 +257,12 @@ public class Organization : ITableObject<Guid>, IStorableSubscriber, IRevisable
|
||||
public bool TwoFactorProviderIsEnabled(TwoFactorProviderType provider)
|
||||
{
|
||||
var providers = GetTwoFactorProviders();
|
||||
if (providers == null || !providers.ContainsKey(provider))
|
||||
if (providers == null || !providers.TryGetValue(provider, out var twoFactorProvider))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return providers[provider].Enabled && Use2fa;
|
||||
return twoFactorProvider.Enabled && Use2fa;
|
||||
}
|
||||
|
||||
public bool TwoFactorIsEnabled()
|
||||
@@ -279,12 +279,7 @@ public class Organization : ITableObject<Guid>, IStorableSubscriber, IRevisable
|
||||
public TwoFactorProvider? GetTwoFactorProvider(TwoFactorProviderType provider)
|
||||
{
|
||||
var providers = GetTwoFactorProviders();
|
||||
if (providers == null || !providers.ContainsKey(provider))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return providers[provider];
|
||||
return providers?.GetValueOrDefault(provider);
|
||||
}
|
||||
|
||||
public void UpdateFromLicense(OrganizationLicense license, IFeatureService featureService)
|
||||
|
||||
@@ -104,8 +104,8 @@ public class SavePolicyCommand : ISavePolicyCommand
|
||||
var dependentPolicyTypes = _policyValidators.Values
|
||||
.Where(otherValidator => otherValidator.RequiredPolicies.Contains(policyUpdate.Type))
|
||||
.Select(otherValidator => otherValidator.Type)
|
||||
.Where(otherPolicyType => savedPoliciesDict.ContainsKey(otherPolicyType) &&
|
||||
savedPoliciesDict[otherPolicyType].Enabled)
|
||||
.Where(otherPolicyType => savedPoliciesDict.TryGetValue(otherPolicyType, out var savedPolicy) &&
|
||||
savedPolicy.Enabled)
|
||||
.ToList();
|
||||
|
||||
switch (dependentPolicyTypes)
|
||||
|
||||
@@ -462,13 +462,13 @@ public class EventService : IEventService
|
||||
|
||||
private bool CanUseEvents(IDictionary<Guid, OrganizationAbility> orgAbilities, Guid orgId)
|
||||
{
|
||||
return orgAbilities != null && orgAbilities.ContainsKey(orgId) &&
|
||||
orgAbilities[orgId].Enabled && orgAbilities[orgId].UseEvents;
|
||||
return orgAbilities != null && orgAbilities.TryGetValue(orgId, out var orgAbility) &&
|
||||
orgAbility.Enabled && orgAbility.UseEvents;
|
||||
}
|
||||
|
||||
private bool CanUseProviderEvents(IDictionary<Guid, ProviderAbility> providerAbilities, Guid providerId)
|
||||
{
|
||||
return providerAbilities != null && providerAbilities.ContainsKey(providerId) &&
|
||||
providerAbilities[providerId].Enabled && providerAbilities[providerId].UseEvents;
|
||||
return providerAbilities != null && providerAbilities.TryGetValue(providerId, out var providerAbility) &&
|
||||
providerAbility.Enabled && providerAbility.UseEvents;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -612,12 +612,12 @@ public class OrganizationService : IOrganizationService
|
||||
}
|
||||
|
||||
var providers = organization.GetTwoFactorProviders();
|
||||
if (!providers?.ContainsKey(type) ?? true)
|
||||
if (providers is null || !providers.TryGetValue(type, out var provider))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
providers[type].Enabled = true;
|
||||
provider.Enabled = true;
|
||||
organization.SetTwoFactorProviders(providers);
|
||||
await UpdateAsync(organization);
|
||||
}
|
||||
@@ -1115,7 +1115,7 @@ public class OrganizationService : IOrganizationService
|
||||
var existingUsersDict = existingExternalUsers.ToDictionary(u => u.ExternalId);
|
||||
var removeUsersSet = new HashSet<string>(removeUserExternalIds)
|
||||
.Except(newUsersSet)
|
||||
.Where(u => existingUsersDict.ContainsKey(u) && existingUsersDict[u].Type != OrganizationUserType.Owner)
|
||||
.Where(u => existingUsersDict.TryGetValue(u, out var existingUser) && existingUser.Type != OrganizationUserType.Owner)
|
||||
.Select(u => existingUsersDict[u]);
|
||||
|
||||
await _organizationUserRepository.DeleteManyAsync(removeUsersSet.Select(u => u.Id));
|
||||
|
||||
@@ -68,7 +68,7 @@ public class PolicyService : IPolicyService
|
||||
var excludedUserTypes = GetUserTypesExcludedFromPolicy(policyType);
|
||||
var orgAbilities = await _applicationCacheService.GetOrganizationAbilitiesAsync();
|
||||
return organizationUserPolicyDetails.Where(o =>
|
||||
(!orgAbilities.ContainsKey(o.OrganizationId) || orgAbilities[o.OrganizationId].UsePolicies) &&
|
||||
(!orgAbilities.TryGetValue(o.OrganizationId, out var orgAbility) || orgAbility.UsePolicies) &&
|
||||
o.PolicyEnabled &&
|
||||
!excludedUserTypes.Contains(o.OrganizationUserType) &&
|
||||
o.OrganizationUserStatus >= minStatus &&
|
||||
|
||||
@@ -43,7 +43,7 @@ public class EmailTwoFactorTokenProvider : EmailTokenProvider
|
||||
|
||||
private static bool HasProperMetaData(TwoFactorProvider provider)
|
||||
{
|
||||
return provider?.MetaData != null && provider.MetaData.ContainsKey("Email") &&
|
||||
!string.IsNullOrWhiteSpace((string)provider.MetaData["Email"]);
|
||||
return provider?.MetaData != null && provider.MetaData.TryGetValue("Email", out var emailValue) &&
|
||||
!string.IsNullOrWhiteSpace((string)emailValue);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ public class WebAuthnTokenProvider : IUserTwoFactorTokenProvider<User>
|
||||
var provider = user.GetTwoFactorProvider(TwoFactorProviderType.WebAuthn);
|
||||
var keys = LoadKeys(provider);
|
||||
|
||||
if (!provider.MetaData.TryGetValue("login", out var value))
|
||||
if (!provider.MetaData.TryGetValue("login", out var login))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -88,7 +88,7 @@ public class WebAuthnTokenProvider : IUserTwoFactorTokenProvider<User>
|
||||
var clientResponse = JsonSerializer.Deserialize<AuthenticatorAssertionRawResponse>(token,
|
||||
new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||||
|
||||
var jsonOptions = value.ToString();
|
||||
var jsonOptions = login.ToString();
|
||||
var options = AssertionOptions.FromJson(jsonOptions);
|
||||
|
||||
var webAuthCred = keys.Find(k => k.Item2.Descriptor.Id.SequenceEqual(clientResponse.Id));
|
||||
@@ -148,9 +148,9 @@ public class WebAuthnTokenProvider : IUserTwoFactorTokenProvider<User>
|
||||
for (var i = 1; i <= 5; i++)
|
||||
{
|
||||
var keyName = $"Key{i}";
|
||||
if (provider.MetaData.ContainsKey(keyName))
|
||||
if (provider.MetaData.TryGetValue(keyName, out var value))
|
||||
{
|
||||
var key = new TwoFactorProvider.WebAuthnData((dynamic)provider.MetaData[keyName]);
|
||||
var key = new TwoFactorProvider.WebAuthnData((dynamic)value);
|
||||
|
||||
keys.Add(new Tuple<string, TwoFactorProvider.WebAuthnData>(keyName, key));
|
||||
}
|
||||
|
||||
@@ -64,39 +64,39 @@ public class CurrentContext : ICurrentContext
|
||||
HttpContext = httpContext;
|
||||
await BuildAsync(httpContext.User, globalSettings);
|
||||
|
||||
if (DeviceIdentifier == null && httpContext.Request.Headers.ContainsKey("Device-Identifier"))
|
||||
if (DeviceIdentifier == null && httpContext.Request.Headers.TryGetValue("Device-Identifier", out var deviceIdentifier))
|
||||
{
|
||||
DeviceIdentifier = httpContext.Request.Headers["Device-Identifier"];
|
||||
DeviceIdentifier = deviceIdentifier;
|
||||
}
|
||||
|
||||
if (httpContext.Request.Headers.ContainsKey("Device-Type") &&
|
||||
Enum.TryParse(httpContext.Request.Headers["Device-Type"].ToString(), out DeviceType dType))
|
||||
if (httpContext.Request.Headers.TryGetValue("Device-Type", out var deviceType) &&
|
||||
Enum.TryParse(deviceType.ToString(), out DeviceType dType))
|
||||
{
|
||||
DeviceType = dType;
|
||||
}
|
||||
|
||||
if (!BotScore.HasValue && httpContext.Request.Headers.ContainsKey("X-Cf-Bot-Score") &&
|
||||
int.TryParse(httpContext.Request.Headers["X-Cf-Bot-Score"], out var parsedBotScore))
|
||||
if (!BotScore.HasValue && httpContext.Request.Headers.TryGetValue("X-Cf-Bot-Score", out var cfBotScore) &&
|
||||
int.TryParse(cfBotScore, out var parsedBotScore))
|
||||
{
|
||||
BotScore = parsedBotScore;
|
||||
}
|
||||
|
||||
if (httpContext.Request.Headers.ContainsKey("X-Cf-Worked-Proxied"))
|
||||
if (httpContext.Request.Headers.TryGetValue("X-Cf-Worked-Proxied", out var cfWorkedProxied))
|
||||
{
|
||||
CloudflareWorkerProxied = httpContext.Request.Headers["X-Cf-Worked-Proxied"] == "1";
|
||||
CloudflareWorkerProxied = cfWorkedProxied == "1";
|
||||
}
|
||||
|
||||
if (httpContext.Request.Headers.ContainsKey("X-Cf-Is-Bot"))
|
||||
if (httpContext.Request.Headers.TryGetValue("X-Cf-Is-Bot", out var cfIsBot))
|
||||
{
|
||||
IsBot = httpContext.Request.Headers["X-Cf-Is-Bot"] == "1";
|
||||
IsBot = cfIsBot == "1";
|
||||
}
|
||||
|
||||
if (httpContext.Request.Headers.ContainsKey("X-Cf-Maybe-Bot"))
|
||||
if (httpContext.Request.Headers.TryGetValue("X-Cf-Maybe-Bot", out var cfMaybeBot))
|
||||
{
|
||||
MaybeBot = httpContext.Request.Headers["X-Cf-Maybe-Bot"] == "1";
|
||||
MaybeBot = cfMaybeBot == "1";
|
||||
}
|
||||
|
||||
if (httpContext.Request.Headers.ContainsKey("Bitwarden-Client-Version") && Version.TryParse(httpContext.Request.Headers["Bitwarden-Client-Version"], out var cVersion))
|
||||
if (httpContext.Request.Headers.TryGetValue("Bitwarden-Client-Version", out var bitWardenClientVersion) && Version.TryParse(bitWardenClientVersion, out var cVersion))
|
||||
{
|
||||
ClientVersion = cVersion;
|
||||
}
|
||||
@@ -190,14 +190,14 @@ public class CurrentContext : ICurrentContext
|
||||
|
||||
private List<CurrentContextOrganization> GetOrganizations(Dictionary<string, IEnumerable<Claim>> claimsDict, bool orgApi)
|
||||
{
|
||||
var accessSecretsManager = claimsDict.ContainsKey(Claims.SecretsManagerAccess)
|
||||
? claimsDict[Claims.SecretsManagerAccess].ToDictionary(s => s.Value, _ => true)
|
||||
var accessSecretsManager = claimsDict.TryGetValue(Claims.SecretsManagerAccess, out var secretsManagerAccessClaim)
|
||||
? secretsManagerAccessClaim.ToDictionary(s => s.Value, _ => true)
|
||||
: new Dictionary<string, bool>();
|
||||
|
||||
var organizations = new List<CurrentContextOrganization>();
|
||||
if (claimsDict.ContainsKey(Claims.OrganizationOwner))
|
||||
if (claimsDict.TryGetValue(Claims.OrganizationOwner, out var organizationOwnerClaim))
|
||||
{
|
||||
organizations.AddRange(claimsDict[Claims.OrganizationOwner].Select(c =>
|
||||
organizations.AddRange(organizationOwnerClaim.Select(c =>
|
||||
new CurrentContextOrganization
|
||||
{
|
||||
Id = new Guid(c.Value),
|
||||
@@ -214,9 +214,9 @@ public class CurrentContext : ICurrentContext
|
||||
});
|
||||
}
|
||||
|
||||
if (claimsDict.ContainsKey(Claims.OrganizationAdmin))
|
||||
if (claimsDict.TryGetValue(Claims.OrganizationAdmin, out var organizationAdminClaim))
|
||||
{
|
||||
organizations.AddRange(claimsDict[Claims.OrganizationAdmin].Select(c =>
|
||||
organizations.AddRange(organizationAdminClaim.Select(c =>
|
||||
new CurrentContextOrganization
|
||||
{
|
||||
Id = new Guid(c.Value),
|
||||
@@ -225,9 +225,9 @@ public class CurrentContext : ICurrentContext
|
||||
}));
|
||||
}
|
||||
|
||||
if (claimsDict.ContainsKey(Claims.OrganizationUser))
|
||||
if (claimsDict.TryGetValue(Claims.OrganizationUser, out var organizationUserClaim))
|
||||
{
|
||||
organizations.AddRange(claimsDict[Claims.OrganizationUser].Select(c =>
|
||||
organizations.AddRange(organizationUserClaim.Select(c =>
|
||||
new CurrentContextOrganization
|
||||
{
|
||||
Id = new Guid(c.Value),
|
||||
@@ -236,9 +236,9 @@ public class CurrentContext : ICurrentContext
|
||||
}));
|
||||
}
|
||||
|
||||
if (claimsDict.ContainsKey(Claims.OrganizationCustom))
|
||||
if (claimsDict.TryGetValue(Claims.OrganizationCustom, out var organizationCustomClaim))
|
||||
{
|
||||
organizations.AddRange(claimsDict[Claims.OrganizationCustom].Select(c =>
|
||||
organizations.AddRange(organizationCustomClaim.Select(c =>
|
||||
new CurrentContextOrganization
|
||||
{
|
||||
Id = new Guid(c.Value),
|
||||
@@ -254,9 +254,9 @@ public class CurrentContext : ICurrentContext
|
||||
private List<CurrentContextProvider> GetProviders(Dictionary<string, IEnumerable<Claim>> claimsDict)
|
||||
{
|
||||
var providers = new List<CurrentContextProvider>();
|
||||
if (claimsDict.ContainsKey(Claims.ProviderAdmin))
|
||||
if (claimsDict.TryGetValue(Claims.ProviderAdmin, out var providerAdminClaim))
|
||||
{
|
||||
providers.AddRange(claimsDict[Claims.ProviderAdmin].Select(c =>
|
||||
providers.AddRange(providerAdminClaim.Select(c =>
|
||||
new CurrentContextProvider
|
||||
{
|
||||
Id = new Guid(c.Value),
|
||||
@@ -264,9 +264,9 @@ public class CurrentContext : ICurrentContext
|
||||
}));
|
||||
}
|
||||
|
||||
if (claimsDict.ContainsKey(Claims.ProviderServiceUser))
|
||||
if (claimsDict.TryGetValue(Claims.ProviderServiceUser, out var providerServiceUserClaim))
|
||||
{
|
||||
providers.AddRange(claimsDict[Claims.ProviderServiceUser].Select(c =>
|
||||
providers.AddRange(providerServiceUserClaim.Select(c =>
|
||||
new CurrentContextProvider
|
||||
{
|
||||
Id = new Guid(c.Value),
|
||||
@@ -504,20 +504,20 @@ public class CurrentContext : ICurrentContext
|
||||
|
||||
private string GetClaimValue(Dictionary<string, IEnumerable<Claim>> claims, string type)
|
||||
{
|
||||
if (!claims.ContainsKey(type))
|
||||
if (!claims.TryGetValue(type, out var claim))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return claims[type].FirstOrDefault()?.Value;
|
||||
return claim.FirstOrDefault()?.Value;
|
||||
}
|
||||
|
||||
private Permissions SetOrganizationPermissionsFromClaims(string organizationId, Dictionary<string, IEnumerable<Claim>> claimsDict)
|
||||
{
|
||||
bool hasClaim(string claimKey)
|
||||
{
|
||||
return claimsDict.ContainsKey(claimKey) ?
|
||||
claimsDict[claimKey].Any(x => x.Value == organizationId) : false;
|
||||
return claimsDict.TryGetValue(claimKey, out var claim) ?
|
||||
claim.Any(x => x.Value == organizationId) : false;
|
||||
}
|
||||
|
||||
return new Permissions
|
||||
|
||||
@@ -195,12 +195,7 @@ public class User : ITableObject<Guid>, IStorableSubscriber, IRevisable, ITwoFac
|
||||
public TwoFactorProvider? GetTwoFactorProvider(TwoFactorProviderType provider)
|
||||
{
|
||||
var providers = GetTwoFactorProviders();
|
||||
if (providers == null || !providers.TryGetValue(provider, out var value))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return value;
|
||||
return providers?.GetValueOrDefault(provider);
|
||||
}
|
||||
|
||||
public long StorageBytesRemaining()
|
||||
|
||||
@@ -63,6 +63,6 @@ public class DistributedCacheCookieManager : ICookieManager
|
||||
private string GetKey(string key, string id) => $"{CacheKeyPrefix}-{key}-{id}";
|
||||
|
||||
private string GetId(HttpContext context, string key) =>
|
||||
context.Request.Cookies.ContainsKey(key) ?
|
||||
context.Request.Cookies[key] : null;
|
||||
context.Request.Cookies.TryGetValue(key, out var cookie) ?
|
||||
cookie : null;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -637,9 +637,9 @@ public static class CoreHelpers
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!globalSettings.SelfHosted && httpContext.Request.Headers.ContainsKey(RealConnectingIp))
|
||||
if (!globalSettings.SelfHosted && httpContext.Request.Headers.TryGetValue(RealConnectingIp, out var realConnectingIp))
|
||||
{
|
||||
return httpContext.Request.Headers[RealConnectingIp].ToString();
|
||||
return realConnectingIp.ToString();
|
||||
}
|
||||
|
||||
return httpContext.Connection?.RemoteIpAddress?.ToString();
|
||||
|
||||
@@ -46,7 +46,7 @@ public static class LoggerFactoryExtensions
|
||||
{
|
||||
return true;
|
||||
}
|
||||
var eventId = e.Properties.ContainsKey("EventId") ? e.Properties["EventId"].ToString() : null;
|
||||
var eventId = e.Properties.TryGetValue("EventId", out var eventIdValue) ? eventIdValue.ToString() : null;
|
||||
if (eventId?.Contains(Constants.BypassFiltersEventId.ToString()) ?? false)
|
||||
{
|
||||
return true;
|
||||
|
||||
@@ -251,16 +251,17 @@ public class AzureAttachmentStorageService : IAttachmentStorageService
|
||||
|
||||
private async Task InitAsync(string containerName)
|
||||
{
|
||||
if (!_attachmentContainers.ContainsKey(containerName) || _attachmentContainers[containerName] == null)
|
||||
if (!_attachmentContainers.TryGetValue(containerName, out var attachmentContainer) || attachmentContainer == null)
|
||||
{
|
||||
_attachmentContainers[containerName] = _blobServiceClient.GetBlobContainerClient(containerName);
|
||||
attachmentContainer = _blobServiceClient.GetBlobContainerClient(containerName);
|
||||
_attachmentContainers[containerName] = attachmentContainer;
|
||||
if (containerName == "attachments")
|
||||
{
|
||||
await _attachmentContainers[containerName].CreateIfNotExistsAsync(PublicAccessType.Blob, null, null);
|
||||
await attachmentContainer.CreateIfNotExistsAsync(PublicAccessType.Blob, null, null);
|
||||
}
|
||||
else
|
||||
{
|
||||
await _attachmentContainers[containerName].CreateIfNotExistsAsync(PublicAccessType.None, null, null);
|
||||
await attachmentContainer.CreateIfNotExistsAsync(PublicAccessType.None, null, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -312,13 +312,11 @@ public class CipherService : ICipherService
|
||||
}
|
||||
|
||||
var attachments = cipher.GetAttachments();
|
||||
if (!attachments.ContainsKey(attachmentId))
|
||||
if (!attachments.TryGetValue(attachmentId, out var originalAttachmentMetadata))
|
||||
{
|
||||
throw new BadRequestException($"Cipher does not own specified attachment");
|
||||
}
|
||||
|
||||
var originalAttachmentMetadata = attachments[attachmentId];
|
||||
|
||||
if (originalAttachmentMetadata.TempMetadata != null)
|
||||
{
|
||||
throw new BadRequestException("Another process is trying to migrate this attachment");
|
||||
@@ -395,12 +393,11 @@ public class CipherService : ICipherService
|
||||
{
|
||||
var attachments = cipher?.GetAttachments() ?? new Dictionary<string, CipherAttachment.MetaData>();
|
||||
|
||||
if (!attachments.ContainsKey(attachmentId))
|
||||
if (!attachments.TryGetValue(attachmentId, out var data))
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
var data = attachments[attachmentId];
|
||||
var response = new AttachmentResponseData
|
||||
{
|
||||
Cipher = cipher,
|
||||
|
||||
Reference in New Issue
Block a user