Files
server/src/Core/Services/Implementations/EventService.cs

94 lines
3.3 KiB
C#
Raw Normal View History

using System.Threading.Tasks;
using System;
using Bit.Core.Enums;
using Bit.Core.Repositories;
using Bit.Core.Models.Data;
using System.Linq;
using System.Collections.Generic;
2017-12-01 14:06:16 -05:00
using Bit.Core.Models.Table;
namespace Bit.Core.Services
{
public class EventService : IEventService
{
2017-12-04 09:58:07 -05:00
private readonly IEventWriteService _eventWriteService;
private readonly IOrganizationUserRepository _organizationUserRepository;
2017-12-01 14:06:16 -05:00
private readonly CurrentContext _currentContext;
private readonly GlobalSettings _globalSettings;
public EventService(
2017-12-04 09:58:07 -05:00
IEventWriteService eventWriteService,
IOrganizationUserRepository organizationUserRepository,
2017-12-01 14:06:16 -05:00
CurrentContext currentContext,
GlobalSettings globalSettings)
{
2017-12-04 09:58:07 -05:00
_eventWriteService = eventWriteService;
_organizationUserRepository = organizationUserRepository;
2017-12-01 14:06:16 -05:00
_currentContext = currentContext;
_globalSettings = globalSettings;
}
public async Task LogUserEventAsync(Guid userId, EventType type)
{
var events = new List<EventTableEntity> { new UserEvent(userId, type) };
2017-12-01 14:06:16 -05:00
IEnumerable<UserEvent> orgEvents;
if(_currentContext.UserId.HasValue)
{
2017-12-01 14:06:16 -05:00
orgEvents = _currentContext.Organizations.Select(o => new UserEvent(userId, o.Id, type));
}
else
{
2017-12-01 14:06:16 -05:00
var orgs = await _organizationUserRepository.GetManyByUserAsync(userId);
orgEvents = orgs.Where(o => o.Status == OrganizationUserStatusType.Confirmed)
.Select(o => new UserEvent(userId, o.Id, type));
}
if(orgEvents.Any())
{
events.AddRange(orgEvents);
2017-12-04 09:58:07 -05:00
await _eventWriteService.CreateManyAsync(events);
}
else
{
2017-12-04 09:58:07 -05:00
await _eventWriteService.CreateAsync(events.First());
}
}
2017-12-01 14:06:16 -05:00
public async Task LogCipherEventAsync(Cipher cipher, EventType type)
{
2017-12-08 14:59:21 -05:00
if(!cipher.OrganizationId.HasValue && (!_currentContext?.UserId.HasValue ?? true))
2017-12-01 14:06:16 -05:00
{
return;
}
var e = new CipherEvent(cipher, _currentContext?.UserId, type);
2017-12-04 09:58:07 -05:00
await _eventWriteService.CreateAsync(e);
}
public async Task LogCollectionEventAsync(Collection collection, EventType type)
{
var e = new CollectionEvent(collection, _currentContext.UserId.Value, type);
2017-12-04 09:58:07 -05:00
await _eventWriteService.CreateAsync(e);
}
public async Task LogGroupEventAsync(Group group, EventType type)
{
var e = new GroupEvent(group, _currentContext.UserId.Value, type);
2017-12-04 09:58:07 -05:00
await _eventWriteService.CreateAsync(e);
}
public async Task LogOrganizationUserEventAsync(OrganizationUser organizationUser, EventType type)
{
var e = new OrganizationUserEvent(organizationUser, _currentContext.UserId.Value, type);
2017-12-04 09:58:07 -05:00
await _eventWriteService.CreateAsync(e);
}
public async Task LogOrganizationEventAsync(Organization organization, EventType type)
{
var e = new OrganizationEvent(organization, _currentContext.UserId.Value, type);
2017-12-04 09:58:07 -05:00
await _eventWriteService.CreateAsync(e);
2017-12-01 14:06:16 -05:00
}
}
}