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

152 lines
5.0 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)
{
2017-12-08 23:09:50 -05:00
var now = DateTime.UtcNow;
var events = new List<IEvent>
{
2017-12-12 14:22:22 -05:00
new EventMessage
2017-12-08 23:09:50 -05:00
{
UserId = userId,
Type = type,
Date = now
}
};
2017-12-01 14:06:16 -05:00
2017-12-08 23:09:50 -05:00
IEnumerable<IEvent> orgEvents;
2017-12-01 14:06:16 -05:00
if(_currentContext.UserId.HasValue)
{
2017-12-12 14:22:22 -05:00
orgEvents = _currentContext.Organizations.Select(o => new EventMessage
2017-12-08 23:09:50 -05:00
{
OrganizationId = o.Id,
UserId = userId,
Type = type,
Date = DateTime.UtcNow
});
}
else
{
2017-12-01 14:06:16 -05:00
var orgs = await _organizationUserRepository.GetManyByUserAsync(userId);
orgEvents = orgs.Where(o => o.Status == OrganizationUserStatusType.Confirmed)
2017-12-12 14:22:22 -05:00
.Select(o => new EventMessage
2017-12-08 23:09:50 -05:00
{
2017-12-14 15:04:20 -05:00
OrganizationId = o.OrganizationId,
2017-12-08 23:09:50 -05:00
UserId = userId,
Type = type,
Date = DateTime.UtcNow
});
}
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-12 15:04:14 -05:00
// Only logging organization cipher events for now.
if(!cipher.OrganizationId.HasValue || (!_currentContext?.UserId.HasValue ?? true))
2017-12-01 14:06:16 -05:00
{
return;
}
2017-12-12 14:22:22 -05:00
var e = new EventMessage
2017-12-08 23:09:50 -05:00
{
OrganizationId = cipher.OrganizationId,
UserId = cipher.OrganizationId.HasValue ? null : cipher.UserId,
CipherId = cipher.Id,
Type = type,
ActingUserId = _currentContext?.UserId,
Date = DateTime.UtcNow
};
2017-12-04 09:58:07 -05:00
await _eventWriteService.CreateAsync(e);
}
public async Task LogCollectionEventAsync(Collection collection, EventType type)
{
2017-12-12 14:22:22 -05:00
var e = new EventMessage
2017-12-08 23:09:50 -05:00
{
OrganizationId = collection.OrganizationId,
CollectionId = collection.Id,
Type = type,
2017-12-12 15:23:45 -05:00
ActingUserId = _currentContext?.UserId,
2017-12-08 23:09:50 -05:00
Date = DateTime.UtcNow
};
2017-12-04 09:58:07 -05:00
await _eventWriteService.CreateAsync(e);
}
public async Task LogGroupEventAsync(Group group, EventType type)
{
2017-12-12 14:22:22 -05:00
var e = new EventMessage
2017-12-08 23:09:50 -05:00
{
OrganizationId = group.OrganizationId,
GroupId = group.Id,
Type = type,
2017-12-12 15:23:45 -05:00
ActingUserId = _currentContext?.UserId,
2017-12-08 23:09:50 -05:00
Date = DateTime.UtcNow
};
2017-12-04 09:58:07 -05:00
await _eventWriteService.CreateAsync(e);
}
public async Task LogOrganizationUserEventAsync(OrganizationUser organizationUser, EventType type)
{
2017-12-12 14:22:22 -05:00
var e = new EventMessage
2017-12-08 23:09:50 -05:00
{
OrganizationId = organizationUser.OrganizationId,
UserId = organizationUser.UserId,
OrganizationUserId = organizationUser.Id,
Type = type,
2017-12-12 15:23:45 -05:00
ActingUserId = _currentContext?.UserId,
2017-12-08 23:09:50 -05:00
Date = DateTime.UtcNow
};
2017-12-04 09:58:07 -05:00
await _eventWriteService.CreateAsync(e);
}
public async Task LogOrganizationEventAsync(Organization organization, EventType type)
{
2017-12-12 14:22:22 -05:00
var e = new EventMessage
2017-12-08 23:09:50 -05:00
{
OrganizationId = organization.Id,
Type = type,
2017-12-12 15:23:45 -05:00
ActingUserId = _currentContext?.UserId,
2017-12-08 23:09:50 -05:00
Date = DateTime.UtcNow
};
2017-12-04 09:58:07 -05:00
await _eventWriteService.CreateAsync(e);
2017-12-01 14:06:16 -05:00
}
}
}