|
| 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