mirror of
https://github.com/bitwarden/server
synced 2026-01-01 16:13:33 +00:00
[PM-336] Nullable Platform & Unowned Services (#5646)
* Nullable Platform & Unowned Services * Fix build errors * Format
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
using System.Security.Claims;
|
||||
#nullable enable
|
||||
|
||||
using System.Security.Claims;
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Models.Business;
|
||||
@@ -12,14 +14,14 @@ public interface ILicensingService
|
||||
Task<bool> ValidateUserPremiumAsync(User user);
|
||||
bool VerifyLicense(ILicense license);
|
||||
byte[] SignLicense(ILicense license);
|
||||
Task<OrganizationLicense> ReadOrganizationLicenseAsync(Organization organization);
|
||||
Task<OrganizationLicense> ReadOrganizationLicenseAsync(Guid organizationId);
|
||||
ClaimsPrincipal GetClaimsPrincipalFromLicense(ILicense license);
|
||||
Task<OrganizationLicense?> ReadOrganizationLicenseAsync(Organization organization);
|
||||
Task<OrganizationLicense?> ReadOrganizationLicenseAsync(Guid organizationId);
|
||||
ClaimsPrincipal? GetClaimsPrincipalFromLicense(ILicense license);
|
||||
|
||||
Task<string> CreateOrganizationTokenAsync(
|
||||
Task<string?> CreateOrganizationTokenAsync(
|
||||
Organization organization,
|
||||
Guid installationId,
|
||||
SubscriptionInfo subscriptionInfo);
|
||||
|
||||
Task<string> CreateUserTokenAsync(User user, SubscriptionInfo subscriptionInfo);
|
||||
Task<string?> CreateUserTokenAsync(User user, SubscriptionInfo subscriptionInfo);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
#nullable enable
|
||||
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
using Bit.Core.AdminConsole.Entities.Provider;
|
||||
using Bit.Core.Auth.Entities;
|
||||
using Bit.Core.Billing.Enums;
|
||||
@@ -55,7 +57,7 @@ public interface IMailService
|
||||
bool mentionInvoices);
|
||||
Task SendPaymentFailedAsync(string email, decimal amount, bool mentionInvoices);
|
||||
Task SendAddedCreditAsync(string email, decimal amount);
|
||||
Task SendLicenseExpiredAsync(IEnumerable<string> emails, string organizationName = null);
|
||||
Task SendLicenseExpiredAsync(IEnumerable<string> emails, string? organizationName = null);
|
||||
Task SendNewDeviceLoggedInEmail(string email, string deviceType, DateTime timestamp, string ip);
|
||||
Task SendRecoverTwoFactorEmail(string email, DateTime timestamp, string ip);
|
||||
Task SendOrganizationUserRemovedForPolicySingleOrgEmailAsync(string organizationName, string email);
|
||||
@@ -96,9 +98,11 @@ public interface IMailService
|
||||
Task SendInitiateDeletProviderEmailAsync(string email, Provider provider, string token);
|
||||
Task SendInitiateDeleteOrganzationEmailAsync(string email, Organization organization, string token);
|
||||
Task SendRequestSMAccessToAdminEmailAsync(IEnumerable<string> adminEmails, string organizationName, string userRequestingAccess, string emailContent);
|
||||
#nullable disable
|
||||
Task SendFamiliesForEnterpriseRemoveSponsorshipsEmailAsync(string email, string offerAcceptanceDate, string organizationId,
|
||||
string organizationName);
|
||||
#nullable enable
|
||||
Task SendClaimedDomainUserEmailAsync(ClaimedUserDomainClaimedEmails emailList);
|
||||
Task SendDeviceApprovalRequestedNotificationEmailAsync(IEnumerable<string> adminEmails, Guid organizationId, string email, string userName);
|
||||
Task SendDeviceApprovalRequestedNotificationEmailAsync(IEnumerable<string> adminEmails, Guid organizationId, string email, string? userName);
|
||||
Task SendBulkSecurityTaskNotificationsAsync(Organization org, IEnumerable<UserSecurityTasksCount> securityTaskNotifications, IEnumerable<string> adminOwnerEmails);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using Amazon;
|
||||
#nullable enable
|
||||
|
||||
using Amazon;
|
||||
using Amazon.SimpleEmail;
|
||||
using Amazon.SimpleEmail.Model;
|
||||
using Bit.Core.Models.Mail;
|
||||
@@ -17,7 +19,7 @@ public class AmazonSesMailDeliveryService : IMailDeliveryService, IDisposable
|
||||
private readonly IAmazonSimpleEmailService _client;
|
||||
private readonly string _source;
|
||||
private readonly string _senderTag;
|
||||
private readonly string _configSetName;
|
||||
private readonly string? _configSetName;
|
||||
|
||||
public AmazonSesMailDeliveryService(
|
||||
GlobalSettings globalSettings,
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using System.Text;
|
||||
#nullable enable
|
||||
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using Azure.Storage.Queues;
|
||||
using Bit.Core.Utilities;
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using Bit.Core.Context;
|
||||
#nullable enable
|
||||
|
||||
using Bit.Core.Context;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Exceptions;
|
||||
using Bit.Core.Models.Data;
|
||||
@@ -34,8 +36,8 @@ public class CollectionService : ICollectionService
|
||||
_currentContext = currentContext;
|
||||
}
|
||||
|
||||
public async Task SaveAsync(Collection collection, IEnumerable<CollectionAccessSelection> groups = null,
|
||||
IEnumerable<CollectionAccessSelection> users = null)
|
||||
public async Task SaveAsync(Collection collection, IEnumerable<CollectionAccessSelection>? groups = null,
|
||||
IEnumerable<CollectionAccessSelection>? users = null)
|
||||
{
|
||||
var org = await _organizationRepository.GetByIdAsync(collection.OrganizationId);
|
||||
if (org == null)
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
using System.Net;
|
||||
#nullable enable
|
||||
|
||||
using System.Diagnostics;
|
||||
using System.Net;
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
@@ -213,6 +216,7 @@ public class HandlebarsMailService : IMailService
|
||||
|
||||
public async Task SendOrganizationAutoscaledEmailAsync(Organization organization, int initialSeatCount, IEnumerable<string> ownerEmails)
|
||||
{
|
||||
Debug.Assert(organization.Seats.HasValue, "Organization is expected to have a non-null value for seats at the time of sending this email");
|
||||
var message = CreateDefaultMessage($"{organization.DisplayName()} Seat Count Has Increased", ownerEmails);
|
||||
var model = new OrganizationSeatsAutoscaledViewModel
|
||||
{
|
||||
@@ -286,7 +290,7 @@ public class HandlebarsMailService : IMailService
|
||||
|
||||
var messageModels = orgInvitesInfo.OrgUserTokenPairs.Select(orgUserTokenPair =>
|
||||
{
|
||||
|
||||
Debug.Assert(orgUserTokenPair.OrgUser.Email is not null);
|
||||
var orgUserInviteViewModel = OrganizationUserInvitedViewModel.CreateFromInviteInfo(
|
||||
orgInvitesInfo, orgUserTokenPair.OrgUser, orgUserTokenPair.Token, _globalSettings);
|
||||
return CreateMessage(orgUserTokenPair.OrgUser.Email, orgUserInviteViewModel);
|
||||
@@ -358,7 +362,7 @@ public class HandlebarsMailService : IMailService
|
||||
WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash,
|
||||
SiteName = _globalSettings.SiteName,
|
||||
ProviderId = provider.Id,
|
||||
ProviderName = CoreHelpers.SanitizeForEmail(provider.DisplayName(), false),
|
||||
ProviderName = CoreHelpers.SanitizeForEmail(provider.DisplayName()!, false),
|
||||
ProviderNameUrlEncoded = WebUtility.UrlEncode(provider.Name),
|
||||
ProviderBillingEmail = provider.BillingEmail,
|
||||
ProviderCreationDate = provider.CreationDate.ToLongDateString(),
|
||||
@@ -448,7 +452,7 @@ public class HandlebarsMailService : IMailService
|
||||
await _mailDeliveryService.SendEmailAsync(message);
|
||||
}
|
||||
|
||||
public async Task SendLicenseExpiredAsync(IEnumerable<string> emails, string organizationName = null)
|
||||
public async Task SendLicenseExpiredAsync(IEnumerable<string> emails, string? organizationName = null)
|
||||
{
|
||||
var message = CreateDefaultMessage("License Expired", emails);
|
||||
var model = new LicenseExpiredViewModel();
|
||||
@@ -598,12 +602,14 @@ public class HandlebarsMailService : IMailService
|
||||
}
|
||||
|
||||
private async Task AddMessageContentAsync<T>(MailMessage message, string templateName, T model)
|
||||
where T : notnull
|
||||
{
|
||||
message.HtmlContent = await RenderAsync($"{templateName}.html", model);
|
||||
message.TextContent = await RenderAsync($"{templateName}.text", model);
|
||||
}
|
||||
|
||||
private async Task<string> RenderAsync<T>(string templateName, T model)
|
||||
private async Task<string?> RenderAsync<T>(string templateName, T model)
|
||||
where T : notnull
|
||||
{
|
||||
await RegisterHelpersAndPartialsAsync();
|
||||
if (!_templateCache.TryGetValue(templateName, out var template))
|
||||
@@ -618,7 +624,7 @@ public class HandlebarsMailService : IMailService
|
||||
return template != null ? template(model) : null;
|
||||
}
|
||||
|
||||
private async Task<string> ReadSourceAsync(string templateName)
|
||||
private async Task<string?> ReadSourceAsync(string templateName)
|
||||
{
|
||||
var assembly = typeof(HandlebarsMailService).GetTypeInfo().Assembly;
|
||||
var fullTemplateName = $"{Namespace}.{templateName}.hbs";
|
||||
@@ -626,7 +632,7 @@ public class HandlebarsMailService : IMailService
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using (var s = assembly.GetManifestResourceStream(fullTemplateName))
|
||||
using (var s = assembly.GetManifestResourceStream(fullTemplateName)!)
|
||||
using (var sr = new StreamReader(s))
|
||||
{
|
||||
return await sr.ReadToEndAsync();
|
||||
@@ -757,7 +763,7 @@ public class HandlebarsMailService : IMailService
|
||||
var emailList = new List<string>();
|
||||
if (parameters[0] is JsonElement jsonElement && jsonElement.ValueKind == JsonValueKind.Array)
|
||||
{
|
||||
emailList = jsonElement.EnumerateArray().Select(e => e.GetString()).ToList();
|
||||
emailList = jsonElement.EnumerateArray().Select(e => e.GetString()!).ToList();
|
||||
}
|
||||
else if (parameters[0] is IEnumerable<string> emails)
|
||||
{
|
||||
@@ -1276,7 +1282,7 @@ public class HandlebarsMailService : IMailService
|
||||
await _mailDeliveryService.SendEmailAsync(message);
|
||||
}
|
||||
|
||||
public async Task SendDeviceApprovalRequestedNotificationEmailAsync(IEnumerable<string> adminEmails, Guid organizationId, string email, string userName)
|
||||
public async Task SendDeviceApprovalRequestedNotificationEmailAsync(IEnumerable<string> adminEmails, Guid organizationId, string email, string? userName)
|
||||
{
|
||||
var templateName = _globalSettings.SelfHosted ?
|
||||
"AdminConsole.SelfHostNotifyAdminDeviceApprovalRequested" :
|
||||
@@ -1313,7 +1319,7 @@ public class HandlebarsMailService : IMailService
|
||||
await EnqueueMailAsync(messageModels.ToList());
|
||||
}
|
||||
|
||||
private static string GetUserIdentifier(string email, string userName)
|
||||
private static string GetUserIdentifier(string email, string? userName)
|
||||
{
|
||||
return string.IsNullOrEmpty(userName) ? email : CoreHelpers.SanitizeForEmail(userName, false);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Reflection;
|
||||
#nullable enable
|
||||
|
||||
using Bit.Core.Resources;
|
||||
using Microsoft.Extensions.Localization;
|
||||
|
||||
@@ -10,8 +11,8 @@ public class I18nService : II18nService
|
||||
|
||||
public I18nService(IStringLocalizerFactory factory)
|
||||
{
|
||||
var assemblyName = new AssemblyName(typeof(SharedResources).GetTypeInfo().Assembly.FullName);
|
||||
_localizer = factory.Create("SharedResources", assemblyName.Name);
|
||||
var assemblyName = typeof(SharedResources).Assembly.GetName()!;
|
||||
_localizer = factory.Create("SharedResources", assemblyName.Name!);
|
||||
}
|
||||
|
||||
public LocalizedString GetLocalizedHtmlString(string key)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Reflection;
|
||||
#nullable enable
|
||||
|
||||
using Bit.Core.Resources;
|
||||
using Microsoft.AspNetCore.Mvc.Localization;
|
||||
using Microsoft.Extensions.Localization;
|
||||
@@ -13,9 +14,9 @@ public class I18nViewLocalizer : IViewLocalizer
|
||||
public I18nViewLocalizer(IStringLocalizerFactory stringFactory,
|
||||
IHtmlLocalizerFactory htmlFactory)
|
||||
{
|
||||
var assemblyName = new AssemblyName(typeof(SharedResources).GetTypeInfo().Assembly.FullName);
|
||||
_stringLocalizer = stringFactory.Create("SharedResources", assemblyName.Name);
|
||||
_htmlLocalizer = htmlFactory.Create("SharedResources", assemblyName.Name);
|
||||
var assemblyName = typeof(SharedResources).Assembly.GetName()!;
|
||||
_stringLocalizer = stringFactory.Create("SharedResources", assemblyName.Name!);
|
||||
_htmlLocalizer = htmlFactory.Create("SharedResources", assemblyName.Name!);
|
||||
}
|
||||
|
||||
public LocalizedHtmlString this[string name] => _htmlLocalizer[name];
|
||||
|
||||
@@ -22,12 +22,17 @@ public class MailKitSmtpMailDeliveryService : IMailDeliveryService
|
||||
ILogger<MailKitSmtpMailDeliveryService> logger,
|
||||
IOptions<X509ChainOptions> x509ChainOptions)
|
||||
{
|
||||
if (globalSettings.Mail?.Smtp?.Host == null)
|
||||
if (globalSettings.Mail.Smtp?.Host == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(globalSettings.Mail.Smtp.Host));
|
||||
}
|
||||
|
||||
_replyEmail = CoreHelpers.PunyEncode(globalSettings.Mail?.ReplyToEmail);
|
||||
if (globalSettings.Mail.ReplyToEmail == null)
|
||||
{
|
||||
throw new InvalidOperationException("A GlobalSettings.Mail.ReplyToEmail is required to be set up.");
|
||||
}
|
||||
|
||||
_replyEmail = CoreHelpers.PunyEncode(globalSettings.Mail.ReplyToEmail);
|
||||
|
||||
if (_replyEmail.Contains("@"))
|
||||
{
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using System.Security.Claims;
|
||||
#nullable enable
|
||||
|
||||
using System.Security.Claims;
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Models.Business;
|
||||
@@ -45,28 +47,28 @@ public class NoopLicensingService : ILicensingService
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
public Task<OrganizationLicense> ReadOrganizationLicenseAsync(Organization organization)
|
||||
public Task<OrganizationLicense?> ReadOrganizationLicenseAsync(Organization organization)
|
||||
{
|
||||
return Task.FromResult<OrganizationLicense>(null);
|
||||
return Task.FromResult<OrganizationLicense?>(null);
|
||||
}
|
||||
|
||||
public Task<OrganizationLicense> ReadOrganizationLicenseAsync(Guid organizationId)
|
||||
public Task<OrganizationLicense?> ReadOrganizationLicenseAsync(Guid organizationId)
|
||||
{
|
||||
return Task.FromResult<OrganizationLicense>(null);
|
||||
return Task.FromResult<OrganizationLicense?>(null);
|
||||
}
|
||||
|
||||
public ClaimsPrincipal GetClaimsPrincipalFromLicense(ILicense license)
|
||||
public ClaimsPrincipal? GetClaimsPrincipalFromLicense(ILicense license)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public Task<string> CreateOrganizationTokenAsync(Organization organization, Guid installationId, SubscriptionInfo subscriptionInfo)
|
||||
public Task<string?> CreateOrganizationTokenAsync(Organization organization, Guid installationId, SubscriptionInfo subscriptionInfo)
|
||||
{
|
||||
return Task.FromResult<string>(null);
|
||||
return Task.FromResult<string?>(null);
|
||||
}
|
||||
|
||||
public Task<string> CreateUserTokenAsync(User user, SubscriptionInfo subscriptionInfo)
|
||||
public Task<string?> CreateUserTokenAsync(User user, SubscriptionInfo subscriptionInfo)
|
||||
{
|
||||
return Task.FromResult<string>(null);
|
||||
return Task.FromResult<string?>(null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
#nullable enable
|
||||
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
using Bit.Core.AdminConsole.Entities.Provider;
|
||||
using Bit.Core.Auth.Entities;
|
||||
using Bit.Core.Billing.Enums;
|
||||
@@ -137,7 +139,7 @@ public class NoopMailService : IMailService
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public Task SendLicenseExpiredAsync(IEnumerable<string> emails, string organizationName = null)
|
||||
public Task SendLicenseExpiredAsync(IEnumerable<string> emails, string? organizationName = null)
|
||||
{
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
@@ -324,7 +326,7 @@ public class NoopMailService : IMailService
|
||||
}
|
||||
public Task SendClaimedDomainUserEmailAsync(ClaimedUserDomainClaimedEmails emailList) => Task.CompletedTask;
|
||||
|
||||
public Task SendDeviceApprovalRequestedNotificationEmailAsync(IEnumerable<string> adminEmails, Guid organizationId, string email, string userName)
|
||||
public Task SendDeviceApprovalRequestedNotificationEmailAsync(IEnumerable<string> adminEmails, Guid organizationId, string email, string? userName)
|
||||
{
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user