mirror of
https://github.com/bitwarden/server
synced 2025-12-22 19:23:45 +00:00
event collection api endpoint
This commit is contained in:
65
src/Events/Controllers/CollectController.cs
Normal file
65
src/Events/Controllers/CollectController.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Repositories;
|
||||
using Bit.Core.Services;
|
||||
using Bit.Events.Models;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Bit.Events.Controllers
|
||||
{
|
||||
[Route("collect")]
|
||||
[Authorize("Application")]
|
||||
public class CollectController : Controller
|
||||
{
|
||||
private readonly CurrentContext _currentContext;
|
||||
private readonly IEventService _eventService;
|
||||
private readonly ICipherRepository _cipherRepository;
|
||||
|
||||
public CollectController(
|
||||
CurrentContext currentContext,
|
||||
IEventService eventService,
|
||||
ICipherRepository cipherRepository)
|
||||
{
|
||||
_currentContext = currentContext;
|
||||
_eventService = eventService;
|
||||
_cipherRepository = cipherRepository;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public Task<IActionResult> Get([FromQuery]EventModel model)
|
||||
{
|
||||
return Post(model);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Post([FromBody]EventModel model)
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user