mirror of
https://github.com/bitwarden/server.git
synced 2025-04-20 20:45:10 -05:00
organize event models. stub out event services
This commit is contained in:
parent
ba9cca057e
commit
f4586002c4
@ -58,7 +58,7 @@ namespace Bit.Api
|
|||||||
StripeConfiguration.SetApiKey(globalSettings.StripeApiKey);
|
StripeConfiguration.SetApiKey(globalSettings.StripeApiKey);
|
||||||
|
|
||||||
// Repositories
|
// Repositories
|
||||||
services.AddSqlServerRepositories();
|
services.AddSqlServerRepositories(globalSettings);
|
||||||
|
|
||||||
// Context
|
// Context
|
||||||
services.AddScoped<CurrentContext>();
|
services.AddScoped<CurrentContext>();
|
||||||
|
@ -36,7 +36,7 @@ namespace Bit.Billing
|
|||||||
StripeConfiguration.SetApiKey(globalSettings.StripeApiKey);
|
StripeConfiguration.SetApiKey(globalSettings.StripeApiKey);
|
||||||
|
|
||||||
// Repositories
|
// Repositories
|
||||||
services.AddSqlServerRepositories();
|
services.AddSqlServerRepositories(globalSettings);
|
||||||
|
|
||||||
// Context
|
// Context
|
||||||
services.AddScoped<CurrentContext>();
|
services.AddScoped<CurrentContext>();
|
||||||
|
29
src/Core/Models/Data/CipherEvent.cs
Normal file
29
src/Core/Models/Data/CipherEvent.cs
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
using Bit.Core.Enums;
|
||||||
|
using Bit.Core.Models.Table;
|
||||||
|
using Bit.Core.Utilities;
|
||||||
|
|
||||||
|
namespace Bit.Core.Models.Data
|
||||||
|
{
|
||||||
|
public class CipherEvent : EventTableEntity
|
||||||
|
{
|
||||||
|
public CipherEvent(Cipher cipher, EventType type)
|
||||||
|
{
|
||||||
|
if(cipher.OrganizationId.HasValue)
|
||||||
|
{
|
||||||
|
PartitionKey = $"OrganizationId={cipher.OrganizationId.Value}";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
PartitionKey = $"UserId={cipher.UserId.Value}";
|
||||||
|
}
|
||||||
|
|
||||||
|
RowKey = string.Format("Date={0}__CipherId={1}__Type={2}",
|
||||||
|
CoreHelpers.DateTimeToTableStorageKey(), cipher.Id, type);
|
||||||
|
|
||||||
|
OrganizationId = cipher.OrganizationId;
|
||||||
|
UserId = cipher.UserId;
|
||||||
|
CipherId = cipher.Id;
|
||||||
|
Type = type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
16
src/Core/Models/Data/EventTableEntity.cs
Normal file
16
src/Core/Models/Data/EventTableEntity.cs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Bit.Core.Enums;
|
||||||
|
using Microsoft.WindowsAzure.Storage.Table;
|
||||||
|
|
||||||
|
namespace Bit.Core.Models.Data
|
||||||
|
{
|
||||||
|
public class EventTableEntity : TableEntity
|
||||||
|
{
|
||||||
|
public EventType Type { get; set; }
|
||||||
|
public Guid? UserId { get; set; }
|
||||||
|
public Guid? OrganizationId { get; set; }
|
||||||
|
public Guid? CipherId { get; set; }
|
||||||
|
public ICollection<Guid> CipherIds { get; set; }
|
||||||
|
}
|
||||||
|
}
|
30
src/Core/Models/Data/OrganizationEvent.cs
Normal file
30
src/Core/Models/Data/OrganizationEvent.cs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
using System;
|
||||||
|
using Bit.Core.Enums;
|
||||||
|
using Bit.Core.Utilities;
|
||||||
|
|
||||||
|
namespace Bit.Core.Models.Data
|
||||||
|
{
|
||||||
|
public class OrganizationEvent : EventTableEntity
|
||||||
|
{
|
||||||
|
public OrganizationEvent(Guid organizationId, EventType type)
|
||||||
|
{
|
||||||
|
PartitionKey = $"OrganizationId={organizationId}";
|
||||||
|
RowKey = string.Format("Date={0}__Type={1}",
|
||||||
|
CoreHelpers.DateTimeToTableStorageKey(), type);
|
||||||
|
|
||||||
|
OrganizationId = organizationId;
|
||||||
|
Type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OrganizationEvent(Guid organizationId, Guid userId, EventType type)
|
||||||
|
{
|
||||||
|
PartitionKey = $"OrganizationId={organizationId}";
|
||||||
|
RowKey = string.Format("Date={0}__UserId={1}__Type={2}",
|
||||||
|
CoreHelpers.DateTimeToTableStorageKey(), userId, type);
|
||||||
|
|
||||||
|
OrganizationId = organizationId;
|
||||||
|
UserId = userId;
|
||||||
|
Type = type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
30
src/Core/Models/Data/UserEvent.cs
Normal file
30
src/Core/Models/Data/UserEvent.cs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
using System;
|
||||||
|
using Bit.Core.Enums;
|
||||||
|
using Bit.Core.Utilities;
|
||||||
|
|
||||||
|
namespace Bit.Core.Models.Data
|
||||||
|
{
|
||||||
|
public class UserEvent : EventTableEntity
|
||||||
|
{
|
||||||
|
public UserEvent(Guid userId, EventType type)
|
||||||
|
{
|
||||||
|
PartitionKey = $"UserId={userId}";
|
||||||
|
RowKey = string.Format("Date={0}__Type={1}",
|
||||||
|
CoreHelpers.DateTimeToTableStorageKey(), type);
|
||||||
|
|
||||||
|
UserId = userId;
|
||||||
|
Type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserEvent(Guid userId, Guid organizationId, EventType type)
|
||||||
|
{
|
||||||
|
PartitionKey = $"OrganizationId={organizationId}";
|
||||||
|
RowKey = string.Format("Date={0}__UserId={1}__Type={2}",
|
||||||
|
CoreHelpers.DateTimeToTableStorageKey(), userId, type);
|
||||||
|
|
||||||
|
OrganizationId = organizationId;
|
||||||
|
UserId = userId;
|
||||||
|
Type = type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
15
src/Core/Repositories/IEventRepository.cs
Normal file
15
src/Core/Repositories/IEventRepository.cs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Bit.Core.Models.Data;
|
||||||
|
using Microsoft.WindowsAzure.Storage.Table;
|
||||||
|
|
||||||
|
namespace Bit.Core.Repositories
|
||||||
|
{
|
||||||
|
public interface IEventRepository
|
||||||
|
{
|
||||||
|
Task<ICollection<EventTableEntity>> GetManyByUserAsync(Guid userId, DateTime startDate, DateTime endDate);
|
||||||
|
Task CreateAsync(ITableEntity entity);
|
||||||
|
Task CreateManyAsync(IEnumerable<ITableEntity> entities);
|
||||||
|
}
|
||||||
|
}
|
@ -2,15 +2,14 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Bit.Core.Enums;
|
using Bit.Core.Models.Data;
|
||||||
using Bit.Core.Models.Table;
|
|
||||||
using Bit.Core.Utilities;
|
using Bit.Core.Utilities;
|
||||||
using Microsoft.WindowsAzure.Storage;
|
using Microsoft.WindowsAzure.Storage;
|
||||||
using Microsoft.WindowsAzure.Storage.Table;
|
using Microsoft.WindowsAzure.Storage.Table;
|
||||||
|
|
||||||
namespace Bit.Core.Repositories.TableStorage
|
namespace Bit.Core.Repositories.TableStorage
|
||||||
{
|
{
|
||||||
public class EventRepository
|
public class EventRepository : IEventRepository
|
||||||
{
|
{
|
||||||
public EventRepository(GlobalSettings globalSettings)
|
public EventRepository(GlobalSettings globalSettings)
|
||||||
{
|
{
|
||||||
@ -21,7 +20,7 @@ namespace Bit.Core.Repositories.TableStorage
|
|||||||
|
|
||||||
protected CloudTable Table { get; set; }
|
protected CloudTable Table { get; set; }
|
||||||
|
|
||||||
public async Task<ICollection<EventTableEntiity>> GetManyByUserAsync(Guid userId,
|
public async Task<ICollection<EventTableEntity>> GetManyByUserAsync(Guid userId,
|
||||||
DateTime startDate, DateTime endDate)
|
DateTime startDate, DateTime endDate)
|
||||||
{
|
{
|
||||||
var start = CoreHelpers.DateTimeToTableStorageKey(startDate);
|
var start = CoreHelpers.DateTimeToTableStorageKey(startDate);
|
||||||
@ -37,8 +36,8 @@ namespace Bit.Core.Repositories.TableStorage
|
|||||||
TableOperators.And,
|
TableOperators.And,
|
||||||
rowFilter);
|
rowFilter);
|
||||||
|
|
||||||
var query = new TableQuery<EventTableEntiity>().Where(filter);
|
var query = new TableQuery<EventTableEntity>().Where(filter);
|
||||||
var results = new List<EventTableEntiity>();
|
var results = new List<EventTableEntity>();
|
||||||
TableContinuationToken continuationToken = null;
|
TableContinuationToken continuationToken = null;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
@ -82,84 +81,4 @@ namespace Bit.Core.Repositories.TableStorage
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class UserEvent : EventTableEntiity
|
|
||||||
{
|
|
||||||
public UserEvent(Guid userId, EventType type)
|
|
||||||
{
|
|
||||||
PartitionKey = $"UserId={userId}";
|
|
||||||
RowKey = string.Format("Date={0}__Type={1}",
|
|
||||||
CoreHelpers.DateTimeToTableStorageKey(), type);
|
|
||||||
|
|
||||||
UserId = userId;
|
|
||||||
Type = type;
|
|
||||||
}
|
|
||||||
|
|
||||||
public UserEvent(Guid userId, Guid organizationId, EventType type)
|
|
||||||
{
|
|
||||||
PartitionKey = $"OrganizationId={organizationId}";
|
|
||||||
RowKey = string.Format("Date={0}__UserId={1}__Type={2}",
|
|
||||||
CoreHelpers.DateTimeToTableStorageKey(), userId, type);
|
|
||||||
|
|
||||||
OrganizationId = organizationId;
|
|
||||||
UserId = userId;
|
|
||||||
Type = type;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class CipherEvent : EventTableEntiity
|
|
||||||
{
|
|
||||||
public CipherEvent(Cipher cipher, EventType type)
|
|
||||||
{
|
|
||||||
if(cipher.OrganizationId.HasValue)
|
|
||||||
{
|
|
||||||
PartitionKey = $"OrganizationId={cipher.OrganizationId.Value}";
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
PartitionKey = $"UserId={cipher.UserId.Value}";
|
|
||||||
}
|
|
||||||
|
|
||||||
RowKey = string.Format("Date={0}__CipherId={1}__Type={2}",
|
|
||||||
CoreHelpers.DateTimeToTableStorageKey(), cipher.Id, type);
|
|
||||||
|
|
||||||
OrganizationId = cipher.OrganizationId;
|
|
||||||
UserId = cipher.UserId;
|
|
||||||
CipherId = cipher.Id;
|
|
||||||
Type = type;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class OrganizationEvent : EventTableEntiity
|
|
||||||
{
|
|
||||||
public OrganizationEvent(Guid organizationId, EventType type)
|
|
||||||
{
|
|
||||||
PartitionKey = $"OrganizationId={organizationId}";
|
|
||||||
RowKey = string.Format("Date={0}__Type={1}",
|
|
||||||
CoreHelpers.DateTimeToTableStorageKey(), type);
|
|
||||||
|
|
||||||
OrganizationId = organizationId;
|
|
||||||
Type = type;
|
|
||||||
}
|
|
||||||
|
|
||||||
public OrganizationEvent(Guid organizationId, Guid userId, EventType type)
|
|
||||||
{
|
|
||||||
PartitionKey = $"OrganizationId={organizationId}";
|
|
||||||
RowKey = string.Format("Date={0}__UserId={1}__Type={2}",
|
|
||||||
CoreHelpers.DateTimeToTableStorageKey(), userId, type);
|
|
||||||
|
|
||||||
OrganizationId = organizationId;
|
|
||||||
UserId = userId;
|
|
||||||
Type = type;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class EventTableEntiity : TableEntity
|
|
||||||
{
|
|
||||||
public EventType Type { get; set; }
|
|
||||||
public Guid? UserId { get; set; }
|
|
||||||
public Guid? OrganizationId { get; set; }
|
|
||||||
public Guid? CipherId { get; set; }
|
|
||||||
public ICollection<Guid> CipherIds { get; set; }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
11
src/Core/Services/IEventService.cs
Normal file
11
src/Core/Services/IEventService.cs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Bit.Core.Enums;
|
||||||
|
|
||||||
|
namespace Bit.Core.Services
|
||||||
|
{
|
||||||
|
public interface IEventService
|
||||||
|
{
|
||||||
|
Task LogUserEventAsync(Guid userId, EventType type);
|
||||||
|
}
|
||||||
|
}
|
28
src/Core/Services/Implementations/EventService.cs
Normal file
28
src/Core/Services/Implementations/EventService.cs
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using System;
|
||||||
|
using Bit.Core.Enums;
|
||||||
|
using Bit.Core.Repositories;
|
||||||
|
using Bit.Core.Models.Data;
|
||||||
|
|
||||||
|
namespace Bit.Core.Services
|
||||||
|
{
|
||||||
|
public class EventService : IEventService
|
||||||
|
{
|
||||||
|
private readonly IEventRepository _eventRepository;
|
||||||
|
private readonly GlobalSettings _globalSettings;
|
||||||
|
|
||||||
|
public EventService(
|
||||||
|
IEventRepository eventRepository,
|
||||||
|
GlobalSettings globalSettings)
|
||||||
|
{
|
||||||
|
_eventRepository = eventRepository;
|
||||||
|
_globalSettings = globalSettings;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task LogUserEventAsync(Guid userId, EventType type)
|
||||||
|
{
|
||||||
|
var userEvent = new UserEvent(userId, type);
|
||||||
|
await _eventRepository.CreateAsync(userEvent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
14
src/Core/Services/NoopImplementations/NoopEventService.cs
Normal file
14
src/Core/Services/NoopImplementations/NoopEventService.cs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Bit.Core.Enums;
|
||||||
|
|
||||||
|
namespace Bit.Core.Services
|
||||||
|
{
|
||||||
|
public class NoopEventService : IEventService
|
||||||
|
{
|
||||||
|
public Task LogUserEventAsync(Guid userId, EventType type)
|
||||||
|
{
|
||||||
|
return Task.FromResult(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -20,13 +20,14 @@ using Microsoft.WindowsAzure.Storage;
|
|||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using SqlServerRepos = Bit.Core.Repositories.SqlServer;
|
using SqlServerRepos = Bit.Core.Repositories.SqlServer;
|
||||||
|
using TableStorageRepos = Bit.Core.Repositories.TableStorage;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Bit.Core.Utilities
|
namespace Bit.Core.Utilities
|
||||||
{
|
{
|
||||||
public static class ServiceCollectionExtensions
|
public static class ServiceCollectionExtensions
|
||||||
{
|
{
|
||||||
public static void AddSqlServerRepositories(this IServiceCollection services)
|
public static void AddSqlServerRepositories(this IServiceCollection services, GlobalSettings globalSettings)
|
||||||
{
|
{
|
||||||
services.AddSingleton<IUserRepository, SqlServerRepos.UserRepository>();
|
services.AddSingleton<IUserRepository, SqlServerRepos.UserRepository>();
|
||||||
services.AddSingleton<ICipherRepository, SqlServerRepos.CipherRepository>();
|
services.AddSingleton<ICipherRepository, SqlServerRepos.CipherRepository>();
|
||||||
@ -40,6 +41,15 @@ namespace Bit.Core.Utilities
|
|||||||
services.AddSingleton<IGroupRepository, SqlServerRepos.GroupRepository>();
|
services.AddSingleton<IGroupRepository, SqlServerRepos.GroupRepository>();
|
||||||
services.AddSingleton<IU2fRepository, SqlServerRepos.U2fRepository>();
|
services.AddSingleton<IU2fRepository, SqlServerRepos.U2fRepository>();
|
||||||
services.AddSingleton<IInstallationRepository, SqlServerRepos.InstallationRepository>();
|
services.AddSingleton<IInstallationRepository, SqlServerRepos.InstallationRepository>();
|
||||||
|
|
||||||
|
if(globalSettings.SelfHosted)
|
||||||
|
{
|
||||||
|
// TODO: Sql server repo
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
services.AddSingleton<IEventRepository, TableStorageRepos.EventRepository>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void AddBaseServices(this IServiceCollection services)
|
public static void AddBaseServices(this IServiceCollection services)
|
||||||
@ -50,6 +60,7 @@ namespace Bit.Core.Utilities
|
|||||||
services.AddSingleton<IOrganizationService, OrganizationService>();
|
services.AddSingleton<IOrganizationService, OrganizationService>();
|
||||||
services.AddSingleton<ICollectionService, CollectionService>();
|
services.AddSingleton<ICollectionService, CollectionService>();
|
||||||
services.AddSingleton<IGroupService, GroupService>();
|
services.AddSingleton<IGroupService, GroupService>();
|
||||||
|
services.AddSingleton<Services.IEventService, EventService>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void AddDefaultServices(this IServiceCollection services, GlobalSettings globalSettings)
|
public static void AddDefaultServices(this IServiceCollection services, GlobalSettings globalSettings)
|
||||||
|
@ -39,7 +39,7 @@ namespace Bit.Identity
|
|||||||
services.AddCustomDataProtectionServices(Environment, globalSettings);
|
services.AddCustomDataProtectionServices(Environment, globalSettings);
|
||||||
|
|
||||||
// Repositories
|
// Repositories
|
||||||
services.AddSqlServerRepositories();
|
services.AddSqlServerRepositories(globalSettings);
|
||||||
|
|
||||||
// Context
|
// Context
|
||||||
services.AddScoped<CurrentContext>();
|
services.AddScoped<CurrentContext>();
|
||||||
|
@ -45,7 +45,7 @@ namespace Bit.Jobs
|
|||||||
services.AddCustomDataProtectionServices(Environment, globalSettings);
|
services.AddCustomDataProtectionServices(Environment, globalSettings);
|
||||||
|
|
||||||
// Repositories
|
// Repositories
|
||||||
services.AddSqlServerRepositories();
|
services.AddSqlServerRepositories(globalSettings);
|
||||||
|
|
||||||
// Context
|
// Context
|
||||||
services.AddScoped<CurrentContext>();
|
services.AddScoped<CurrentContext>();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user