mirror of
https://github.com/bitwarden/server.git
synced 2025-06-30 07:36:14 -05:00
process messages with IEvent
This commit is contained in:
@ -1,34 +0,0 @@
|
||||
using System;
|
||||
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, Guid? actingUserId, EventType type)
|
||||
{
|
||||
OrganizationId = cipher.OrganizationId;
|
||||
UserId = cipher.UserId;
|
||||
CipherId = cipher.Id;
|
||||
Type = (int)type;
|
||||
ActingUserId = actingUserId;
|
||||
Date = DateTime.UtcNow;
|
||||
|
||||
if(OrganizationId.HasValue)
|
||||
{
|
||||
UserId = null;
|
||||
PartitionKey = $"OrganizationId={OrganizationId}";
|
||||
RowKey = string.Format("Date={0}__CipherId={1}__ActingUserId={2}__Type={3}",
|
||||
CoreHelpers.DateTimeToTableStorageKey(Date), CipherId, ActingUserId, Type);
|
||||
}
|
||||
else
|
||||
{
|
||||
PartitionKey = $"UserId={UserId}";
|
||||
RowKey = string.Format("Date={0}__CipherId={1}__Type={2}",
|
||||
CoreHelpers.DateTimeToTableStorageKey(Date), CipherId, Type);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
using System;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Models.Table;
|
||||
using Bit.Core.Utilities;
|
||||
|
||||
namespace Bit.Core.Models.Data
|
||||
{
|
||||
public class CollectionEvent : EventTableEntity
|
||||
{
|
||||
public CollectionEvent(Collection collection, Guid actingUserId, EventType type)
|
||||
{
|
||||
OrganizationId = collection.OrganizationId;
|
||||
CollectionId = collection.Id;
|
||||
Type = (int)type;
|
||||
ActingUserId = actingUserId;
|
||||
Date = DateTime.UtcNow;
|
||||
|
||||
PartitionKey = $"OrganizationId={OrganizationId}";
|
||||
RowKey = string.Format("Date={0}__ActingUserId={1}__Type={2}",
|
||||
CoreHelpers.DateTimeToTableStorageKey(Date), ActingUserId, Type);
|
||||
}
|
||||
}
|
||||
}
|
18
src/Core/Models/Data/Event.cs
Normal file
18
src/Core/Models/Data/Event.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using Bit.Core.Enums;
|
||||
|
||||
namespace Bit.Core.Models.Data
|
||||
{
|
||||
public class Event : IEvent
|
||||
{
|
||||
public DateTime Date { get; set; }
|
||||
public EventType Type { get; set; }
|
||||
public Guid? UserId { get; set; }
|
||||
public Guid? OrganizationId { get; set; }
|
||||
public Guid? CipherId { get; set; }
|
||||
public Guid? CollectionId { get; set; }
|
||||
public Guid? GroupId { get; set; }
|
||||
public Guid? OrganizationUserId { get; set; }
|
||||
public Guid? ActingUserId { get; set; }
|
||||
}
|
||||
}
|
@ -1,20 +1,125 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Utilities;
|
||||
using Microsoft.WindowsAzure.Storage;
|
||||
using Microsoft.WindowsAzure.Storage.Table;
|
||||
|
||||
namespace Bit.Core.Models.Data
|
||||
{
|
||||
public class EventTableEntity : TableEntity
|
||||
public class EventTableEntity : TableEntity, IEvent
|
||||
{
|
||||
public EventTableEntity() { }
|
||||
|
||||
public EventTableEntity(IEvent e)
|
||||
{
|
||||
Date = e.Date;
|
||||
Type = e.Type;
|
||||
UserId = e.UserId;
|
||||
OrganizationId = e.OrganizationId;
|
||||
CipherId = e.CipherId;
|
||||
CollectionId = e.CollectionId;
|
||||
GroupId = e.GroupId;
|
||||
OrganizationUserId = e.OrganizationUserId;
|
||||
ActingUserId = e.ActingUserId;
|
||||
|
||||
switch(e.Type)
|
||||
{
|
||||
case EventType.User_LoggedIn:
|
||||
case EventType.User_ChangedPassword:
|
||||
case EventType.User_Enabled2fa:
|
||||
case EventType.User_Disabled2fa:
|
||||
case EventType.User_Recovered2fa:
|
||||
case EventType.User_FailedLogIn:
|
||||
case EventType.User_FailedLogIn2fa:
|
||||
if(e.OrganizationId.HasValue)
|
||||
{
|
||||
PartitionKey = $"OrganizationId={OrganizationId}";
|
||||
RowKey = string.Format("Date={0}__UserId={1}__Type={2}",
|
||||
CoreHelpers.DateTimeToTableStorageKey(Date), UserId, Type);
|
||||
}
|
||||
else
|
||||
{
|
||||
PartitionKey = $"UserId={UserId}";
|
||||
RowKey = string.Format("Date={0}__Type={1}",
|
||||
CoreHelpers.DateTimeToTableStorageKey(Date), Type);
|
||||
}
|
||||
break;
|
||||
case EventType.Cipher_Created:
|
||||
case EventType.Cipher_Updated:
|
||||
case EventType.Cipher_Deleted:
|
||||
case EventType.Cipher_AttachmentCreated:
|
||||
case EventType.Cipher_AttachmentDeleted:
|
||||
case EventType.Cipher_Shared:
|
||||
case EventType.Cipher_UpdatedCollections:
|
||||
if(OrganizationId.HasValue)
|
||||
{
|
||||
PartitionKey = $"OrganizationId={OrganizationId}";
|
||||
RowKey = string.Format("Date={0}__CipherId={1}__ActingUserId={2}__Type={3}",
|
||||
CoreHelpers.DateTimeToTableStorageKey(Date), CipherId, ActingUserId, Type);
|
||||
}
|
||||
else
|
||||
{
|
||||
PartitionKey = $"UserId={UserId}";
|
||||
RowKey = string.Format("Date={0}__CipherId={1}__Type={2}",
|
||||
CoreHelpers.DateTimeToTableStorageKey(Date), CipherId, Type);
|
||||
}
|
||||
break;
|
||||
case EventType.Collection_Created:
|
||||
case EventType.Collection_Updated:
|
||||
case EventType.Collection_Deleted:
|
||||
PartitionKey = $"OrganizationId={OrganizationId}";
|
||||
RowKey = string.Format("Date={0}__ActingUserId={1}__Type={2}",
|
||||
CoreHelpers.DateTimeToTableStorageKey(Date), ActingUserId, Type);
|
||||
break;
|
||||
case EventType.Group_Created:
|
||||
case EventType.Group_Updated:
|
||||
case EventType.Group_Deleted:
|
||||
PartitionKey = $"OrganizationId={OrganizationId}";
|
||||
RowKey = string.Format("Date={0}__ActingUserId={1}__Type={2}",
|
||||
CoreHelpers.DateTimeToTableStorageKey(Date), ActingUserId, Type);
|
||||
break;
|
||||
case EventType.OrganizationUser_Invited:
|
||||
case EventType.OrganizationUser_Confirmed:
|
||||
case EventType.OrganizationUser_Updated:
|
||||
case EventType.OrganizationUser_Removed:
|
||||
case EventType.OrganizationUser_UpdatedGroups:
|
||||
PartitionKey = $"OrganizationId={OrganizationId}";
|
||||
RowKey = string.Format("Date={0}__ActingUserId={1}__Type={2}",
|
||||
CoreHelpers.DateTimeToTableStorageKey(Date), ActingUserId, Type);
|
||||
break;
|
||||
case EventType.Organization_Updated:
|
||||
PartitionKey = $"OrganizationId={OrganizationId}";
|
||||
RowKey = string.Format("Date={0}__ActingUserId={1}__Type={2}",
|
||||
CoreHelpers.DateTimeToTableStorageKey(Date), ActingUserId, Type);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public DateTime Date { get; set; }
|
||||
public int Type { get; set; }
|
||||
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; }
|
||||
public Guid? CollectionId { get; set; }
|
||||
public Guid? GroupId { get; set; }
|
||||
public Guid? OrganizationUserId { get; set; }
|
||||
public Guid? ActingUserId { get; set; }
|
||||
|
||||
public override IDictionary<string, EntityProperty> WriteEntity(OperationContext operationContext)
|
||||
{
|
||||
var result = base.WriteEntity(operationContext);
|
||||
if(result.ContainsKey(nameof(Type)))
|
||||
{
|
||||
result[nameof(Type)] = new EntityProperty((int)Type);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.Add(nameof(Type), new EntityProperty((int)Type));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,23 +0,0 @@
|
||||
using System;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Models.Table;
|
||||
using Bit.Core.Utilities;
|
||||
|
||||
namespace Bit.Core.Models.Data
|
||||
{
|
||||
public class GroupEvent : EventTableEntity
|
||||
{
|
||||
public GroupEvent(Group group, Guid actingUserId, EventType type)
|
||||
{
|
||||
OrganizationId = group.OrganizationId;
|
||||
GroupId = group.Id;
|
||||
Type = (int)type;
|
||||
ActingUserId = actingUserId;
|
||||
Date = DateTime.UtcNow;
|
||||
|
||||
PartitionKey = $"OrganizationId={OrganizationId}";
|
||||
RowKey = string.Format("Date={0}__ActingUserId={1}__Type={2}",
|
||||
CoreHelpers.DateTimeToTableStorageKey(Date), ActingUserId, Type);
|
||||
}
|
||||
}
|
||||
}
|
18
src/Core/Models/Data/IEvent.cs
Normal file
18
src/Core/Models/Data/IEvent.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using Bit.Core.Enums;
|
||||
|
||||
namespace Bit.Core.Models.Data
|
||||
{
|
||||
public interface IEvent
|
||||
{
|
||||
Guid? ActingUserId { get; set; }
|
||||
Guid? CipherId { get; set; }
|
||||
Guid? CollectionId { get; set; }
|
||||
DateTime Date { get; set; }
|
||||
Guid? GroupId { get; set; }
|
||||
Guid? OrganizationId { get; set; }
|
||||
Guid? OrganizationUserId { get; set; }
|
||||
EventType Type { get; set; }
|
||||
Guid? UserId { get; set; }
|
||||
}
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
using System;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Models.Table;
|
||||
using Bit.Core.Utilities;
|
||||
|
||||
namespace Bit.Core.Models.Data
|
||||
{
|
||||
public class OrganizationEvent : EventTableEntity
|
||||
{
|
||||
public OrganizationEvent(Organization organization, Guid actingUserId, EventType type)
|
||||
{
|
||||
OrganizationId = organization.Id;
|
||||
Type = (int)type;
|
||||
ActingUserId = actingUserId;
|
||||
Date = DateTime.UtcNow;
|
||||
|
||||
PartitionKey = $"OrganizationId={OrganizationId}";
|
||||
RowKey = string.Format("Date={0}__ActingUserId={1}__Type={2}",
|
||||
CoreHelpers.DateTimeToTableStorageKey(Date), ActingUserId, Type);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
using System;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Models.Table;
|
||||
using Bit.Core.Utilities;
|
||||
|
||||
namespace Bit.Core.Models.Data
|
||||
{
|
||||
public class OrganizationUserEvent : EventTableEntity
|
||||
{
|
||||
public OrganizationUserEvent(OrganizationUser organizationUser, Guid actingUserId, EventType type)
|
||||
{
|
||||
OrganizationId = organizationUser.OrganizationId;
|
||||
UserId = organizationUser.UserId;
|
||||
OrganizationUserId = organizationUser.Id;
|
||||
Type = (int)type;
|
||||
ActingUserId = actingUserId;
|
||||
Date = DateTime.UtcNow;
|
||||
|
||||
PartitionKey = $"OrganizationId={OrganizationId}";
|
||||
RowKey = string.Format("Date={0}__ActingUserId={1}__Type={2}",
|
||||
CoreHelpers.DateTimeToTableStorageKey(Date), ActingUserId, Type);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
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)
|
||||
{
|
||||
UserId = userId;
|
||||
Type = (int)type;
|
||||
Date = DateTime.UtcNow;
|
||||
|
||||
PartitionKey = $"UserId={UserId}";
|
||||
RowKey = string.Format("Date={0}__Type={1}",
|
||||
CoreHelpers.DateTimeToTableStorageKey(Date), Type);
|
||||
}
|
||||
|
||||
public UserEvent(Guid userId, Guid organizationId, EventType type)
|
||||
{
|
||||
OrganizationId = organizationId;
|
||||
UserId = userId;
|
||||
Type = (int)type;
|
||||
Date = DateTime.UtcNow;
|
||||
|
||||
PartitionKey = $"OrganizationId={OrganizationId}";
|
||||
RowKey = string.Format("Date={0}__UserId={1}__Type={2}",
|
||||
CoreHelpers.DateTimeToTableStorageKey(Date), UserId, Type);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user