mirror of
https://github.com/bitwarden/server
synced 2025-12-24 04:03:25 +00:00
[PM-24011] Create new policy sync push notification (#6594)
* create new policy sync push notification * CR feedback * add tests, fix typo
This commit is contained in:
@@ -4,6 +4,8 @@ using Bit.Core.AdminConsole.OrganizationFeatures.Policies.Models;
|
||||
using Bit.Core.AdminConsole.Repositories;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Exceptions;
|
||||
using Bit.Core.Models;
|
||||
using Bit.Core.Platform.Push;
|
||||
using Bit.Core.Services;
|
||||
|
||||
namespace Bit.Core.AdminConsole.OrganizationFeatures.Policies.Implementations;
|
||||
@@ -16,19 +18,22 @@ public class SavePolicyCommand : ISavePolicyCommand
|
||||
private readonly IReadOnlyDictionary<PolicyType, IPolicyValidator> _policyValidators;
|
||||
private readonly TimeProvider _timeProvider;
|
||||
private readonly IPostSavePolicySideEffect _postSavePolicySideEffect;
|
||||
private readonly IPushNotificationService _pushNotificationService;
|
||||
|
||||
public SavePolicyCommand(IApplicationCacheService applicationCacheService,
|
||||
IEventService eventService,
|
||||
IPolicyRepository policyRepository,
|
||||
IEnumerable<IPolicyValidator> policyValidators,
|
||||
TimeProvider timeProvider,
|
||||
IPostSavePolicySideEffect postSavePolicySideEffect)
|
||||
IPostSavePolicySideEffect postSavePolicySideEffect,
|
||||
IPushNotificationService pushNotificationService)
|
||||
{
|
||||
_applicationCacheService = applicationCacheService;
|
||||
_eventService = eventService;
|
||||
_policyRepository = policyRepository;
|
||||
_timeProvider = timeProvider;
|
||||
_postSavePolicySideEffect = postSavePolicySideEffect;
|
||||
_pushNotificationService = pushNotificationService;
|
||||
|
||||
var policyValidatorsDict = new Dictionary<PolicyType, IPolicyValidator>();
|
||||
foreach (var policyValidator in policyValidators)
|
||||
@@ -75,6 +80,8 @@ public class SavePolicyCommand : ISavePolicyCommand
|
||||
await _policyRepository.UpsertAsync(policy);
|
||||
await _eventService.LogPolicyEventAsync(policy, EventType.Policy_Updated);
|
||||
|
||||
await PushPolicyUpdateToClients(policy.OrganizationId, policy);
|
||||
|
||||
return policy;
|
||||
}
|
||||
|
||||
@@ -152,4 +159,17 @@ public class SavePolicyCommand : ISavePolicyCommand
|
||||
var currentPolicy = savedPoliciesDict.GetValueOrDefault(policyUpdate.Type);
|
||||
return (savedPoliciesDict, currentPolicy);
|
||||
}
|
||||
|
||||
Task PushPolicyUpdateToClients(Guid organizationId, Policy policy) => this._pushNotificationService.PushAsync(new PushNotification<SyncPolicyPushNotification>
|
||||
{
|
||||
Type = PushType.PolicyChanged,
|
||||
Target = NotificationTarget.Organization,
|
||||
TargetId = organizationId,
|
||||
ExcludeCurrentContext = false,
|
||||
Payload = new SyncPolicyPushNotification
|
||||
{
|
||||
Policy = policy,
|
||||
OrganizationId = organizationId
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ using Bit.Core.AdminConsole.OrganizationFeatures.Policies.PolicyUpdateEvents.Int
|
||||
using Bit.Core.AdminConsole.Repositories;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Exceptions;
|
||||
using Bit.Core.Models;
|
||||
using Bit.Core.Platform.Push;
|
||||
using Bit.Core.Services;
|
||||
|
||||
namespace Bit.Core.AdminConsole.OrganizationFeatures.Policies.Implementations;
|
||||
@@ -15,7 +17,8 @@ public class VNextSavePolicyCommand(
|
||||
IPolicyRepository policyRepository,
|
||||
IEnumerable<IPolicyUpdateEvent> policyUpdateEventHandlers,
|
||||
TimeProvider timeProvider,
|
||||
IPolicyEventHandlerFactory policyEventHandlerFactory)
|
||||
IPolicyEventHandlerFactory policyEventHandlerFactory,
|
||||
IPushNotificationService pushNotificationService)
|
||||
: IVNextSavePolicyCommand
|
||||
{
|
||||
|
||||
@@ -74,7 +77,7 @@ public class VNextSavePolicyCommand(
|
||||
policy.RevisionDate = timeProvider.GetUtcNow().UtcDateTime;
|
||||
|
||||
await policyRepository.UpsertAsync(policy);
|
||||
|
||||
await PushPolicyUpdateToClients(policyUpdateRequest.OrganizationId, policy);
|
||||
return policy;
|
||||
}
|
||||
|
||||
@@ -192,4 +195,17 @@ public class VNextSavePolicyCommand(
|
||||
var savedPoliciesDict = savedPolicies.ToDictionary(p => p.Type);
|
||||
return savedPoliciesDict;
|
||||
}
|
||||
|
||||
Task PushPolicyUpdateToClients(Guid organizationId, Policy policy) => pushNotificationService.PushAsync(new PushNotification<SyncPolicyPushNotification>
|
||||
{
|
||||
Type = PushType.PolicyChanged,
|
||||
Target = NotificationTarget.Organization,
|
||||
TargetId = organizationId,
|
||||
ExcludeCurrentContext = false,
|
||||
Payload = new SyncPolicyPushNotification
|
||||
{
|
||||
Policy = policy,
|
||||
OrganizationId = organizationId
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.NotificationCenter.Enums;
|
||||
|
||||
namespace Bit.Core.Models;
|
||||
@@ -103,3 +104,9 @@ public class LogOutPushNotification
|
||||
public Guid UserId { get; set; }
|
||||
public PushNotificationLogOutReason? Reason { get; set; }
|
||||
}
|
||||
|
||||
public class SyncPolicyPushNotification
|
||||
{
|
||||
public Guid OrganizationId { get; set; }
|
||||
public required Policy Policy { get; set; }
|
||||
}
|
||||
|
||||
@@ -95,5 +95,8 @@ public enum PushType : byte
|
||||
OrganizationBankAccountVerified = 23,
|
||||
|
||||
[NotificationInfo("@bitwarden/team-billing-dev", typeof(Models.ProviderBankAccountVerifiedPushNotification))]
|
||||
ProviderBankAccountVerified = 24
|
||||
ProviderBankAccountVerified = 24,
|
||||
|
||||
[NotificationInfo("@bitwarden/team-admin-console-dev", typeof(Models.SyncPolicyPushNotification))]
|
||||
PolicyChanged = 25,
|
||||
}
|
||||
|
||||
@@ -231,9 +231,26 @@ public class HubHelpers
|
||||
await _hubContext.Clients.User(pendingTasksData.Payload.UserId.ToString())
|
||||
.SendAsync(_receiveMessageMethod, pendingTasksData, cancellationToken);
|
||||
break;
|
||||
case PushType.PolicyChanged:
|
||||
await policyChangedNotificationHandler(notificationJson, cancellationToken);
|
||||
break;
|
||||
default:
|
||||
_logger.LogWarning("Notification type '{NotificationType}' has not been registered in HubHelpers and will not be pushed as as result", notification.Type);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task policyChangedNotificationHandler(string notificationJson, CancellationToken cancellationToken)
|
||||
{
|
||||
var policyData = JsonSerializer.Deserialize<PushNotificationData<SyncPolicyPushNotification>>(notificationJson, _deserializerOptions);
|
||||
if (policyData is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await _hubContext.Clients
|
||||
.Group(NotificationsHub.GetOrganizationGroup(policyData.Payload.OrganizationId))
|
||||
.SendAsync(_receiveMessageMethod, policyData, cancellationToken);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user