Files
server/src/Api/Controllers/PushController.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

115 lines
3.6 KiB
C#
Raw Normal View History

using Bit.Core.Context;
using Bit.Core.Exceptions;
2021-12-14 15:05:07 +00:00
using Bit.Core.Models.Api;
2018-03-23 09:44:48 -04:00
using Bit.Core.Services;
using Bit.Core.Settings;
using Bit.Core.Utilities;
using Microsoft.AspNetCore.Authorization;
2017-08-11 08:57:31 -04:00
using Microsoft.AspNetCore.Mvc;
namespace Bit.Api.Controllers;
2022-08-29 16:06:55 -04:00
[Route("push")]
[Authorize("Push")]
[SelfHosted(NotSelfHostedOnly = true)]
public class PushController : Controller
{
private readonly IPushRegistrationService _pushRegistrationService;
private readonly IPushNotificationService _pushNotificationService;
private readonly IWebHostEnvironment _environment;
2017-08-11 08:57:31 -04:00
private readonly ICurrentContext _currentContext;
private readonly GlobalSettings _globalSettings;
2022-08-29 16:06:55 -04:00
2017-08-11 08:57:31 -04:00
public PushController(
IPushRegistrationService pushRegistrationService,
IPushNotificationService pushNotificationService,
IWebHostEnvironment environment,
ICurrentContext currentContext,
GlobalSettings globalSettings)
{
_currentContext = currentContext;
_environment = environment;
_pushRegistrationService = pushRegistrationService;
_pushNotificationService = pushNotificationService;
_globalSettings = globalSettings;
2022-08-29 16:06:55 -04:00
}
2017-08-11 08:57:31 -04:00
[HttpPost("register")]
public async Task PostRegister([FromBody] PushRegistrationRequestModel model)
{
CheckUsage();
2017-08-11 08:57:31 -04:00
await _pushRegistrationService.CreateOrUpdateRegistrationAsync(model.PushToken, Prefix(model.DeviceId),
Prefix(model.UserId), Prefix(model.Identifier), model.Type);
}
[HttpDelete("{id}")]
public async Task Delete(string id)
2017-08-11 08:57:31 -04:00
{
CheckUsage();
await _pushRegistrationService.DeleteRegistrationAsync(Prefix(id));
}
[HttpPut("add-organization")]
public async Task PutAddOrganization([FromBody] PushUpdateRequestModel model)
{
2017-08-11 08:57:31 -04:00
CheckUsage();
await _pushRegistrationService.AddUserRegistrationOrganizationAsync(
model.DeviceIds.Select(d => Prefix(d)), Prefix(model.OrganizationId));
}
2017-08-11 08:57:31 -04:00
[HttpPut("delete-organization")]
public async Task PutDeleteOrganization([FromBody] PushUpdateRequestModel model)
{
CheckUsage();
await _pushRegistrationService.DeleteUserRegistrationOrganizationAsync(
model.DeviceIds.Select(d => Prefix(d)), Prefix(model.OrganizationId));
2017-08-11 08:57:31 -04:00
}
2022-08-29 16:06:55 -04:00
[HttpPost("send")]
public async Task PostSend([FromBody] PushSendRequestModel model)
2022-08-29 16:06:55 -04:00
{
CheckUsage();
2017-08-11 08:57:31 -04:00
if (!string.IsNullOrWhiteSpace(model.UserId))
{
2017-08-11 08:57:31 -04:00
await _pushNotificationService.SendPayloadToUserAsync(Prefix(model.UserId),
model.Type.Value, model.Payload, Prefix(model.Identifier), Prefix(model.DeviceId));
}
else if (!string.IsNullOrWhiteSpace(model.OrganizationId))
2022-08-29 14:53:16 -04:00
{
await _pushNotificationService.SendPayloadToOrganizationAsync(Prefix(model.OrganizationId),
2019-03-19 00:39:03 -04:00
model.Type.Value, model.Payload, Prefix(model.Identifier), Prefix(model.DeviceId));
2017-08-11 08:57:31 -04:00
}
2022-08-29 16:06:55 -04:00
}
2017-08-11 08:57:31 -04:00
private string Prefix(string value)
2022-08-29 16:06:55 -04:00
{
2017-08-11 08:57:31 -04:00
if (string.IsNullOrWhiteSpace(value))
{
2017-08-11 08:57:31 -04:00
return null;
}
return $"{_currentContext.InstallationId.Value}_{value}";
}
private void CheckUsage()
2022-08-29 16:06:55 -04:00
{
if (CanUse())
2022-08-29 16:06:55 -04:00
{
return;
}
2022-08-29 14:53:16 -04:00
throw new BadRequestException("Not correctly configured for push relays.");
}
private bool CanUse()
2022-08-29 16:06:55 -04:00
{
if (_environment.IsDevelopment())
2022-08-29 16:06:55 -04:00
{
return true;
}
2022-08-29 16:06:55 -04:00
return _currentContext.InstallationId.HasValue && !_globalSettings.SelfHosted;
}
}