Files
server/src/Events/Controllers/CollectController.cs

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

106 lines
4.0 KiB
C#
Raw Normal View History

using Bit.Core.Context;
using Bit.Core.Entities;
2019-03-19 17:26:34 -04:00
using Bit.Core.Enums;
using Bit.Core.Repositories;
using Bit.Core.Services;
using Bit.Events.Models;
using Microsoft.AspNetCore.Authorization;
2017-12-04 10:12:11 -05:00
using Microsoft.AspNetCore.Mvc;
2019-03-19 17:45:31 -04:00
namespace Bit.Events.Controllers;
2022-08-29 16:06:55 -04:00
2019-03-19 17:45:31 -04:00
[Route("collect")]
[Authorize("Application")]
2019-03-19 17:45:31 -04:00
public class CollectController : Controller
2017-12-04 10:12:11 -05:00
{
2019-03-19 17:45:31 -04:00
private readonly ICurrentContext _currentContext;
private readonly IEventService _eventService;
private readonly ICipherRepository _cipherRepository;
private readonly IOrganizationRepository _organizationRepository;
2022-08-29 16:06:55 -04:00
2019-03-19 17:45:31 -04:00
public CollectController(
ICurrentContext currentContext,
IEventService eventService,
ICipherRepository cipherRepository,
IOrganizationRepository organizationRepository)
2017-12-04 10:12:11 -05:00
{
_currentContext = currentContext;
_eventService = eventService;
_cipherRepository = cipherRepository;
_organizationRepository = organizationRepository;
2022-08-29 16:06:55 -04:00
}
2019-07-03 12:11:40 -04:00
[HttpPost]
2019-03-19 17:45:31 -04:00
public async Task<IActionResult> Post([FromBody] IEnumerable<EventModel> model)
2022-08-29 16:06:55 -04:00
{
2019-03-19 17:45:31 -04:00
if (model == null || !model.Any())
{
return new BadRequestResult();
}
2019-07-03 12:11:40 -04:00
var cipherEvents = new List<Tuple<Cipher, EventType, DateTime?>>();
var ciphersCache = new Dictionary<Guid, Cipher>();
foreach (var eventModel in model)
2019-07-03 08:58:18 -04:00
{
switch (eventModel.Type)
2019-07-03 08:58:18 -04:00
{
// User events
case EventType.User_ClientExportedVault:
await _eventService.LogUserEventAsync(_currentContext.UserId.Value, eventModel.Type, eventModel.Date);
break;
// Cipher events
case EventType.Cipher_ClientAutofilled:
case EventType.Cipher_ClientCopiedHiddenField:
case EventType.Cipher_ClientCopiedPassword:
case EventType.Cipher_ClientCopiedCardCode:
case EventType.Cipher_ClientToggledCardNumberVisible:
case EventType.Cipher_ClientToggledCardCodeVisible:
case EventType.Cipher_ClientToggledHiddenFieldVisible:
case EventType.Cipher_ClientToggledPasswordVisible:
case EventType.Cipher_ClientViewed:
if (!eventModel.CipherId.HasValue)
{
continue;
}
2019-07-25 16:29:34 -04:00
Cipher cipher = null;
if (ciphersCache.ContainsKey(eventModel.CipherId.Value))
2019-07-25 16:29:34 -04:00
{
cipher = ciphersCache[eventModel.CipherId.Value];
}
else
{
cipher = await _cipherRepository.GetByIdAsync(eventModel.CipherId.Value,
_currentContext.UserId.Value);
}
if (cipher == null)
{
continue;
}
if (!ciphersCache.ContainsKey(eventModel.CipherId.Value))
2019-07-25 16:29:34 -04:00
{
ciphersCache.Add(eventModel.CipherId.Value, cipher);
}
cipherEvents.Add(new Tuple<Cipher, EventType, DateTime?>(cipher, eventModel.Type, eventModel.Date));
break;
case EventType.Organization_ClientExportedVault:
if (!eventModel.OrganizationId.HasValue)
{
continue;
}
var organization = await _organizationRepository.GetByIdAsync(eventModel.OrganizationId.Value);
await _eventService.LogOrganizationEventAsync(organization, eventModel.Type, eventModel.Date);
2022-08-29 16:06:55 -04:00
break;
default:
continue;
2019-07-03 08:58:18 -04:00
}
2022-08-29 16:06:55 -04:00
}
if (cipherEvents.Any())
2022-08-29 16:06:55 -04:00
{
2022-09-14 14:57:05 -04:00
foreach (var eventsBatch in cipherEvents.Chunk(50))
2019-03-19 17:26:34 -04:00
{
2019-07-25 15:50:13 -04:00
await _eventService.LogCipherEventsAsync(eventsBatch);
2019-03-19 17:26:34 -04:00
}
2017-12-04 10:12:11 -05:00
}
return new OkResult();
2017-12-04 10:12:11 -05:00
}
}