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