1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-26 12:55:17 +01:00

update to razorlight 2.0

This commit is contained in:
Kyle Spearrin 2017-10-06 12:00:41 -04:00
parent f2183246d2
commit 6c2ade2590
2 changed files with 68 additions and 42 deletions

View File

@ -65,7 +65,7 @@
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="2.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="2.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="Portable.BouncyCastle" Version="1.8.1.3" /> <PackageReference Include="Portable.BouncyCastle" Version="1.8.1.3" />
<PackageReference Include="RazorLight" Version="1.1.0" /> <PackageReference Include="RazorLight" Version="2.0.0-alpha3" />
<PackageReference Include="Sendgrid" Version="9.9.0" /> <PackageReference Include="Sendgrid" Version="9.9.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="2.0.2" /> <PackageReference Include="Serilog.Extensions.Logging" Version="2.0.2" />
<PackageReference Include="Serilog.Extensions.Logging.File" Version="1.1.0" /> <PackageReference Include="Serilog.Extensions.Logging.File" Version="1.1.0" />

View File

@ -4,10 +4,12 @@ using System.Threading.Tasks;
using Bit.Core.Models.Table; using Bit.Core.Models.Table;
using RazorLight; using RazorLight;
using Bit.Core.Models.Mail; using Bit.Core.Models.Mail;
using RazorLight.Templating;
using System.IO; using System.IO;
using System.Net; using System.Net;
using Bit.Core.Utilities; using Bit.Core.Utilities;
using RazorLight.Razor;
using System.Linq;
using System.Reflection;
namespace Bit.Core.Services namespace Bit.Core.Services
{ {
@ -24,11 +26,9 @@ namespace Bit.Core.Services
_globalSettings = globalSettings; _globalSettings = globalSettings;
_mailDeliveryService = mailDeliveryService; _mailDeliveryService = mailDeliveryService;
var manager = new CustomEmbeddedResourceTemplateManager("Bit.Core.MailTemplates.Razor");
var core = new EngineCore(manager, EngineConfiguration.Default); var factory = new EngineFactory();
var pageFactory = new DefaultPageFactory(core.KeyCompile); _engine = factory.Create(new CustomEmbeddedRazorProject());
var lookup = new DefaultPageLookup(pageFactory);
_engine = new RazorLightEngine(core, lookup);
} }
public async Task SendVerifyEmailEmailAsync(string email, Guid userId, string token) public async Task SendVerifyEmailEmailAsync(string email, Guid userId, string token)
@ -41,8 +41,8 @@ namespace Bit.Core.Services
WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash, WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash,
SiteName = _globalSettings.SiteName SiteName = _globalSettings.SiteName
}; };
message.HtmlContent = _engine.Parse("VerifyEmail", model); message.HtmlContent = await _engine.CompileRenderAsync("VerifyEmail", model);
message.TextContent = _engine.Parse("VerifyEmail.text", model); message.TextContent = await _engine.CompileRenderAsync("VerifyEmail.text", model);
message.MetaData.Add("SendGridBypassListManagement", true); message.MetaData.Add("SendGridBypassListManagement", true);
await _mailDeliveryService.SendEmailAsync(message); await _mailDeliveryService.SendEmailAsync(message);
@ -60,8 +60,8 @@ namespace Bit.Core.Services
Email = email, Email = email,
EmailEncoded = WebUtility.UrlEncode(email) EmailEncoded = WebUtility.UrlEncode(email)
}; };
message.HtmlContent = _engine.Parse("VerifyDelete", model); message.HtmlContent = await _engine.CompileRenderAsync("VerifyDelete", model);
message.TextContent = _engine.Parse("VerifyDelete.text", model); message.TextContent = await _engine.CompileRenderAsync("VerifyDelete.text", model);
message.MetaData.Add("SendGridBypassListManagement", true); message.MetaData.Add("SendGridBypassListManagement", true);
await _mailDeliveryService.SendEmailAsync(message); await _mailDeliveryService.SendEmailAsync(message);
@ -77,8 +77,8 @@ namespace Bit.Core.Services
WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash, WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash,
SiteName = _globalSettings.SiteName SiteName = _globalSettings.SiteName
}; };
message.HtmlContent = _engine.Parse("ChangeEmailAlreadyExists", model); message.HtmlContent = await _engine.CompileRenderAsync("ChangeEmailAlreadyExists", model);
message.TextContent = _engine.Parse("ChangeEmailAlreadyExists.text", model); message.TextContent = await _engine.CompileRenderAsync("ChangeEmailAlreadyExists.text", model);
await _mailDeliveryService.SendEmailAsync(message); await _mailDeliveryService.SendEmailAsync(message);
} }
@ -91,8 +91,8 @@ namespace Bit.Core.Services
WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash, WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash,
SiteName = _globalSettings.SiteName SiteName = _globalSettings.SiteName
}; };
message.HtmlContent = _engine.Parse("ChangeEmail", model); message.HtmlContent = await _engine.CompileRenderAsync("ChangeEmail", model);
message.TextContent = _engine.Parse("ChangeEmail.text", model); message.TextContent = await _engine.CompileRenderAsync("ChangeEmail.text", model);
message.MetaData.Add("SendGridBypassListManagement", true); message.MetaData.Add("SendGridBypassListManagement", true);
await _mailDeliveryService.SendEmailAsync(message); await _mailDeliveryService.SendEmailAsync(message);
@ -107,8 +107,8 @@ namespace Bit.Core.Services
WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash, WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash,
SiteName = _globalSettings.SiteName SiteName = _globalSettings.SiteName
}; };
message.HtmlContent = _engine.Parse("TwoFactorEmail", model); message.HtmlContent = await _engine.CompileRenderAsync("TwoFactorEmail", model);
message.TextContent = _engine.Parse("TwoFactorEmail.text", model); message.TextContent = await _engine.CompileRenderAsync("TwoFactorEmail.text", model);
message.MetaData.Add("SendGridBypassListManagement", true); message.MetaData.Add("SendGridBypassListManagement", true);
await _mailDeliveryService.SendEmailAsync(message); await _mailDeliveryService.SendEmailAsync(message);
@ -123,8 +123,8 @@ namespace Bit.Core.Services
WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash, WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash,
SiteName = _globalSettings.SiteName SiteName = _globalSettings.SiteName
}; };
message.HtmlContent = _engine.Parse("MasterPasswordHint", model); message.HtmlContent = await _engine.CompileRenderAsync("MasterPasswordHint", model);
message.TextContent = _engine.Parse("MasterPasswordHint.text", model); message.TextContent = await _engine.CompileRenderAsync("MasterPasswordHint.text", model);
await _mailDeliveryService.SendEmailAsync(message); await _mailDeliveryService.SendEmailAsync(message);
} }
@ -136,8 +136,8 @@ namespace Bit.Core.Services
WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash, WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash,
SiteName = _globalSettings.SiteName SiteName = _globalSettings.SiteName
}; };
message.HtmlContent = _engine.Parse("NoMasterPasswordHint", model); message.HtmlContent = await _engine.CompileRenderAsync("NoMasterPasswordHint", model);
message.TextContent = _engine.Parse("NoMasterPasswordHint.text", model); message.TextContent = await _engine.CompileRenderAsync("NoMasterPasswordHint.text", model);
await _mailDeliveryService.SendEmailAsync(message); await _mailDeliveryService.SendEmailAsync(message);
} }
@ -152,8 +152,8 @@ namespace Bit.Core.Services
WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash, WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash,
SiteName = _globalSettings.SiteName SiteName = _globalSettings.SiteName
}; };
message.HtmlContent = _engine.Parse("OrganizationUserAccepted", model); message.HtmlContent = await _engine.CompileRenderAsync("OrganizationUserAccepted", model);
message.TextContent = _engine.Parse("OrganizationUserAccepted.text", model); message.TextContent = await _engine.CompileRenderAsync("OrganizationUserAccepted.text", model);
await _mailDeliveryService.SendEmailAsync(message); await _mailDeliveryService.SendEmailAsync(message);
} }
@ -166,8 +166,8 @@ namespace Bit.Core.Services
WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash, WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash,
SiteName = _globalSettings.SiteName SiteName = _globalSettings.SiteName
}; };
message.HtmlContent = _engine.Parse("OrganizationUserConfirmed", model); message.HtmlContent = await _engine.CompileRenderAsync("OrganizationUserConfirmed", model);
message.TextContent = _engine.Parse("OrganizationUserConfirmed.text", model); message.TextContent = await _engine.CompileRenderAsync("OrganizationUserConfirmed.text", model);
await _mailDeliveryService.SendEmailAsync(message); await _mailDeliveryService.SendEmailAsync(message);
} }
@ -185,8 +185,8 @@ namespace Bit.Core.Services
WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash, WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash,
SiteName = _globalSettings.SiteName SiteName = _globalSettings.SiteName
}; };
message.HtmlContent = _engine.Parse("OrganizationUserInvited", model); message.HtmlContent = await _engine.CompileRenderAsync("OrganizationUserInvited", model);
message.TextContent = _engine.Parse("OrganizationUserInvited.text", model); message.TextContent = await _engine.CompileRenderAsync("OrganizationUserInvited.text", model);
await _mailDeliveryService.SendEmailAsync(message); await _mailDeliveryService.SendEmailAsync(message);
} }
@ -198,8 +198,8 @@ namespace Bit.Core.Services
WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash, WebVaultUrl = _globalSettings.BaseServiceUri.VaultWithHash,
SiteName = _globalSettings.SiteName SiteName = _globalSettings.SiteName
}; };
message.HtmlContent = _engine.Parse("Welcome", model); message.HtmlContent = await _engine.CompileRenderAsync("Welcome", model);
message.TextContent = _engine.Parse("Welcome.text", model); message.TextContent = await _engine.CompileRenderAsync("Welcome.text", model);
await _mailDeliveryService.SendEmailAsync(message); await _mailDeliveryService.SendEmailAsync(message);
} }
@ -218,29 +218,55 @@ namespace Bit.Core.Services
}; };
} }
public class CustomEmbeddedResourceTemplateManager : ITemplateManager public class CustomEmbeddedRazorProject : RazorLightProject
{ {
public CustomEmbeddedResourceTemplateManager(string rootNamespace) public override Task<RazorLightProjectItem> GetItemAsync(string templateKey)
{ {
Namespace = rootNamespace ?? throw new ArgumentNullException(nameof(rootNamespace)); if(string.IsNullOrEmpty(templateKey))
{
throw new ArgumentNullException(nameof(templateKey));
}
var item = new CustomEmbeddedRazorProjectItem(templateKey);
return Task.FromResult(item as RazorLightProjectItem);
} }
public string Namespace { get; } public override Task<IEnumerable<RazorLightProjectItem>> GetImportsAsync(string templateKey)
public ITemplateSource Resolve(string key)
{ {
var assembly = GetType().Assembly; return Task.FromResult(Enumerable.Empty<RazorLightProjectItem>());
using(var stream = assembly.GetManifestResourceStream(Namespace + "." + key + ".cshtml")) }
}
public class CustomEmbeddedRazorProjectItem : RazorLightProjectItem
{
private readonly string _fullTemplateKey;
private readonly Assembly _assembly;
public CustomEmbeddedRazorProjectItem(string key)
{
if(string.IsNullOrEmpty(key))
{
throw new ArgumentNullException(nameof(key));
}
Key = key;
_assembly = GetType().Assembly;
_fullTemplateKey = $"Bit.Core.MailTemplates.Razor.{key}.cshtml";
}
public override string Key { get; set; }
public override bool Exists => true;
public override Stream Read()
{
using(var stream = _assembly.GetManifestResourceStream(_fullTemplateKey))
{ {
if(stream == null) if(stream == null)
{ {
throw new RazorLightException(string.Format("Couldn't load resource '{0}.{1}.cshtml'.", Namespace, key)); throw new RazorLightException($"Couldn't load resource '{_fullTemplateKey}'.");
} }
using(var reader = new StreamReader(stream)) return stream;
{
return new LoadedTemplateSource(reader.ReadToEnd());
}
} }
} }
} }