1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-04 09:32:48 -05:00

log events to various organization indexes as well

This commit is contained in:
Kyle Spearrin
2017-12-01 12:14:46 -05:00
parent d94c2a8f50
commit 0662fc2163
8 changed files with 75 additions and 22 deletions

View File

@ -7,5 +7,6 @@ namespace Bit.Core.Services
public interface IEventService
{
Task LogUserEventAsync(Guid userId, EventType type);
Task LogUserEventAsync(Guid userId, CurrentContext currentContext, EventType type);
}
}

View File

@ -3,26 +3,58 @@ using System;
using Bit.Core.Enums;
using Bit.Core.Repositories;
using Bit.Core.Models.Data;
using System.Linq;
using System.Collections.Generic;
using Microsoft.WindowsAzure.Storage.Table;
namespace Bit.Core.Services
{
public class EventService : IEventService
{
private readonly IEventRepository _eventRepository;
private readonly IOrganizationUserRepository _organizationUserRepository;
private readonly GlobalSettings _globalSettings;
public EventService(
IEventRepository eventRepository,
IOrganizationUserRepository organizationUserRepository,
GlobalSettings globalSettings)
{
_eventRepository = eventRepository;
_organizationUserRepository = organizationUserRepository;
_globalSettings = globalSettings;
}
public async Task LogUserEventAsync(Guid userId, EventType type)
{
var userEvent = new UserEvent(userId, type);
await _eventRepository.CreateAsync(userEvent);
var events = new List<ITableEntity> { new UserEvent(userId, type) };
var orgs = await _organizationUserRepository.GetManyByUserAsync(userId);
var orgEvents = orgs.Where(o => o.Status == OrganizationUserStatusType.Confirmed)
.Select(o => new UserEvent(userId, o.Id, type));
if(orgEvents.Any())
{
events.AddRange(orgEvents);
await _eventRepository.CreateManyAsync(events);
}
else
{
await _eventRepository.CreateAsync(events.First());
}
}
public async Task LogUserEventAsync(Guid userId, CurrentContext currentContext, EventType type)
{
var events = new List<ITableEntity> { new UserEvent(userId, type) };
var orgEvents = currentContext.Organizations.Select(o => new UserEvent(userId, o.Id, type));
if(orgEvents.Any())
{
events.AddRange(orgEvents);
await _eventRepository.CreateManyAsync(events);
}
else
{
await _eventRepository.CreateAsync(events.First());
}
}
}
}

View File

@ -425,7 +425,7 @@ namespace Bit.Core.Services
user.Key = key;
await _userRepository.ReplaceAsync(user);
await _eventService.LogUserEventAsync(user.Id, EventType.User_ChangedPassword);
await _eventService.LogUserEventAsync(user.Id, _currentContext, EventType.User_ChangedPassword);
return IdentityResult.Success;
}
@ -503,7 +503,7 @@ namespace Bit.Core.Services
user.TwoFactorRecoveryCode = CoreHelpers.SecureRandomString(32, upper: false, special: false);
}
await SaveUserAsync(user);
await _eventService.LogUserEventAsync(user.Id, EventType.User_Enabled2fa);
await _eventService.LogUserEventAsync(user.Id, _currentContext, EventType.User_Enabled2fa);
}
public async Task DisableTwoFactorProviderAsync(User user, TwoFactorProviderType type)
@ -517,7 +517,7 @@ namespace Bit.Core.Services
providers.Remove(type);
user.SetTwoFactorProviders(providers);
await SaveUserAsync(user);
await _eventService.LogUserEventAsync(user.Id, EventType.User_Disabled2fa);
await _eventService.LogUserEventAsync(user.Id, _currentContext, EventType.User_Disabled2fa);
}
public async Task<bool> RecoverTwoFactorAsync(string email, string masterPassword, string recoveryCode)
@ -542,7 +542,7 @@ namespace Bit.Core.Services
user.TwoFactorProviders = null;
user.TwoFactorRecoveryCode = CoreHelpers.SecureRandomString(32, upper: false, special: false);
await SaveUserAsync(user);
await _eventService.LogUserEventAsync(user.Id, EventType.User_Recovered2fa);
await _eventService.LogUserEventAsync(user.Id, _currentContext, EventType.User_Recovered2fa);
return true;
}

View File

@ -10,5 +10,10 @@ namespace Bit.Core.Services
{
return Task.FromResult(0);
}
public Task LogUserEventAsync(Guid userId, CurrentContext currentContext, EventType type)
{
return Task.FromResult(0);
}
}
}