From db41a1bd1363d49a206d2ad227d0dcc0b6d91afa Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 3 Jul 2019 08:58:18 -0400 Subject: [PATCH] collect many events --- src/Core/Enums/EventType.cs | 2 +- src/Events/Controllers/CollectController.cs | 46 +++++++++++++++------ 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/Core/Enums/EventType.cs b/src/Core/Enums/EventType.cs index 10d517994..7f8786180 100644 --- a/src/Core/Enums/EventType.cs +++ b/src/Core/Enums/EventType.cs @@ -9,7 +9,7 @@ User_Recovered2fa = 1004, User_FailedLogIn = 1005, User_FailedLogIn2fa = 1006, - User_ExportedVault = 1007, + User_ClientExportedVault = 1007, Cipher_Created = 1100, Cipher_Updated = 1101, diff --git a/src/Events/Controllers/CollectController.cs b/src/Events/Controllers/CollectController.cs index cf7c4626e..08874c19e 100644 --- a/src/Events/Controllers/CollectController.cs +++ b/src/Events/Controllers/CollectController.cs @@ -1,4 +1,6 @@ -using System.Threading.Tasks; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; using Bit.Core; using Bit.Core.Enums; using Bit.Core.Repositories; @@ -27,19 +29,39 @@ namespace Bit.Events.Controllers _cipherRepository = cipherRepository; } - [HttpGet] - public Task Get([FromQuery]EventModel model) - { - return Post(model); - } - [HttpPost] public async Task Post([FromBody]EventModel model) + { + if(await LogEventAsync(model)) + { + return new OkResult(); + } + else + { + return new BadRequestResult(); + } + } + + [HttpPost("many")] + public async Task PostMany([FromBody]IEnumerable model) + { + if(model == null || !model.Any()) + { + return new BadRequestResult(); + } + foreach(var eventModel in model) + { + await LogEventAsync(eventModel); + } + return new OkResult(); + } + + private async Task LogEventAsync(EventModel model) { switch(model.Type) { // User events - case EventType.User_ExportedVault: + case EventType.User_ClientExportedVault: await _eventService.LogUserEventAsync(_currentContext.UserId.Value, model.Type); break; // Cipher events @@ -51,20 +73,20 @@ namespace Bit.Events.Controllers case EventType.Cipher_ClientViewed: if(!model.CipherId.HasValue) { - return new BadRequestResult(); + return false; } var cipher = await _cipherRepository.GetByIdAsync(model.CipherId.Value, _currentContext.UserId.Value); if(cipher == null) { - return new BadRequestResult(); + return false; } await _eventService.LogCipherEventAsync(cipher, model.Type); break; default: - return new BadRequestResult(); + return false; } - return new OkResult(); + return true; } } }