Skip to content

Commit 5698f3e

Browse files
authored
Merge pull request #7295 from umbraco/cms/server-events
Adds details on SignalR server events
2 parents 7b4f79c + 2d0457a commit 5698f3e

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

16/umbraco-cms/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@
249249
* [Custom File Systems (IFileSystem)](extending/filesystemproviders/README.md)
250250
* [Using Azure Blob Storage for Media and ImageSharp Cache](extending/filesystemproviders/azure-blob-storage.md)
251251
* [Configuring Azure Key Vault](extending/key-vault.md)
252+
* [Server Events From SignalR](extending/server-events.md)
252253
* [Packages](extending/packages/README.md)
253254
* [Creating a Package](extending/packages/creating-a-package.md)
254255
* [Language file for packages](extending/packages/language-files-for-packages.md)
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
---
2+
description: Describes server events emitted via a SignalR hub and available for consumption in the backoffice
3+
---
4+
5+
# Server Events
6+
7+
Umbraco registers a SignalR event hub that broadcasts events related to the update of entities. These can be used in the backoffice to respond to changes made by users other than the current editor.
8+
9+
Each server event is triggered via a notification handler. So for example, when a document is saved, the `ContentSavedNotification` is published. This is handled by a class responsible for issuing a server event.
10+
11+
Not all server events should be broadcast to all users. For example, if a user doesn't have access to the Media section, they shouldn't receive notifications on updates to media. For core entities, Umbraco uses the same permission system that defines access in the backoffice. In this way, only events appropriate for the currently logged in editor are exposed.
12+
13+
## Event Information
14+
15+
Each event emitted contains the following fields:
16+
17+
- `EventType` - the event type, which might be `Created`, `Updated`, `Deleted` etc.
18+
- `EventSource` - the event source, which might be `Document`, `Media` etc.
19+
- `Key` - the unique GUID that identifies the entity changed.
20+
21+
## Event Authorization
22+
23+
The currently authorized user will have one or more claims indicating the access they have to different areas of the backoffice. When first connecting to the SignalR hub, these details are used to assign the user to one or more SignalR groups.
24+
25+
Which groups they have access to is determined by the collection of registered `IEventSourceAuthorizer` instances.
26+
27+
For example, there is a `DocumentEventAuthorizer` that ensures users with access to the documents (content) tree are assigned to the `Umbraco:CMS:Document` event source. As a result, when server events are emitted for documents, only those users with this access will receive them.
28+
29+
## Extending Server Events
30+
31+
Using the same patterns as the core CMS, server events for other entities defined in packages or custom solutions can be emitted.
32+
33+
Firstly, a `IEventSourceAuthorizer` should be registered. This will likely hook into what you already have in place for controlling access to the backoffice section where the entity is managed.
34+
35+
For example, if you had a `Product` entity managed in a custom CMS section, the authorizer might look like this:
36+
37+
```csharp
38+
using Microsoft.AspNetCore.Authorization;
39+
using Umbraco.Cms.Core;
40+
using Umbraco.Cms.Web.Common.Authorization;
41+
42+
public class ProductEventAuthorizer : EventSourcePolicyAuthorizer
43+
{
44+
public ProductEventAuthorizer(IAuthorizationService authorizationService)
45+
: base(authorizationService)
46+
{
47+
}
48+
49+
public override IEnumerable<string> AuthorizableEventSources => ["Umbraco:Custom:Product"];
50+
51+
protected override string Policy => "TreeAccessProducts"; // Maps to an existing authorization policy
52+
// used in an [Authorize] attribute.
53+
}
54+
```
55+
56+
The authorizer should be registered with Umbraco using something like the following, called from a composer, or otherwise in application start up:
57+
58+
```csharp
59+
public static IUmbracoBuilder AddCustomAuthorizers(this IUmbracoBuilder builder)
60+
{
61+
builder.EventSourceAuthorizers()
62+
.Append<ProductEventAuthorizer>();
63+
}
64+
```
65+
66+
You then need to emit the event from a notification handler which handles notifications published when the entity is updated.
67+
68+
For example, assuming an existing `ProductSavedNotification` that is published with the product is saved:
69+
70+
```csharp
71+
using Umbraco.Cms.Core;
72+
using Umbraco.Cms.Core.Events;
73+
using Umbraco.Cms.Core.Models.Entities;
74+
using Umbraco.Cms.Core.Models.ServerEvents;
75+
using Umbraco.Cms.Core.Notifications;
76+
using Umbraco.Cms.Core.ServerEvents;
77+
78+
namespace Umbraco.Cms.Api.Management.ServerEvents;
79+
80+
public class ProductServerEventSender : INotificationAsyncHandler<ProductSavedNotification>
81+
{
82+
private readonly IServerEventRouter _serverEventRouter;
83+
84+
public ProductServerEventSender(IServerEventRouter serverEventRouter) => _serverEventRouter = serverEventRouter;
85+
86+
public async Task HandleAsync(ProductSavedNotification notification, CancellationToken cancellationToken) =>
87+
await NotifySavedAsync(notification, "Umbraco:Custom:Product");
88+
89+
private async Task NotifySavedAsync<T>(SavedNotification<T> notification, string source)
90+
where T : IEntity
91+
{
92+
foreach (T entity in notification.SavedEntities)
93+
{
94+
var eventModel = new ServerEvent
95+
{
96+
EventType = entity.CreateDate == entity.UpdateDate
97+
? Constants.ServerEvents.EventType.Created : Constants.ServerEvents.EventType.Updated,
98+
Key = entity.Key,
99+
EventSource = source,
100+
};
101+
102+
await _serverEventRouter.RouteEventAsync(eventModel);
103+
}
104+
}
105+
}
106+
```
107+
108+
Again, the notification handler will need to be registered with Umbraco:
109+
110+
```csharp
111+
public static IUmbracoBuilder AddCustomEvents(this IUmbracoBuilder builder)
112+
{
113+
builder.AddNotificationAsyncHandler<ProductSavedNotification, ProductServerEventSender>();
114+
}
115+
```
116+
117+
118+

0 commit comments

Comments
 (0)