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

71 lines
2.4 KiB
C#
Raw Normal View History

2019-03-19 17:26:34 -04:00
using System.Threading.Tasks;
using Bit.Core;
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
2017-12-04 10:12:11 -05: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
{
private readonly CurrentContext _currentContext;
private readonly IEventService _eventService;
private readonly ICipherRepository _cipherRepository;
2019-03-19 17:45:31 -04:00
public CollectController(
CurrentContext currentContext,
IEventService eventService,
ICipherRepository cipherRepository)
{
_currentContext = currentContext;
_eventService = eventService;
_cipherRepository = cipherRepository;
}
2019-03-19 17:45:31 -04:00
[HttpGet]
public Task<IActionResult> Get([FromQuery]EventModel model)
{
2019-03-19 17:45:31 -04:00
return Post(model);
}
2019-03-19 17:45:31 -04:00
[HttpPost]
public async Task<IActionResult> Post([FromBody]EventModel model)
2017-12-04 10:12:11 -05:00
{
2019-03-19 17:26:34 -04:00
switch(model.Type)
{
// User events
2019-06-20 08:56:10 -04:00
case EventType.User_ExportedVault:
2019-03-19 17:26:34 -04:00
await _eventService.LogUserEventAsync(_currentContext.UserId.Value, model.Type);
break;
// Cipher events
2019-06-20 08:56:10 -04:00
case EventType.Cipher_ClientAutofilled:
case EventType.Cipher_ClientCopedHiddenField:
case EventType.Cipher_ClientCopiedPassword:
case EventType.Cipher_ClientToggledHiddenFieldVisible:
case EventType.Cipher_ClientToggledPasswordVisible:
case EventType.Cipher_ClientViewed:
2019-03-19 17:26:34 -04:00
if(!model.CipherId.HasValue)
{
return new BadRequestResult();
}
var cipher = await _cipherRepository.GetByIdAsync(model.CipherId.Value,
_currentContext.UserId.Value);
if(cipher == null)
{
return new BadRequestResult();
}
await _eventService.LogCipherEventAsync(cipher, model.Type);
break;
default:
return new BadRequestResult();
}
return new OkResult();
2017-12-04 10:12:11 -05:00
}
}
}