Skip to content

Commit 165505b

Browse files
authored
#8862: Preventing the SQL Server Service Broker feature from being enabled when not using SQL Server (Lombiq Technologies: ORCH-309) (#8875)
1 parent d9d0711 commit 165505b

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

src/Orchard.Web/Modules/Orchard.MessageBus/Orchard.MessageBus.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
<Compile Include="Services\IHostNameProvider.cs" />
131131
<Compile Include="Services\MessageBusNotificationProvider.cs" />
132132
<Compile Include="Handler\MessageBusHandler.cs" />
133+
<Compile Include="Services\SqlServerServiceBrokerFeatureGuard.cs" />
133134
<Compile Include="SqlServerBrokerMigrations.cs" />
134135
<Compile Include="Models\MessageRecord.cs" />
135136
<Compile Include="Services\DefaultMessageBus.cs" />
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using System;
2+
using System.Web.Mvc;
3+
using Orchard.Environment;
4+
using Orchard.Environment.Configuration;
5+
using Orchard.Environment.Extensions.Models;
6+
using Orchard.Environment.Features;
7+
using Orchard.Localization;
8+
using Orchard.Mvc;
9+
using Orchard.Mvc.Filters;
10+
using Orchard.UI.Notify;
11+
12+
namespace Orchard.MessageBus.Services
13+
{
14+
/// <summary>
15+
/// Prevents the SQL Server Service Broker feature from being enabled for tenants that are not using Microsoft SQL
16+
/// Server as their database provider.
17+
/// </summary>
18+
/// <remarks>
19+
/// The implementation is hackish but there seems to be no other way: if it would use <see cref="INotifier"/> it
20+
/// wouldn't work since <see cref="NotifyFilter"/> that writes out notifications to TempData runs before feature
21+
/// events are raised. Thus we have to manually add the messages to TempData, just as the filter would do.
22+
/// </remarks>
23+
public class SqlServerServiceBrokerFeatureGuard : FilterProvider, IFeatureEventHandler, IActionFilter
24+
{
25+
private const string FeatureId = "Orchard.MessageBus.SqlServerServiceBroker";
26+
private const string TempDataKey = FeatureId + ".TempData";
27+
28+
private readonly ShellSettings _shellSettings;
29+
private readonly IFeatureManager _featureManager;
30+
private readonly IHttpContextAccessor _hca;
31+
32+
public Localizer T { get; set; }
33+
34+
public SqlServerServiceBrokerFeatureGuard(
35+
ShellSettings shellSettings,
36+
IFeatureManager featureManager,
37+
IHttpContextAccessor hca)
38+
{
39+
_shellSettings = shellSettings;
40+
_featureManager = featureManager;
41+
_hca = hca;
42+
43+
T = NullLocalizer.Instance;
44+
}
45+
46+
void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext) { }
47+
48+
void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)
49+
{
50+
var httpContext = _hca.Current();
51+
52+
if (httpContext == null) return;
53+
54+
httpContext.Items[TempDataKey] = filterContext.Controller.TempData;
55+
}
56+
57+
public void Installing(Feature feature) { }
58+
59+
public void Installed(Feature feature) { }
60+
61+
public void Enabling(Feature feature) { }
62+
63+
public void Enabled(Feature feature)
64+
{
65+
if (feature.Descriptor.Id != FeatureId)
66+
return;
67+
68+
if (!_shellSettings.DataProvider.Equals("SqlServer", StringComparison.OrdinalIgnoreCase))
69+
{
70+
_featureManager.DisableFeatures(new[] { FeatureId }, force: true);
71+
72+
AddNotification(
73+
T("The SQL Server Service Broker cannot be enabled because it requires Microsoft SQL Server."),
74+
NotifyType.Error);
75+
}
76+
}
77+
78+
public void Disabling(Feature feature) { }
79+
80+
public void Disabled(Feature feature) { }
81+
82+
public void Uninstalling(Feature feature) { }
83+
84+
public void Uninstalled(Feature feature) { }
85+
86+
private void AddNotification(LocalizedString message, NotifyType notifyType = NotifyType.Warning)
87+
{
88+
var tempDataDictionary = _hca.Current()?.Items[TempDataKey];
89+
90+
if (tempDataDictionary == null) return;
91+
92+
((TempDataDictionary)tempDataDictionary)["messages"] +=
93+
$"{notifyType}:{message.Text}{System.Environment.NewLine}-{System.Environment.NewLine}";
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)