1
0
mirror of https://github.com/bitwarden/server.git synced 2025-05-29 23:34:53 -05:00

new client event types, pass date to event funcs

This commit is contained in:
Kyle Spearrin 2019-07-09 11:44:09 -04:00
parent f7be870bfe
commit da5c385d4a
6 changed files with 37 additions and 31 deletions

View File

@ -21,9 +21,11 @@
Cipher_ClientViewed = 1107, Cipher_ClientViewed = 1107,
Cipher_ClientToggledPasswordVisible = 1108, Cipher_ClientToggledPasswordVisible = 1108,
Cipher_ClientToggledHiddenFieldVisible = 1109, Cipher_ClientToggledHiddenFieldVisible = 1109,
Cipher_ClientCopiedPassword = 1110, Cipher_ClientToggledCardCodeVisible = 1110,
Cipher_ClientCopedHiddenField = 1111, Cipher_ClientCopiedPassword = 1111,
Cipher_ClientAutofilled = 1112, Cipher_ClientCopiedHiddenField = 1112,
Cipher_ClientCopiedCardCode = 1113,
Cipher_ClientAutofilled = 1114,
Collection_Created = 1300, Collection_Created = 1300,
Collection_Updated = 1301, Collection_Updated = 1301,

View File

@ -7,11 +7,11 @@ namespace Bit.Core.Services
{ {
public interface IEventService public interface IEventService
{ {
Task LogUserEventAsync(Guid userId, EventType type); Task LogUserEventAsync(Guid userId, EventType type, DateTime? date = null);
Task LogCipherEventAsync(Cipher cipher, EventType type); Task LogCipherEventAsync(Cipher cipher, EventType type, DateTime? date = null);
Task LogCollectionEventAsync(Collection collection, EventType type); Task LogCollectionEventAsync(Collection collection, EventType type, DateTime? date = null);
Task LogGroupEventAsync(Group group, EventType type); Task LogGroupEventAsync(Group group, EventType type, DateTime? date = null);
Task LogOrganizationUserEventAsync(OrganizationUser organizationUser, EventType type); Task LogOrganizationUserEventAsync(OrganizationUser organizationUser, EventType type, DateTime? date = null);
Task LogOrganizationEventAsync(Organization organization, EventType type); Task LogOrganizationEventAsync(Organization organization, EventType type, DateTime? date = null);
} }
} }

View File

@ -31,9 +31,8 @@ namespace Bit.Core.Services
_globalSettings = globalSettings; _globalSettings = globalSettings;
} }
public async Task LogUserEventAsync(Guid userId, EventType type) public async Task LogUserEventAsync(Guid userId, EventType type, DateTime? date = null)
{ {
var now = DateTime.UtcNow;
var events = new List<IEvent> var events = new List<IEvent>
{ {
new EventMessage(_currentContext) new EventMessage(_currentContext)
@ -41,7 +40,7 @@ namespace Bit.Core.Services
UserId = userId, UserId = userId,
ActingUserId = userId, ActingUserId = userId,
Type = type, Type = type,
Date = now Date = date.GetValueOrDefault(DateTime.UtcNow)
} }
}; };
@ -68,7 +67,7 @@ namespace Bit.Core.Services
} }
} }
public async Task LogCipherEventAsync(Cipher cipher, EventType type) public async Task LogCipherEventAsync(Cipher cipher, EventType type, DateTime? date = null)
{ {
// Only logging organization cipher events for now. // Only logging organization cipher events for now.
if(!cipher.OrganizationId.HasValue || (!_currentContext?.UserId.HasValue ?? true)) if(!cipher.OrganizationId.HasValue || (!_currentContext?.UserId.HasValue ?? true))
@ -92,12 +91,12 @@ namespace Bit.Core.Services
CipherId = cipher.Id, CipherId = cipher.Id,
Type = type, Type = type,
ActingUserId = _currentContext?.UserId, ActingUserId = _currentContext?.UserId,
Date = DateTime.UtcNow Date = date.GetValueOrDefault(DateTime.UtcNow)
}; };
await _eventWriteService.CreateAsync(e); await _eventWriteService.CreateAsync(e);
} }
public async Task LogCollectionEventAsync(Collection collection, EventType type) public async Task LogCollectionEventAsync(Collection collection, EventType type, DateTime? date = null)
{ {
var orgAbilities = await _applicationCacheService.GetOrganizationAbilitiesAsync(); var orgAbilities = await _applicationCacheService.GetOrganizationAbilitiesAsync();
if(!CanUseEvents(orgAbilities, collection.OrganizationId)) if(!CanUseEvents(orgAbilities, collection.OrganizationId))
@ -111,12 +110,12 @@ namespace Bit.Core.Services
CollectionId = collection.Id, CollectionId = collection.Id,
Type = type, Type = type,
ActingUserId = _currentContext?.UserId, ActingUserId = _currentContext?.UserId,
Date = DateTime.UtcNow Date = date.GetValueOrDefault(DateTime.UtcNow)
}; };
await _eventWriteService.CreateAsync(e); await _eventWriteService.CreateAsync(e);
} }
public async Task LogGroupEventAsync(Group group, EventType type) public async Task LogGroupEventAsync(Group group, EventType type, DateTime? date = null)
{ {
var orgAbilities = await _applicationCacheService.GetOrganizationAbilitiesAsync(); var orgAbilities = await _applicationCacheService.GetOrganizationAbilitiesAsync();
if(!CanUseEvents(orgAbilities, group.OrganizationId)) if(!CanUseEvents(orgAbilities, group.OrganizationId))
@ -130,12 +129,13 @@ namespace Bit.Core.Services
GroupId = group.Id, GroupId = group.Id,
Type = type, Type = type,
ActingUserId = _currentContext?.UserId, ActingUserId = _currentContext?.UserId,
Date = DateTime.UtcNow Date = date.GetValueOrDefault(DateTime.UtcNow)
}; };
await _eventWriteService.CreateAsync(e); await _eventWriteService.CreateAsync(e);
} }
public async Task LogOrganizationUserEventAsync(OrganizationUser organizationUser, EventType type) public async Task LogOrganizationUserEventAsync(OrganizationUser organizationUser, EventType type,
DateTime? date = null)
{ {
var orgAbilities = await _applicationCacheService.GetOrganizationAbilitiesAsync(); var orgAbilities = await _applicationCacheService.GetOrganizationAbilitiesAsync();
if(!CanUseEvents(orgAbilities, organizationUser.OrganizationId)) if(!CanUseEvents(orgAbilities, organizationUser.OrganizationId))
@ -150,12 +150,12 @@ namespace Bit.Core.Services
OrganizationUserId = organizationUser.Id, OrganizationUserId = organizationUser.Id,
Type = type, Type = type,
ActingUserId = _currentContext?.UserId, ActingUserId = _currentContext?.UserId,
Date = DateTime.UtcNow Date = date.GetValueOrDefault(DateTime.UtcNow)
}; };
await _eventWriteService.CreateAsync(e); await _eventWriteService.CreateAsync(e);
} }
public async Task LogOrganizationEventAsync(Organization organization, EventType type) public async Task LogOrganizationEventAsync(Organization organization, EventType type, DateTime? date = null)
{ {
if(!organization.Enabled || !organization.UseEvents) if(!organization.Enabled || !organization.UseEvents)
{ {
@ -167,7 +167,7 @@ namespace Bit.Core.Services
OrganizationId = organization.Id, OrganizationId = organization.Id,
Type = type, Type = type,
ActingUserId = _currentContext?.UserId, ActingUserId = _currentContext?.UserId,
Date = DateTime.UtcNow Date = date.GetValueOrDefault(DateTime.UtcNow)
}; };
await _eventWriteService.CreateAsync(e); await _eventWriteService.CreateAsync(e);
} }

View File

@ -7,32 +7,33 @@ namespace Bit.Core.Services
{ {
public class NoopEventService : IEventService public class NoopEventService : IEventService
{ {
public Task LogCipherEventAsync(Cipher cipher, EventType type) public Task LogCipherEventAsync(Cipher cipher, EventType type, DateTime? date = null)
{ {
return Task.FromResult(0); return Task.FromResult(0);
} }
public Task LogCollectionEventAsync(Collection collection, EventType type) public Task LogCollectionEventAsync(Collection collection, EventType type, DateTime? date = null)
{ {
return Task.FromResult(0); return Task.FromResult(0);
} }
public Task LogGroupEventAsync(Group group, EventType type) public Task LogGroupEventAsync(Group group, EventType type, DateTime? date = null)
{ {
return Task.FromResult(0); return Task.FromResult(0);
} }
public Task LogOrganizationEventAsync(Organization organization, EventType type) public Task LogOrganizationEventAsync(Organization organization, EventType type, DateTime? date = null)
{ {
return Task.FromResult(0); return Task.FromResult(0);
} }
public Task LogOrganizationUserEventAsync(OrganizationUser organizationUser, EventType type) public Task LogOrganizationUserEventAsync(OrganizationUser organizationUser, EventType type,
DateTime? date = null)
{ {
return Task.FromResult(0); return Task.FromResult(0);
} }
public Task LogUserEventAsync(Guid userId, EventType type) public Task LogUserEventAsync(Guid userId, EventType type, DateTime? date = null)
{ {
return Task.FromResult(0); return Task.FromResult(0);
} }

View File

@ -49,12 +49,14 @@ namespace Bit.Events.Controllers
{ {
// User events // User events
case EventType.User_ClientExportedVault: case EventType.User_ClientExportedVault:
await _eventService.LogUserEventAsync(_currentContext.UserId.Value, model.Type); await _eventService.LogUserEventAsync(_currentContext.UserId.Value, model.Type, model.Date);
break; break;
// Cipher events // Cipher events
case EventType.Cipher_ClientAutofilled: case EventType.Cipher_ClientAutofilled:
case EventType.Cipher_ClientCopedHiddenField: case EventType.Cipher_ClientCopiedHiddenField:
case EventType.Cipher_ClientCopiedPassword: case EventType.Cipher_ClientCopiedPassword:
case EventType.Cipher_ClientCopiedCardCode:
case EventType.Cipher_ClientToggledCardCodeVisible:
case EventType.Cipher_ClientToggledHiddenFieldVisible: case EventType.Cipher_ClientToggledHiddenFieldVisible:
case EventType.Cipher_ClientToggledPasswordVisible: case EventType.Cipher_ClientToggledPasswordVisible:
case EventType.Cipher_ClientViewed: case EventType.Cipher_ClientViewed:
@ -68,7 +70,7 @@ namespace Bit.Events.Controllers
{ {
return false; return false;
} }
await _eventService.LogCipherEventAsync(cipher, model.Type); await _eventService.LogCipherEventAsync(cipher, model.Type, model.Date);
break; break;
default: default:
return false; return false;

View File

@ -7,5 +7,6 @@ namespace Bit.Events.Models
{ {
public EventType Type { get; set; } public EventType Type { get; set; }
public Guid? CipherId { get; set; } public Guid? CipherId { get; set; }
public DateTime Date { get; set; }
} }
} }