Files
server/src/Core/Models/Data/EventTableEntity.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

155 lines
4.6 KiB
C#
Raw Normal View History

using Bit.Core.Enums;
2017-12-08 23:09:50 -05:00
using Bit.Core.Utilities;
2020-01-10 08:33:13 -05:00
using Microsoft.Azure.Cosmos.Table;
2022-08-29 14:53:16 -04:00
namespace Bit.Core.Models.Data;
public class EventTableEntity : TableEntity, IEvent
{
2022-08-29 14:53:16 -04:00
public EventTableEntity() { }
private EventTableEntity(IEvent e)
{
2022-08-29 14:53:16 -04:00
Date = e.Date;
Type = e.Type;
UserId = e.UserId;
OrganizationId = e.OrganizationId;
InstallationId = e.InstallationId;
ProviderId = e.ProviderId;
CipherId = e.CipherId;
CollectionId = e.CollectionId;
PolicyId = e.PolicyId;
GroupId = e.GroupId;
OrganizationUserId = e.OrganizationUserId;
ProviderUserId = e.ProviderUserId;
ProviderOrganizationId = e.ProviderOrganizationId;
DeviceType = e.DeviceType;
IpAddress = e.IpAddress;
ActingUserId = e.ActingUserId;
}
2017-12-08 23:09:50 -05:00
2022-08-29 14:53:16 -04:00
public DateTime Date { get; set; }
public EventType Type { get; set; }
public Guid? UserId { get; set; }
public Guid? OrganizationId { get; set; }
public Guid? InstallationId { get; set; }
public Guid? ProviderId { get; set; }
public Guid? CipherId { get; set; }
public Guid? CollectionId { get; set; }
public Guid? PolicyId { get; set; }
public Guid? GroupId { get; set; }
public Guid? OrganizationUserId { get; set; }
public Guid? ProviderUserId { get; set; }
public Guid? ProviderOrganizationId { get; set; }
public DeviceType? DeviceType { get; set; }
public string IpAddress { get; set; }
public Guid? ActingUserId { get; set; }
public override IDictionary<string, EntityProperty> WriteEntity(OperationContext operationContext)
{
var result = base.WriteEntity(operationContext);
var typeName = nameof(Type);
if (result.ContainsKey(typeName))
{
result[typeName] = new EntityProperty((int)Type);
}
else
2017-12-08 23:09:50 -05:00
{
2022-08-29 14:53:16 -04:00
result.Add(typeName, new EntityProperty((int)Type));
2017-12-08 23:09:50 -05:00
}
2022-08-29 14:53:16 -04:00
var deviceTypeName = nameof(DeviceType);
if (result.ContainsKey(deviceTypeName))
2017-12-08 23:09:50 -05:00
{
2022-08-29 14:53:16 -04:00
result[deviceTypeName] = new EntityProperty((int?)DeviceType);
}
else
{
result.Add(deviceTypeName, new EntityProperty((int?)DeviceType));
}
2022-08-29 14:53:16 -04:00
return result;
}
2022-08-29 14:53:16 -04:00
public override void ReadEntity(IDictionary<string, EntityProperty> properties,
OperationContext operationContext)
{
base.ReadEntity(properties, operationContext);
2022-08-29 14:53:16 -04:00
var typeName = nameof(Type);
if (properties.ContainsKey(typeName) && properties[typeName].Int32Value.HasValue)
{
Type = (EventType)properties[typeName].Int32Value.Value;
2017-12-08 23:09:50 -05:00
}
2017-12-14 15:04:20 -05:00
2022-08-29 14:53:16 -04:00
var deviceTypeName = nameof(DeviceType);
if (properties.ContainsKey(deviceTypeName) && properties[deviceTypeName].Int32Value.HasValue)
2017-12-14 15:04:20 -05:00
{
2022-08-29 14:53:16 -04:00
DeviceType = (DeviceType)properties[deviceTypeName].Int32Value.Value;
2017-12-14 17:23:46 -05:00
}
2022-08-29 14:53:16 -04:00
}
2017-12-14 17:23:46 -05:00
2022-08-29 14:53:16 -04:00
public static List<EventTableEntity> IndexEvent(EventMessage e)
{
var uniquifier = e.IdempotencyId.GetValueOrDefault(Guid.NewGuid());
2022-08-29 14:53:16 -04:00
var pKey = GetPartitionKey(e);
2022-08-29 14:53:16 -04:00
var dateKey = CoreHelpers.DateTimeToTableStorageKey(e.Date);
2017-12-14 17:23:46 -05:00
2022-08-29 14:53:16 -04:00
var entities = new List<EventTableEntity>
{
new EventTableEntity(e)
2017-12-14 17:23:46 -05:00
{
2022-08-29 14:53:16 -04:00
PartitionKey = pKey,
RowKey = $"Date={dateKey}__Uniquifier={uniquifier}"
}
2022-08-29 14:53:16 -04:00
};
2022-08-29 14:53:16 -04:00
if (e.OrganizationId.HasValue && e.ActingUserId.HasValue)
{
entities.Add(new EventTableEntity(e)
{
2022-08-29 14:53:16 -04:00
PartitionKey = pKey,
RowKey = $"ActingUserId={e.ActingUserId}__Date={dateKey}__Uniquifier={uniquifier}"
});
}
2017-12-14 17:23:46 -05:00
2022-08-29 14:53:16 -04:00
if (!e.OrganizationId.HasValue && e.ProviderId.HasValue && e.ActingUserId.HasValue)
{
entities.Add(new EventTableEntity(e)
2017-12-14 17:23:46 -05:00
{
2022-08-29 14:53:16 -04:00
PartitionKey = pKey,
RowKey = $"ActingUserId={e.ActingUserId}__Date={dateKey}__Uniquifier={uniquifier}"
});
2017-12-14 17:23:46 -05:00
}
2022-08-29 14:53:16 -04:00
if (e.CipherId.HasValue)
{
2022-08-29 14:53:16 -04:00
entities.Add(new EventTableEntity(e)
{
2022-08-29 14:53:16 -04:00
PartitionKey = pKey,
RowKey = $"CipherId={e.CipherId}__Date={dateKey}__Uniquifier={uniquifier}"
});
}
2021-12-16 15:35:09 +01:00
2022-08-29 14:53:16 -04:00
return entities;
}
2022-08-29 14:53:16 -04:00
private static string GetPartitionKey(EventMessage e)
{
if (e.OrganizationId.HasValue)
{
return $"OrganizationId={e.OrganizationId}";
}
if (e.ProviderId.HasValue)
{
return $"ProviderId={e.ProviderId}";
}
2022-08-29 14:53:16 -04:00
return $"UserId={e.UserId}";
}
}