mirror of
https://github.com/bitwarden/server.git
synced 2024-11-22 12:15:36 +01:00
stub out API for events
This commit is contained in:
parent
fecf665290
commit
adc23bf007
85
src/Api/Controllers/EventsController.cs
Normal file
85
src/Api/Controllers/EventsController.cs
Normal file
@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Bit.Core.Repositories;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Bit.Core.Exceptions;
|
||||
using Bit.Core.Models.Api;
|
||||
using Bit.Core.Services;
|
||||
using Bit.Core;
|
||||
|
||||
namespace Bit.Api.Controllers
|
||||
{
|
||||
[Route("events")]
|
||||
[Authorize("Application")]
|
||||
public class EventsController : Controller
|
||||
{
|
||||
private readonly IUserService _userService;
|
||||
private readonly IEventRepository _eventRepository;
|
||||
private readonly CurrentContext _currentContext;
|
||||
|
||||
public EventsController(
|
||||
IUserService userService,
|
||||
IEventRepository eventRepository,
|
||||
CurrentContext currentContext)
|
||||
{
|
||||
_userService = userService;
|
||||
_eventRepository = eventRepository;
|
||||
_currentContext = currentContext;
|
||||
}
|
||||
|
||||
[HttpGet("user")]
|
||||
public async Task<ListResponseModel<EventResponseModel>> GetUser(
|
||||
[FromQuery]DateTime? start = null, [FromQuery]DateTime? end = null)
|
||||
{
|
||||
var dateRange = GetDateRange(start, end);
|
||||
var userId = _userService.GetProperUserId(User).Value;
|
||||
var events = await _eventRepository.GetManyByUserAsync(userId, dateRange.Item1, dateRange.Item2);
|
||||
var responses = events.Select(e => new EventResponseModel(e));
|
||||
return new ListResponseModel<EventResponseModel>(responses);
|
||||
}
|
||||
|
||||
[HttpGet("organization/{id}")]
|
||||
public async Task<ListResponseModel<EventResponseModel>> GetOrganization(string id,
|
||||
[FromQuery]DateTime? start = null, [FromQuery]DateTime? end = null)
|
||||
{
|
||||
var orgId = new Guid(id);
|
||||
if(!_currentContext.OrganizationAdmin(orgId))
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
var dateRange = GetDateRange(start, end);
|
||||
var events = await _eventRepository.GetManyByOrganizationAsync(orgId, dateRange.Item1, dateRange.Item2);
|
||||
var responses = events.Select(e => new EventResponseModel(e));
|
||||
return new ListResponseModel<EventResponseModel>(responses);
|
||||
}
|
||||
|
||||
private Tuple<DateTime, DateTime> GetDateRange(DateTime? start, DateTime? end)
|
||||
{
|
||||
var endSet = false;
|
||||
if(!end.HasValue)
|
||||
{
|
||||
endSet = true;
|
||||
end = DateTime.UtcNow.Date.AddDays(1).AddMilliseconds(-1);
|
||||
}
|
||||
|
||||
if(!start.HasValue)
|
||||
{
|
||||
start = end.Value.AddDays(-30);
|
||||
if(endSet)
|
||||
{
|
||||
start = end.Value.AddMilliseconds(1);
|
||||
}
|
||||
}
|
||||
|
||||
if(start.Value > end.Value || (end.Value - start.Value) > TimeSpan.FromDays(32))
|
||||
{
|
||||
throw new BadRequestException("Invalid date range.");
|
||||
}
|
||||
|
||||
return new Tuple<DateTime, DateTime>(start.Value, end.Value);
|
||||
}
|
||||
}
|
||||
}
|
38
src/Core/Models/Api/Response/EventResponseModel.cs
Normal file
38
src/Core/Models/Api/Response/EventResponseModel.cs
Normal file
@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Models.Data;
|
||||
|
||||
namespace Bit.Core.Models.Api
|
||||
{
|
||||
public class EventResponseModel : ResponseModel
|
||||
{
|
||||
public EventResponseModel(IEvent ev)
|
||||
: base("event")
|
||||
{
|
||||
if(ev == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(ev));
|
||||
}
|
||||
|
||||
Type = ev.Type;
|
||||
UserId = ev.UserId;
|
||||
OrganizationId = ev.OrganizationId;
|
||||
CipherId = ev.CipherId;
|
||||
CollectionId = ev.CollectionId;
|
||||
GroupId = ev.GroupId;
|
||||
OrganizationUserId = ev.OrganizationUserId;
|
||||
ActingUserId = ev.ActingUserId;
|
||||
Date = ev.Date;
|
||||
}
|
||||
|
||||
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 Guid? ActingUserId { get; set; }
|
||||
public DateTime Date { get; set; }
|
||||
}
|
||||
}
|
@ -8,6 +8,7 @@ namespace Bit.Core.Repositories
|
||||
public interface IEventRepository
|
||||
{
|
||||
Task<ICollection<IEvent>> GetManyByUserAsync(Guid userId, DateTime startDate, DateTime endDate);
|
||||
Task<ICollection<IEvent>> GetManyByOrganizationAsync(Guid organizationId, DateTime startDate, DateTime endDate);
|
||||
Task CreateAsync(IEvent e);
|
||||
Task CreateManyAsync(IList<IEvent> e);
|
||||
}
|
||||
|
@ -25,6 +25,11 @@ namespace Bit.Core.Repositories.SqlServer
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<ICollection<IEvent>> GetManyByOrganizationAsync(Guid organizationId, DateTime startDate, DateTime endDate)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public async Task CreateAsync(IEvent e)
|
||||
{
|
||||
if(!(e is Event ev))
|
||||
|
@ -24,8 +24,7 @@ namespace Bit.Core.Repositories.TableStorage
|
||||
_table = tableClient.GetTableReference("event");
|
||||
}
|
||||
|
||||
public async Task<ICollection<IEvent>> GetManyByUserAsync(Guid userId,
|
||||
DateTime startDate, DateTime endDate)
|
||||
public async Task<ICollection<IEvent>> GetManyByUserAsync(Guid userId, DateTime startDate, DateTime endDate)
|
||||
{
|
||||
var start = CoreHelpers.DateTimeToTableStorageKey(startDate);
|
||||
var end = CoreHelpers.DateTimeToTableStorageKey(endDate);
|
||||
@ -48,7 +47,38 @@ namespace Bit.Core.Repositories.TableStorage
|
||||
var queryResults = await _table.ExecuteQuerySegmentedAsync(query, continuationToken);
|
||||
continuationToken = queryResults.ContinuationToken;
|
||||
results.AddRange(queryResults.Results);
|
||||
} while(continuationToken != null);
|
||||
}
|
||||
while(continuationToken != null);
|
||||
|
||||
return results.Select(r => r as IEvent).ToList();
|
||||
}
|
||||
|
||||
public async Task<ICollection<IEvent>> GetManyByOrganizationAsync(Guid organizationId,
|
||||
DateTime startDate, DateTime endDate)
|
||||
{
|
||||
var start = CoreHelpers.DateTimeToTableStorageKey(startDate);
|
||||
var end = CoreHelpers.DateTimeToTableStorageKey(endDate);
|
||||
|
||||
var rowFilter = TableQuery.CombineFilters(
|
||||
TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.GreaterThanOrEqual, $"{start}_"),
|
||||
TableOperators.And,
|
||||
TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.LessThanOrEqual, $"{end}`"));
|
||||
|
||||
var filter = TableQuery.CombineFilters(
|
||||
TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, $"OrganizationId={organizationId}"),
|
||||
TableOperators.And,
|
||||
rowFilter);
|
||||
|
||||
var query = new TableQuery<EventTableEntity>().Where(filter);
|
||||
var results = new List<EventTableEntity>();
|
||||
TableContinuationToken continuationToken = null;
|
||||
do
|
||||
{
|
||||
var queryResults = await _table.ExecuteQuerySegmentedAsync(query, continuationToken);
|
||||
continuationToken = queryResults.ContinuationToken;
|
||||
results.AddRange(queryResults.Results);
|
||||
}
|
||||
while(continuationToken != null);
|
||||
|
||||
return results.Select(r => r as IEvent).ToList();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user