-
Notifications
You must be signed in to change notification settings - Fork 2
/
DashboardQuotaFilter.cs
58 lines (50 loc) · 1.91 KB
/
DashboardQuotaFilter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using Lombiq.Hosting.Tenants.EmailQuotaManagement.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using OrchardCore.DisplayManagement;
using OrchardCore.DisplayManagement.Layout;
using System.Threading.Tasks;
namespace Lombiq.Hosting.Tenants.EmailQuotaManagement.Filters;
public class DashboardQuotaFilter : IAsyncResultFilter
{
private readonly IShapeFactory _shapeFactory;
private readonly ILayoutAccessor _layoutAccessor;
private readonly IEmailQuotaService _emailQuotaService;
public DashboardQuotaFilter(
IShapeFactory shapeFactory,
ILayoutAccessor layoutAccessor,
IEmailQuotaService emailQuotaService)
{
_shapeFactory = shapeFactory;
_layoutAccessor = layoutAccessor;
_emailQuotaService = emailQuotaService;
}
public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
{
if (!context.IsAdmin())
{
await next();
return;
}
if (context.Result is ViewResult &&
_emailQuotaService.ShouldLimitEmails())
{
var currentEmailQuota = await _emailQuotaService.IsQuotaOverTheLimitAsync();
var currentUsagePercentage = currentEmailQuota.EmailQuota
.CurrentUsagePercentage(_emailQuotaService.GetEmailQuotaPerMonth());
if (currentUsagePercentage >= 80)
{
var layout = await _layoutAccessor.GetLayoutAsync();
var contentZone = layout.Zones["Messages"];
await contentZone.AddAsync(
await _shapeFactory.CreateAsync("DashboardQuotaMessage", new
{
currentEmailQuota.IsOverQuota,
UsagePercentage = currentUsagePercentage,
}),
"0");
}
}
await next();
}
}