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

91 lines
2.8 KiB
C#
Raw Normal View History

2017-08-11 08:57:31 -04:00
using Microsoft.AspNetCore.Mvc;
using Bit.Core.Services;
using Microsoft.AspNetCore.Authorization;
using Bit.Core;
using Bit.Core.Exceptions;
2017-08-11 08:57:31 -04:00
using Bit.Core.Models.Api;
using System.Threading.Tasks;
using System.Linq;
using Microsoft.AspNetCore.Hosting;
namespace Bit.Api.Controllers
{
[Route("push")]
[Authorize("Push")]
public class PushController : Controller
{
private readonly IPushRegistrationService _pushRegistrationService;
2017-08-11 08:57:31 -04:00
private readonly IHostingEnvironment _environment;
private readonly CurrentContext _currentContext;
2017-08-11 08:57:31 -04:00
private readonly GlobalSettings _globalSettings;
public PushController(
IPushRegistrationService pushRegistrationService,
2017-08-11 08:57:31 -04:00
IHostingEnvironment environment,
CurrentContext currentContext,
GlobalSettings globalSettings)
{
_currentContext = currentContext;
2017-08-11 08:57:31 -04:00
_environment = environment;
_pushRegistrationService = pushRegistrationService;
2017-08-11 08:57:31 -04:00
_globalSettings = globalSettings;
}
2017-08-11 08:57:31 -04:00
[HttpPost("register")]
public async Task PostRegister(PushRegistrationRequestModel model)
{
2017-08-11 08:57:31 -04:00
CheckUsage();
await _pushRegistrationService.CreateOrUpdateRegistrationAsync(model.PushToken, Prefix(model.DeviceId),
Prefix(model.UserId), Prefix(model.Identifier), model.Type);
}
[HttpDelete("{id}")]
public async Task Delete(string id)
{
CheckUsage();
await _pushRegistrationService.DeleteRegistrationAsync(Prefix(id));
}
[HttpPut("add-organization")]
public async Task PutAddOrganization(PushUpdateRequestModel model)
{
CheckUsage();
await _pushRegistrationService.AddUserRegistrationOrganizationAsync(
model.DeviceIds.Select(d => Prefix(d)), Prefix(model.OrganizationId));
}
[HttpPut("delete-organization")]
public async Task PutDeleteOrganization(PushUpdateRequestModel model)
{
CheckUsage();
await _pushRegistrationService.DeleteUserRegistrationOrganizationAsync(
model.DeviceIds.Select(d => Prefix(d)), Prefix(model.OrganizationId));
}
private string Prefix(string value)
{
return $"{_currentContext.InstallationId.Value}_{value}";
}
private void CheckUsage()
{
if(CanUse())
{
return;
}
throw new BadRequestException("Not correctly configured for push relays.");
}
private bool CanUse()
{
if(_environment.IsDevelopment())
{
2017-08-11 08:57:31 -04:00
return true;
}
2017-08-11 08:57:31 -04:00
return _currentContext.InstallationId.HasValue && _globalSettings.SelfHosted;
}
}
}