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.

156 lines
5.1 KiB
C#
Raw Normal View History

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;
namespace Bit.Core.Models.Data
{
2017-12-08 23:09:50 -05:00
public class EventTableEntity : TableEntity, IEvent
{
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;
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;
ProviderUserId = e.ProviderUserId;
ProviderOrganizationId = e.ProviderOrganizationId;
DeviceType = e.DeviceType;
IpAddress = e.IpAddress;
2017-12-08 23:09:50 -05:00
ActingUserId = e.ActingUserId;
}
public DateTime Date { get; set; }
2017-12-08 23:09:50 -05:00
public EventType Type { get; set; }
public Guid? UserId { get; set; }
public Guid? OrganizationId { get; set; }
public Guid? ProviderId { get; set; }
public Guid? CipherId { get; set; }
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; }
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);
var typeName = nameof(Type);
if (result.ContainsKey(typeName))
2017-12-08 23:09:50 -05:00
{
result[typeName] = new EntityProperty((int)Type);
2017-12-08 23:09:50 -05:00
}
else
{
result.Add(typeName, new EntityProperty((int)Type));
}
var deviceTypeName = nameof(DeviceType);
if (result.ContainsKey(deviceTypeName))
{
result[deviceTypeName] = new EntityProperty((int?)DeviceType);
2017-12-08 23:09:50 -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
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
var typeName = nameof(Type);
if (properties.ContainsKey(typeName) && properties[typeName].Int32Value.HasValue)
2017-12-14 17:23:46 -05:00
{
Type = (EventType)properties[typeName].Int32Value.Value;
2017-12-14 17:23:46 -05:00
}
var deviceTypeName = nameof(DeviceType);
if (properties.ContainsKey(deviceTypeName) && properties[deviceTypeName].Int32Value.HasValue)
2017-12-14 17:23:46 -05:00
{
DeviceType = (DeviceType)properties[deviceTypeName].Int32Value.Value;
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}"
2017-12-14 17:23:46 -05:00
}
};
if (e.OrganizationId.HasValue && e.ActingUserId.HasValue)
2017-12-14 17:23:46 -05:00
{
entities.Add(new EventTableEntity(e)
{
PartitionKey = pKey,
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
});
}
if (e.CipherId.HasValue)
2017-12-14 17:23:46 -05:00
{
entities.Add(new EventTableEntity(e)
{
PartitionKey = pKey,
RowKey = $"CipherId={e.CipherId}__Date={dateKey}__Uniquifier={uniquifier}"
2017-12-14 17:23:46 -05:00
});
}
return entities;
}
private static string GetPartitionKey(EventMessage e)
{
if (e.OrganizationId.HasValue)
{
return $"OrganizationId={e.OrganizationId}";
}
2021-12-16 15:35:09 +01:00
if (e.ProviderId.HasValue)
{
return $"ProviderId={e.ProviderId}";
}
return $"UserId={e.UserId}";
}
}
}