Line data Source code
1 : #region Copyright
2 : // // -----------------------------------------------------------------------
3 : // // <copyright company="Chinchilla Software Limited">
4 : // // Copyright Chinchilla Software Limited. All rights reserved.
5 : // // </copyright>
6 : // // -----------------------------------------------------------------------
7 : #endregion
8 :
9 : using System;
10 : using cdmdotnet.Logging;
11 : using Cqrs.Authentication;
12 : using Cqrs.Events;
13 : using Cqrs.WebApi.SignalR.Hubs;
14 :
15 : namespace Cqrs.WebApi.Events.Handlers
16 : {
17 : /// <summary>
18 : /// Proxies <see cref="IEvent{TSingleSignOnToken}">events</see> from the event bus to the <see cref="INotificationHub"/>.
19 : /// This requires one or more <see cref="IEventHandler"/> implementations in order to be triggered.
20 : /// </summary>
21 : /// <typeparam name="TSingleSignOnToken">The <see cref="ISingleSignOnToken"/> of the authentication token.</typeparam>
22 : public abstract class SingleSignOnTokenEventToHubProxy<TSingleSignOnToken>
23 : where TSingleSignOnToken : ISingleSignOnToken, new()
24 1 : {
25 : /// <summary>
26 : /// Instantiates a new instance of <see cref="SingleSignOnTokenEventToHubProxy{TSingleSignOnToken}"/>.
27 : /// </summary>
28 1 : protected SingleSignOnTokenEventToHubProxy(ILogger logger, INotificationHub notificationHub, IAuthenticationTokenHelper<TSingleSignOnToken> authenticationTokenHelper)
29 : {
30 : Logger = logger;
31 : NotificationHub = notificationHub;
32 : AuthenticationTokenHelper = authenticationTokenHelper;
33 : }
34 :
35 : /// <summary>
36 : /// Gets or sets the <see cref="ILogger"/>.
37 : /// </summary>
38 : protected ILogger Logger { get; private set; }
39 :
40 : /// <summary>
41 : /// The <see cref="INotificationHub"/> to proxy <see cref="IEvent{TAuthenticationToken}">events</see> to.
42 : /// </summary>
43 : protected INotificationHub NotificationHub { get; private set; }
44 :
45 : /// <summary>
46 : /// Gets or sets the <see cref="IAuthenticationTokenHelper{TSingleSignOnToken}"/>.
47 : /// </summary>
48 : protected IAuthenticationTokenHelper<TSingleSignOnToken> AuthenticationTokenHelper { get; private set; }
49 :
50 : /// <summary>
51 : /// Get the authentication token of the entity that triggered the request generating the provided <paramref name="message"/>
52 : /// Extract any proxy information attributes (<see cref="NotifyCallerEventAttribute"/>, <see cref="NotifyEveryoneEventAttribute"/> and <see cref="NotifyEveryoneExceptCallerEventAttribute"/>)
53 : /// then proxy the provided <paramref name="message"/> to <see cref="NotificationHub"/> if an attribute is present.
54 : /// </summary>
55 : /// <param name="message">The <see cref="IEvent{TSingleSignOnToken}"/> to proxy.</param>
56 1 : protected virtual void HandleGenericEvent(IEvent<TSingleSignOnToken> message)
57 : {
58 : Type eventType = message.GetType();
59 : var notifyCallerEventAttribute = Attribute.GetCustomAttribute(eventType, typeof(NotifyCallerEventAttribute)) as NotifyCallerEventAttribute;
60 : var notifyEveryoneEventAttribute = Attribute.GetCustomAttribute(eventType, typeof(NotifyEveryoneEventAttribute)) as NotifyEveryoneEventAttribute;
61 : var notifyEveryoneExceptCallerEventAttribute = Attribute.GetCustomAttribute(eventType, typeof(NotifyEveryoneExceptCallerEventAttribute)) as NotifyEveryoneExceptCallerEventAttribute;
62 :
63 : string userToken = (AuthenticationTokenHelper.GetAuthenticationToken().Token ?? string.Empty).Replace(".", string.Empty);
64 :
65 : if (notifyCallerEventAttribute != null)
66 : {
67 : NotificationHub.SendUserEvent(message, userToken);
68 : }
69 : if (notifyEveryoneEventAttribute != null)
70 : {
71 : NotificationHub.SendAllUsersEvent(message);
72 : }
73 : if (notifyEveryoneExceptCallerEventAttribute != null)
74 : {
75 : NotificationHub.SendExceptThisUserEvent(message, userToken);
76 : }
77 : }
78 : }
79 : }
|