2019-03-19 17:26:34 -04:00
|
|
|
|
using System.Threading.Tasks;
|
2017-12-04 10:59:07 -05:00
|
|
|
|
using Bit.Core;
|
2019-03-19 17:26:34 -04:00
|
|
|
|
using Bit.Core.Enums;
|
2017-12-04 10:59:07 -05:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Events.Controllers
|
|
|
|
|
|
{
|
2017-12-04 10:59:07 -05:00
|
|
|
|
[Authorize("Application")]
|
2017-12-04 10:12:11 -05:00
|
|
|
|
public class EventsController : Controller
|
|
|
|
|
|
{
|
2017-12-04 10:59:07 -05:00
|
|
|
|
private readonly CurrentContext _currentContext;
|
|
|
|
|
|
private readonly IEventService _eventService;
|
|
|
|
|
|
private readonly ICipherRepository _cipherRepository;
|
|
|
|
|
|
|
|
|
|
|
|
public EventsController(
|
|
|
|
|
|
CurrentContext currentContext,
|
|
|
|
|
|
IEventService eventService,
|
|
|
|
|
|
ICipherRepository cipherRepository)
|
|
|
|
|
|
{
|
|
|
|
|
|
_currentContext = currentContext;
|
|
|
|
|
|
_eventService = eventService;
|
|
|
|
|
|
_cipherRepository = cipherRepository;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-03-19 17:26:34 -04:00
|
|
|
|
[HttpGet("~/collect")]
|
|
|
|
|
|
public Task<IActionResult> GetCollect([FromQuery]EventModel model)
|
2017-12-04 10:59:07 -05:00
|
|
|
|
{
|
2019-03-19 17:26:34 -04:00
|
|
|
|
return PostCollect(model);
|
2017-12-04 10:59:07 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-03-19 17:26:34 -04:00
|
|
|
|
[HttpPost("~/collect")]
|
|
|
|
|
|
public async Task<IActionResult> PostCollect([FromBody]EventModel model)
|
2017-12-04 10:12:11 -05:00
|
|
|
|
{
|
2019-03-19 17:26:34 -04:00
|
|
|
|
switch(model.Type)
|
|
|
|
|
|
{
|
|
|
|
|
|
// User events
|
|
|
|
|
|
case EventType.User_LoggedIn:
|
|
|
|
|
|
await _eventService.LogUserEventAsync(_currentContext.UserId.Value, model.Type);
|
|
|
|
|
|
break;
|
|
|
|
|
|
// Cipher events
|
|
|
|
|
|
case EventType.Cipher_Created:
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|