mirror of
https://github.com/bitwarden/server
synced 2026-01-03 00:53:37 +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:
@@ -25,7 +25,7 @@ public class UpdateTwoFactorAuthenticatorRequestModel : SecretVerificationReques
|
||||
{
|
||||
providers = new Dictionary<TwoFactorProviderType, TwoFactorProvider>();
|
||||
}
|
||||
else if (providers.ContainsKey(TwoFactorProviderType.Authenticator))
|
||||
else
|
||||
{
|
||||
providers.Remove(TwoFactorProviderType.Authenticator);
|
||||
}
|
||||
@@ -62,7 +62,7 @@ public class UpdateTwoFactorDuoRequestModel : SecretVerificationRequestModel, IV
|
||||
{
|
||||
providers = [];
|
||||
}
|
||||
else if (providers.ContainsKey(TwoFactorProviderType.Duo))
|
||||
else
|
||||
{
|
||||
providers.Remove(TwoFactorProviderType.Duo);
|
||||
}
|
||||
@@ -88,7 +88,7 @@ public class UpdateTwoFactorDuoRequestModel : SecretVerificationRequestModel, IV
|
||||
{
|
||||
providers = [];
|
||||
}
|
||||
else if (providers.ContainsKey(TwoFactorProviderType.OrganizationDuo))
|
||||
else
|
||||
{
|
||||
providers.Remove(TwoFactorProviderType.OrganizationDuo);
|
||||
}
|
||||
@@ -145,7 +145,7 @@ public class UpdateTwoFactorYubicoOtpRequestModel : SecretVerificationRequestMod
|
||||
{
|
||||
providers = new Dictionary<TwoFactorProviderType, TwoFactorProvider>();
|
||||
}
|
||||
else if (providers.ContainsKey(TwoFactorProviderType.YubiKey))
|
||||
else
|
||||
{
|
||||
providers.Remove(TwoFactorProviderType.YubiKey);
|
||||
}
|
||||
@@ -228,7 +228,7 @@ public class TwoFactorEmailRequestModel : SecretVerificationRequestModel
|
||||
{
|
||||
providers = new Dictionary<TwoFactorProviderType, TwoFactorProvider>();
|
||||
}
|
||||
else if (providers.ContainsKey(TwoFactorProviderType.Email))
|
||||
else
|
||||
{
|
||||
providers.Remove(TwoFactorProviderType.Email);
|
||||
}
|
||||
|
||||
@@ -13,9 +13,9 @@ public class TwoFactorAuthenticatorResponseModel : ResponseModel
|
||||
ArgumentNullException.ThrowIfNull(user);
|
||||
|
||||
var provider = user.GetTwoFactorProvider(TwoFactorProviderType.Authenticator);
|
||||
if (provider?.MetaData?.ContainsKey("Key") ?? false)
|
||||
if (provider?.MetaData?.TryGetValue("Key", out var keyValue) ?? false)
|
||||
{
|
||||
Key = (string)provider.MetaData["Key"];
|
||||
Key = (string)keyValue;
|
||||
Enabled = provider.Enabled;
|
||||
}
|
||||
else
|
||||
|
||||
@@ -15,9 +15,9 @@ public class TwoFactorEmailResponseModel : ResponseModel
|
||||
}
|
||||
|
||||
var provider = user.GetTwoFactorProvider(TwoFactorProviderType.Email);
|
||||
if (provider?.MetaData?.ContainsKey("Email") ?? false)
|
||||
if (provider?.MetaData?.TryGetValue("Email", out var email) ?? false)
|
||||
{
|
||||
Email = (string)provider.MetaData["Email"];
|
||||
Email = (string)email;
|
||||
Enabled = provider.Enabled;
|
||||
}
|
||||
else
|
||||
|
||||
@@ -19,29 +19,29 @@ public class TwoFactorYubiKeyResponseModel : ResponseModel
|
||||
{
|
||||
Enabled = provider.Enabled;
|
||||
|
||||
if (provider.MetaData.ContainsKey("Key1"))
|
||||
if (provider.MetaData.TryGetValue("Key1", out var key1))
|
||||
{
|
||||
Key1 = (string)provider.MetaData["Key1"];
|
||||
Key1 = (string)key1;
|
||||
}
|
||||
if (provider.MetaData.ContainsKey("Key2"))
|
||||
if (provider.MetaData.TryGetValue("Key2", out var key2))
|
||||
{
|
||||
Key2 = (string)provider.MetaData["Key2"];
|
||||
Key2 = (string)key2;
|
||||
}
|
||||
if (provider.MetaData.ContainsKey("Key3"))
|
||||
if (provider.MetaData.TryGetValue("Key3", out var key3))
|
||||
{
|
||||
Key3 = (string)provider.MetaData["Key3"];
|
||||
Key3 = (string)key3;
|
||||
}
|
||||
if (provider.MetaData.ContainsKey("Key4"))
|
||||
if (provider.MetaData.TryGetValue("Key4", out var key4))
|
||||
{
|
||||
Key4 = (string)provider.MetaData["Key4"];
|
||||
Key4 = (string)key4;
|
||||
}
|
||||
if (provider.MetaData.ContainsKey("Key5"))
|
||||
if (provider.MetaData.TryGetValue("Key5", out var key5))
|
||||
{
|
||||
Key5 = (string)provider.MetaData["Key5"];
|
||||
Key5 = (string)key5;
|
||||
}
|
||||
if (provider.MetaData.ContainsKey("Nfc"))
|
||||
if (provider.MetaData.TryGetValue("Nfc", out var nfc))
|
||||
{
|
||||
Nfc = (bool)provider.MetaData["Nfc"];
|
||||
Nfc = (bool)nfc;
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
@@ -62,9 +62,9 @@ public static class ApiHelpers
|
||||
}
|
||||
}
|
||||
|
||||
if (eventTypeHandlers.ContainsKey(eventGridEvent.EventType))
|
||||
if (eventTypeHandlers.TryGetValue(eventGridEvent.EventType, out var eventTypeHandler))
|
||||
{
|
||||
await eventTypeHandlers[eventGridEvent.EventType](eventGridEvent);
|
||||
await eventTypeHandler(eventGridEvent);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1188,14 +1188,14 @@ public class CiphersController : Controller
|
||||
var cipher = await GetByIdAsync(id, userId);
|
||||
var attachments = cipher?.GetAttachments();
|
||||
|
||||
if (attachments == null || !attachments.ContainsKey(attachmentId) || attachments[attachmentId].Validated)
|
||||
if (attachments == null || !attachments.TryGetValue(attachmentId, out var attachment) || attachment.Validated)
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
return new AttachmentUploadDataResponseModel
|
||||
{
|
||||
Url = await _attachmentStorageService.GetAttachmentUploadUrlAsync(cipher, attachments[attachmentId]),
|
||||
Url = await _attachmentStorageService.GetAttachmentUploadUrlAsync(cipher, attachment),
|
||||
FileUploadType = _attachmentStorageService.FileUploadType,
|
||||
};
|
||||
}
|
||||
@@ -1214,11 +1214,10 @@ public class CiphersController : Controller
|
||||
var userId = _userService.GetProperUserId(User).Value;
|
||||
var cipher = await GetByIdAsync(id, userId);
|
||||
var attachments = cipher?.GetAttachments();
|
||||
if (attachments == null || !attachments.ContainsKey(attachmentId))
|
||||
if (attachments == null || !attachments.TryGetValue(attachmentId, out var attachmentData))
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
var attachmentData = attachments[attachmentId];
|
||||
|
||||
await Request.GetFileAsync(async (stream) =>
|
||||
{
|
||||
@@ -1368,7 +1367,7 @@ public class CiphersController : Controller
|
||||
var cipher = await _cipherRepository.GetByIdAsync(new Guid(cipherId));
|
||||
var attachments = cipher?.GetAttachments() ?? new Dictionary<string, CipherAttachment.MetaData>();
|
||||
|
||||
if (cipher == null || !attachments.ContainsKey(attachmentId) || attachments[attachmentId].Validated)
|
||||
if (cipher == null || !attachments.TryGetValue(attachmentId, out var attachment) || attachment.Validated)
|
||||
{
|
||||
if (_attachmentStorageService is AzureSendFileStorageService azureFileStorageService)
|
||||
{
|
||||
@@ -1378,7 +1377,7 @@ public class CiphersController : Controller
|
||||
return;
|
||||
}
|
||||
|
||||
await _cipherService.ValidateCipherAttachmentFile(cipher, attachments[attachmentId]);
|
||||
await _cipherService.ValidateCipherAttachmentFile(cipher, attachment);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
||||
@@ -113,18 +113,25 @@ public class CipherRequestModel
|
||||
|
||||
if (hasAttachments2)
|
||||
{
|
||||
foreach (var attachment in attachments.Where(a => Attachments2.ContainsKey(a.Key)))
|
||||
foreach (var attachment in attachments)
|
||||
{
|
||||
var attachment2 = Attachments2[attachment.Key];
|
||||
if (!Attachments2.TryGetValue(attachment.Key, out var attachment2))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
attachment.Value.FileName = attachment2.FileName;
|
||||
attachment.Value.Key = attachment2.Key;
|
||||
}
|
||||
}
|
||||
else if (hasAttachments)
|
||||
{
|
||||
foreach (var attachment in attachments.Where(a => Attachments.ContainsKey(a.Key)))
|
||||
foreach (var attachment in attachments)
|
||||
{
|
||||
attachment.Value.FileName = Attachments[attachment.Key];
|
||||
if (!Attachments.TryGetValue(attachment.Key, out var attachmentForKey))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
attachment.Value.FileName = attachmentForKey;
|
||||
attachment.Value.Key = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,13 +129,13 @@ public class CipherDetailsResponseModel : CipherResponseModel
|
||||
IDictionary<Guid, IGrouping<Guid, CollectionCipher>> collectionCiphers, string obj = "cipherDetails")
|
||||
: base(cipher, user, organizationAbilities, globalSettings, obj)
|
||||
{
|
||||
if (collectionCiphers?.ContainsKey(cipher.Id) ?? false)
|
||||
if (collectionCiphers?.TryGetValue(cipher.Id, out var collectionCipher) ?? false)
|
||||
{
|
||||
CollectionIds = collectionCiphers[cipher.Id].Select(c => c.CollectionId);
|
||||
CollectionIds = collectionCipher.Select(c => c.CollectionId);
|
||||
}
|
||||
else
|
||||
{
|
||||
CollectionIds = new Guid[] { };
|
||||
CollectionIds = [];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,7 +147,7 @@ public class CipherDetailsResponseModel : CipherResponseModel
|
||||
IEnumerable<CollectionCipher> collectionCiphers, string obj = "cipherDetails")
|
||||
: base(cipher, user, organizationAbilities, globalSettings, obj)
|
||||
{
|
||||
CollectionIds = collectionCiphers?.Select(c => c.CollectionId) ?? new List<Guid>();
|
||||
CollectionIds = collectionCiphers?.Select(c => c.CollectionId) ?? [];
|
||||
}
|
||||
|
||||
public CipherDetailsResponseModel(
|
||||
@@ -158,7 +158,7 @@ public class CipherDetailsResponseModel : CipherResponseModel
|
||||
string obj = "cipherDetails")
|
||||
: base(cipher, user, organizationAbilities, globalSettings, obj)
|
||||
{
|
||||
CollectionIds = cipher.CollectionIds ?? new List<Guid>();
|
||||
CollectionIds = cipher.CollectionIds ?? [];
|
||||
}
|
||||
|
||||
public IEnumerable<Guid> CollectionIds { get; set; }
|
||||
@@ -170,13 +170,13 @@ public class CipherMiniDetailsResponseModel : CipherMiniResponseModel
|
||||
IDictionary<Guid, IGrouping<Guid, CollectionCipher>> collectionCiphers, bool orgUseTotp, string obj = "cipherMiniDetails")
|
||||
: base(cipher, globalSettings, orgUseTotp, obj)
|
||||
{
|
||||
if (collectionCiphers?.ContainsKey(cipher.Id) ?? false)
|
||||
if (collectionCiphers?.TryGetValue(cipher.Id, out var collectionCipher) ?? false)
|
||||
{
|
||||
CollectionIds = collectionCiphers[cipher.Id].Select(c => c.CollectionId);
|
||||
CollectionIds = collectionCipher.Select(c => c.CollectionId);
|
||||
}
|
||||
else
|
||||
{
|
||||
CollectionIds = new Guid[] { };
|
||||
CollectionIds = [];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -184,7 +184,7 @@ public class CipherMiniDetailsResponseModel : CipherMiniResponseModel
|
||||
GlobalSettings globalSettings, bool orgUseTotp, string obj = "cipherMiniDetails")
|
||||
: base(cipher, globalSettings, orgUseTotp, obj)
|
||||
{
|
||||
CollectionIds = cipher.CollectionIds ?? new List<Guid>();
|
||||
CollectionIds = cipher.CollectionIds ?? [];
|
||||
}
|
||||
|
||||
public CipherMiniDetailsResponseModel(CipherOrganizationDetailsWithCollections cipher,
|
||||
|
||||
Reference in New Issue
Block a user