mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 16:12:49 -05:00
PM-10600: Organization push notifications not sending to mobile device from self-hosted.
Self-hosted instance uses relay to register the mobile device against Bitwarden Cloud Api. Only the self-hosted server knows client's organization membership, which means it needs to pass in the organization id's information to the relay. Similarly, for Bitwarden Cloud, the organizaton id will come directly from the server.
This commit is contained in:
@ -39,7 +39,7 @@ public class PushController : Controller
|
||||
{
|
||||
CheckUsage();
|
||||
await _pushRegistrationService.CreateOrUpdateRegistrationAsync(model.PushToken, Prefix(model.DeviceId),
|
||||
Prefix(model.UserId), Prefix(model.Identifier), model.Type);
|
||||
Prefix(model.UserId), Prefix(model.Identifier), model.Type, model.OrganizationIds);
|
||||
}
|
||||
|
||||
[HttpPost("delete")]
|
||||
|
@ -15,4 +15,5 @@ public class PushRegistrationRequestModel
|
||||
public DeviceType Type { get; set; }
|
||||
[Required]
|
||||
public string Identifier { get; set; }
|
||||
public IEnumerable<string> OrganizationIds { get; set; }
|
||||
}
|
||||
|
@ -11,20 +11,17 @@ public class NotificationHubPushRegistrationService : IPushRegistrationService
|
||||
{
|
||||
private readonly IInstallationDeviceRepository _installationDeviceRepository;
|
||||
private readonly INotificationHubPool _notificationHubPool;
|
||||
private readonly IOrganizationUserRepository _organizationUserRepository;
|
||||
|
||||
public NotificationHubPushRegistrationService(
|
||||
IInstallationDeviceRepository installationDeviceRepository,
|
||||
INotificationHubPool notificationHubPool,
|
||||
IOrganizationUserRepository organizationUserRepository)
|
||||
INotificationHubPool notificationHubPool)
|
||||
{
|
||||
_installationDeviceRepository = installationDeviceRepository;
|
||||
_notificationHubPool = notificationHubPool;
|
||||
_organizationUserRepository = organizationUserRepository;
|
||||
}
|
||||
|
||||
public async Task CreateOrUpdateRegistrationAsync(string pushToken, string deviceId, string userId,
|
||||
string identifier, DeviceType type)
|
||||
string identifier, DeviceType type, IEnumerable<string> organizationIds)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(pushToken))
|
||||
{
|
||||
@ -47,10 +44,10 @@ public class NotificationHubPushRegistrationService : IPushRegistrationService
|
||||
installation.Tags.Add("deviceIdentifier:" + identifier);
|
||||
}
|
||||
|
||||
foreach (var organizationUserDetails in await _organizationUserRepository.GetManyDetailsByUserAsync(
|
||||
Guid.Parse(userId), OrganizationUserStatusType.Confirmed))
|
||||
var organizationIdsList = organizationIds.ToList();
|
||||
foreach (var organizationId in organizationIdsList)
|
||||
{
|
||||
installation.Tags.Add($"organizationId:{organizationUserDetails.OrganizationId}");
|
||||
installation.Tags.Add($"organizationId:{organizationId}");
|
||||
}
|
||||
|
||||
string payloadTemplate = null, messageTemplate = null, badgeMessageTemplate = null;
|
||||
@ -82,10 +79,12 @@ public class NotificationHubPushRegistrationService : IPushRegistrationService
|
||||
break;
|
||||
}
|
||||
|
||||
BuildInstallationTemplate(installation, "payload", payloadTemplate, userId, identifier, clientType);
|
||||
BuildInstallationTemplate(installation, "message", messageTemplate, userId, identifier, clientType);
|
||||
BuildInstallationTemplate(installation, "payload", payloadTemplate, userId, identifier, clientType,
|
||||
organizationIdsList);
|
||||
BuildInstallationTemplate(installation, "message", messageTemplate, userId, identifier, clientType,
|
||||
organizationIdsList);
|
||||
BuildInstallationTemplate(installation, "badgeMessage", badgeMessageTemplate ?? messageTemplate,
|
||||
userId, identifier, clientType);
|
||||
userId, identifier, clientType, organizationIdsList);
|
||||
|
||||
await ClientFor(GetComb(deviceId)).CreateOrUpdateInstallationAsync(installation);
|
||||
if (InstallationDeviceEntity.IsInstallationDeviceId(deviceId))
|
||||
@ -95,7 +94,7 @@ public class NotificationHubPushRegistrationService : IPushRegistrationService
|
||||
}
|
||||
|
||||
private void BuildInstallationTemplate(Installation installation, string templateId, string templateBody,
|
||||
string userId, string identifier, ClientType clientType)
|
||||
string userId, string identifier, ClientType clientType, List<string> organizationIds)
|
||||
{
|
||||
if (templateBody == null)
|
||||
{
|
||||
@ -118,6 +117,11 @@ public class NotificationHubPushRegistrationService : IPushRegistrationService
|
||||
template.Tags.Add($"{fullTemplateId}_deviceIdentifier:{identifier}");
|
||||
}
|
||||
|
||||
foreach (var organizationId in organizationIds)
|
||||
{
|
||||
template.Tags.Add($"organizationId:{organizationId}");
|
||||
}
|
||||
|
||||
installation.Templates.Add(fullTemplateId, template);
|
||||
}
|
||||
|
||||
|
@ -5,7 +5,7 @@ namespace Bit.Core.Services;
|
||||
public interface IPushRegistrationService
|
||||
{
|
||||
Task CreateOrUpdateRegistrationAsync(string pushToken, string deviceId, string userId,
|
||||
string identifier, DeviceType type);
|
||||
string identifier, DeviceType type, IEnumerable<string> organizationIds);
|
||||
Task DeleteRegistrationAsync(string deviceId);
|
||||
Task AddUserRegistrationOrganizationAsync(IEnumerable<string> deviceIds, string organizationId);
|
||||
Task DeleteUserRegistrationOrganizationAsync(IEnumerable<string> deviceIds, string organizationId);
|
||||
|
@ -1,6 +1,7 @@
|
||||
using Bit.Core.Auth.Models.Api.Request;
|
||||
using Bit.Core.Auth.Utilities;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Exceptions;
|
||||
using Bit.Core.Repositories;
|
||||
|
||||
@ -10,13 +11,16 @@ public class DeviceService : IDeviceService
|
||||
{
|
||||
private readonly IDeviceRepository _deviceRepository;
|
||||
private readonly IPushRegistrationService _pushRegistrationService;
|
||||
private readonly IOrganizationUserRepository _organizationUserRepository;
|
||||
|
||||
public DeviceService(
|
||||
IDeviceRepository deviceRepository,
|
||||
IPushRegistrationService pushRegistrationService)
|
||||
IPushRegistrationService pushRegistrationService,
|
||||
IOrganizationUserRepository organizationUserRepository)
|
||||
{
|
||||
_deviceRepository = deviceRepository;
|
||||
_pushRegistrationService = pushRegistrationService;
|
||||
_organizationUserRepository = organizationUserRepository;
|
||||
}
|
||||
|
||||
public async Task SaveAsync(Device device)
|
||||
@ -31,8 +35,13 @@ public class DeviceService : IDeviceService
|
||||
await _deviceRepository.ReplaceAsync(device);
|
||||
}
|
||||
|
||||
var organizationIdsString =
|
||||
(await _organizationUserRepository.GetManyDetailsByUserAsync(device.UserId,
|
||||
OrganizationUserStatusType.Confirmed))
|
||||
.Select(ou => ou.OrganizationId.ToString());
|
||||
|
||||
await _pushRegistrationService.CreateOrUpdateRegistrationAsync(device.PushToken, device.Id.ToString(),
|
||||
device.UserId.ToString(), device.Identifier, device.Type);
|
||||
device.UserId.ToString(), device.Identifier, device.Type, organizationIdsString);
|
||||
}
|
||||
|
||||
public async Task ClearTokenAsync(Device device)
|
||||
|
@ -25,7 +25,7 @@ public class RelayPushRegistrationService : BaseIdentityClientService, IPushRegi
|
||||
}
|
||||
|
||||
public async Task CreateOrUpdateRegistrationAsync(string pushToken, string deviceId, string userId,
|
||||
string identifier, DeviceType type)
|
||||
string identifier, DeviceType type, IEnumerable<string> organizationIds)
|
||||
{
|
||||
var requestModel = new PushRegistrationRequestModel
|
||||
{
|
||||
@ -33,7 +33,8 @@ public class RelayPushRegistrationService : BaseIdentityClientService, IPushRegi
|
||||
Identifier = identifier,
|
||||
PushToken = pushToken,
|
||||
Type = type,
|
||||
UserId = userId
|
||||
UserId = userId,
|
||||
OrganizationIds = organizationIds
|
||||
};
|
||||
await SendAsync(HttpMethod.Post, "push/register", requestModel);
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ public class NoopPushRegistrationService : IPushRegistrationService
|
||||
}
|
||||
|
||||
public Task CreateOrUpdateRegistrationAsync(string pushToken, string deviceId, string userId,
|
||||
string identifier, DeviceType type)
|
||||
string identifier, DeviceType type, IEnumerable<string> organizationIds)
|
||||
{
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
Reference in New Issue
Block a user