1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-25 12:45:18 +01:00

Added templates and email side effect for claiming a domain.

This commit is contained in:
jrmccannon 2024-11-15 09:45:26 -06:00
parent 2f3da19b64
commit 17fc4c844c
No known key found for this signature in database
GPG Key ID: CF03F3DB01CE96A6
7 changed files with 47 additions and 3 deletions

View File

@ -22,6 +22,9 @@ public class VerifyOrganizationDomainCommand(
IPolicyService policyService,
IFeatureService featureService,
ICurrentContext currentContext,
IMailService mailService,
IOrganizationUserRepository organizationUserRepository,
IOrganizationRepository organizationRepository,
ILogger<VerifyOrganizationDomainCommand> logger)
: IVerifyOrganizationDomainCommand
{
@ -43,7 +46,7 @@ public class VerifyOrganizationDomainCommand(
return domainVerificationResult;
}
public async Task<OrganizationDomain> SystemVerifyOrganizationDomainAsync(OrganizationDomain organizationDomain)
public async Task<OrganizationDomain> SystemVerifyOrganizationDomainAsync(OrganizationDomain organizationDomain) // need request object to hold who is doing the action
{
var actingUser = new SystemUser(EventSystemUser.DomainVerification);
@ -102,7 +105,7 @@ public class VerifyOrganizationDomainCommand(
{
domain.SetVerifiedDate();
await EnableSingleOrganizationPolicyAsync(domain.OrganizationId, actingUser);
await DomainVerificationSideEffectsAsync(domain.OrganizationId, actingUser);
}
}
catch (Exception e)
@ -114,7 +117,7 @@ public class VerifyOrganizationDomainCommand(
return domain;
}
private async Task EnableSingleOrganizationPolicyAsync(Guid organizationId, IActingUser actingUser)
private async Task DomainVerificationSideEffectsAsync(Guid organizationId, IActingUser actingUser)
{
if (featureService.IsEnabled(FeatureFlagKeys.AccountDeprovisioning))
{
@ -124,4 +127,24 @@ public class VerifyOrganizationDomainCommand(
eventSystemUser: actingUser is SystemUser systemUser ? systemUser.SystemUserType : null);
}
}
private async Task EnableSingleOrganizationPolicyAsync(Guid organizationId, IActingUser actingUser) =>
await policyService.SaveAsync(
new Policy { OrganizationId = organizationId, Type = PolicyType.SingleOrg, Enabled = true },
savingUserId: actingUser is StandardUser standardUser ? standardUser.UserId : null,
eventSystemUser: actingUser is SystemUser systemUser ? systemUser.SystemUserType : null);
private async Task SendVerifiedDomainUserEmailAsync(Guid organizationId)
{
var orgUsers = await organizationUserRepository.GetManyByOrganizationWithClaimedDomainsAsync(organizationId);
var orgUserUsers = await organizationUserRepository.GetManyDetailsByOrganizationAsync(organizationId);
var userEmails = orgUsers.Where(x => x.Status != OrganizationUserStatusType.Revoked)
.Select(y => orgUserUsers.FirstOrDefault(x => x.Id == y.Id)?.Email)
.Where(x => x is not null);
var organization = await organizationRepository.GetByIdAsync(organizationId);
await Task.WhenAll(userEmails.Select(email => mailService.SendVerifiedDomainUserEmailAsync(email, organization)));
}
}

View File

@ -0,0 +1,6 @@
namespace Bit.Core.Models.Mail;
public class VerifiedDomainUserNotificationViewModel : BaseMailModel
{
public string OrganizationName { get; init; }
}

View File

@ -91,5 +91,6 @@ 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);
Task SendVerifiedDomainUserEmailAsync(string email, Organization organization);
}

View File

@ -460,6 +460,19 @@ public class HandlebarsMailService : IMailService
await _mailDeliveryService.SendEmailAsync(message);
}
public async Task SendVerifiedDomainUserEmailAsync(string email, Organization organization)
{
var message = CreateDefaultMessage($"Your Bitwarden account is claimed by {organization.DisplayName()}", email);
var model = new VerifiedDomainUserNotificationViewModel
{
OrganizationName = CoreHelpers.SanitizeForEmail(organization.DisplayName(), false)
};
await AddMessageContentAsync(message, "AdminConsole.VerifiedDomainUserNotification", model);
message.Category = "VerifiedDomainUserNotification";
await _mailDeliveryService.SendEmailAsync(message);
}
public async Task SendNewDeviceLoggedInEmail(string email, string deviceType, DateTime timestamp, string ip)
{
var message = CreateDefaultMessage($"New Device Logged In From {deviceType}", email);

View File

@ -302,5 +302,6 @@ public class NoopMailService : IMailService
return Task.FromResult(0);
}
public Task SendRequestSMAccessToAdminEmailAsync(IEnumerable<string> adminEmails, string organizationName, string userRequestingAccess, string emailContent) => throw new NotImplementedException();
public Task SendVerifiedDomainUserEmailAsync(string email, Organization organization) => Task.CompletedTask;
}