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