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

125 lines
4.2 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;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.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;
CipherId = e.CipherId;
CollectionId = e.CollectionId;
GroupId = e.GroupId;
OrganizationUserId = e.OrganizationUserId;
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? CipherId { get; set; }
public Guid? CollectionId { get; set; }
public Guid? GroupId { get; set; }
public Guid? OrganizationUserId { 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 = e.OrganizationId.HasValue ? $"OrganizationId={e.OrganizationId}" : $"UserId={e.UserId}";
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 = string.Format("Date={0}__Uniquifier={1}", dateKey, uniquifier)
}
};
if(e.OrganizationId.HasValue && e.ActingUserId.HasValue)
2017-12-14 17:23:46 -05:00
{
entities.Add(new EventTableEntity(e)
{
PartitionKey = pKey,
RowKey = string.Format("ActingUserId={0}__Date={1}__Uniquifier={2}",
e.ActingUserId, dateKey, uniquifier)
2017-12-14 17:23:46 -05:00
});
}
if(e.CipherId.HasValue)
{
entities.Add(new EventTableEntity(e)
{
PartitionKey = pKey,
RowKey = string.Format("CipherId={0}__Date={1}__Uniquifier={2}",
e.CipherId, dateKey, uniquifier)
2017-12-14 17:23:46 -05:00
});
}
return entities;
}
}
}