1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-23 05:55:13 -05:00

event write services

This commit is contained in:
Kyle Spearrin 2017-12-04 09:58:07 -05:00
parent bcd8a06994
commit 8a88a36140
5 changed files with 101 additions and 10 deletions

View File

@ -0,0 +1,12 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage.Table;
namespace Bit.Core.Services
{
public interface IEventWriteService
{
Task CreateAsync(ITableEntity entity);
Task CreateManyAsync(IList<ITableEntity> entities);
}
}

View File

@ -0,0 +1,46 @@
using System.Threading.Tasks;
using Bit.Core.Repositories;
using System.Collections.Generic;
using Microsoft.WindowsAzure.Storage.Table;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue;
using Newtonsoft.Json;
namespace Bit.Core.Services
{
public class AzureQueueEventWriteService : IEventWriteService
{
private readonly CloudQueue _queue;
private readonly GlobalSettings _globalSettings;
private JsonSerializerSettings _jsonSettings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
};
public AzureQueueEventWriteService(
IEventRepository eventRepository,
GlobalSettings globalSettings)
{
var storageAccount = CloudStorageAccount.Parse(globalSettings.Storage.ConnectionString);
var queueClient = storageAccount.CreateCloudQueueClient();
_queue = queueClient.GetQueueReference("event");
_globalSettings = globalSettings;
}
public async Task CreateAsync(ITableEntity entity)
{
var json = JsonConvert.SerializeObject(entity, _jsonSettings);
var message = new CloudQueueMessage(json);
await _queue.AddMessageAsync(message);
}
public async Task CreateManyAsync(IList<ITableEntity> entities)
{
var json = JsonConvert.SerializeObject(entities, _jsonSettings);
var message = new CloudQueueMessage(json);
await _queue.AddMessageAsync(message);
}
}
}

View File

@ -12,18 +12,18 @@ namespace Bit.Core.Services
{ {
public class EventService : IEventService public class EventService : IEventService
{ {
private readonly IEventRepository _eventRepository; private readonly IEventWriteService _eventWriteService;
private readonly IOrganizationUserRepository _organizationUserRepository; private readonly IOrganizationUserRepository _organizationUserRepository;
private readonly CurrentContext _currentContext; private readonly CurrentContext _currentContext;
private readonly GlobalSettings _globalSettings; private readonly GlobalSettings _globalSettings;
public EventService( public EventService(
IEventRepository eventRepository, IEventWriteService eventWriteService,
IOrganizationUserRepository organizationUserRepository, IOrganizationUserRepository organizationUserRepository,
CurrentContext currentContext, CurrentContext currentContext,
GlobalSettings globalSettings) GlobalSettings globalSettings)
{ {
_eventRepository = eventRepository; _eventWriteService = eventWriteService;
_organizationUserRepository = organizationUserRepository; _organizationUserRepository = organizationUserRepository;
_currentContext = currentContext; _currentContext = currentContext;
_globalSettings = globalSettings; _globalSettings = globalSettings;
@ -48,11 +48,11 @@ namespace Bit.Core.Services
if(orgEvents.Any()) if(orgEvents.Any())
{ {
events.AddRange(orgEvents); events.AddRange(orgEvents);
await _eventRepository.CreateManyAsync(events); await _eventWriteService.CreateManyAsync(events);
} }
else else
{ {
await _eventRepository.CreateAsync(events.First()); await _eventWriteService.CreateAsync(events.First());
} }
} }
@ -64,31 +64,31 @@ namespace Bit.Core.Services
} }
var e = new CipherEvent(cipher, _currentContext?.UserId, type); var e = new CipherEvent(cipher, _currentContext?.UserId, type);
await _eventRepository.CreateAsync(e); await _eventWriteService.CreateAsync(e);
} }
public async Task LogCollectionEventAsync(Collection collection, EventType type) public async Task LogCollectionEventAsync(Collection collection, EventType type)
{ {
var e = new CollectionEvent(collection, _currentContext.UserId.Value, type); var e = new CollectionEvent(collection, _currentContext.UserId.Value, type);
await _eventRepository.CreateAsync(e); await _eventWriteService.CreateAsync(e);
} }
public async Task LogGroupEventAsync(Group group, EventType type) public async Task LogGroupEventAsync(Group group, EventType type)
{ {
var e = new GroupEvent(group, _currentContext.UserId.Value, type); var e = new GroupEvent(group, _currentContext.UserId.Value, type);
await _eventRepository.CreateAsync(e); await _eventWriteService.CreateAsync(e);
} }
public async Task LogOrganizationUserEventAsync(OrganizationUser organizationUser, EventType type) public async Task LogOrganizationUserEventAsync(OrganizationUser organizationUser, EventType type)
{ {
var e = new OrganizationUserEvent(organizationUser, _currentContext.UserId.Value, type); var e = new OrganizationUserEvent(organizationUser, _currentContext.UserId.Value, type);
await _eventRepository.CreateAsync(e); await _eventWriteService.CreateAsync(e);
} }
public async Task LogOrganizationEventAsync(Organization organization, EventType type) public async Task LogOrganizationEventAsync(Organization organization, EventType type)
{ {
var e = new OrganizationEvent(organization, _currentContext.UserId.Value, type); var e = new OrganizationEvent(organization, _currentContext.UserId.Value, type);
await _eventRepository.CreateAsync(e); await _eventWriteService.CreateAsync(e);
} }
} }
} }

View File

@ -0,0 +1,31 @@
using System.Threading.Tasks;
using Bit.Core.Repositories;
using System.Collections.Generic;
using Microsoft.WindowsAzure.Storage.Table;
namespace Bit.Core.Services
{
public class RepositoryEventWriteService : IEventWriteService
{
private readonly IEventRepository _eventRepository;
private readonly GlobalSettings _globalSettings;
public RepositoryEventWriteService(
IEventRepository eventRepository,
GlobalSettings globalSettings)
{
_eventRepository = eventRepository;
_globalSettings = globalSettings;
}
public async Task CreateAsync(ITableEntity entity)
{
await _eventRepository.CreateAsync(entity);
}
public async Task CreateManyAsync(IList<ITableEntity> entities)
{
await _eventRepository.CreateManyAsync(entities);
}
}
}

View File

@ -105,10 +105,12 @@ namespace Bit.Core.Utilities
if(!globalSettings.SelfHosted && CoreHelpers.SettingHasValue(globalSettings.Storage.ConnectionString)) if(!globalSettings.SelfHosted && CoreHelpers.SettingHasValue(globalSettings.Storage.ConnectionString))
{ {
services.AddSingleton<IBlockIpService, AzureQueueBlockIpService>(); services.AddSingleton<IBlockIpService, AzureQueueBlockIpService>();
services.AddSingleton<IEventWriteService, AzureQueueEventWriteService>();
} }
else else
{ {
services.AddSingleton<IBlockIpService, NoopBlockIpService>(); services.AddSingleton<IBlockIpService, NoopBlockIpService>();
services.AddSingleton<IEventWriteService, RepositoryEventWriteService>();
} }
if(CoreHelpers.SettingHasValue(globalSettings.Attachment.ConnectionString)) if(CoreHelpers.SettingHasValue(globalSettings.Attachment.ConnectionString))