mirror of
https://github.com/bitwarden/server
synced 2025-12-30 23:23:37 +00:00
[PM-28746] Add support for Organization_ItemOrganization_Accepted/Declined event types (#6747)
This commit is contained in:
@@ -21,17 +21,21 @@ public class CollectController : Controller
|
||||
private readonly IEventService _eventService;
|
||||
private readonly ICipherRepository _cipherRepository;
|
||||
private readonly IOrganizationRepository _organizationRepository;
|
||||
private readonly IOrganizationUserRepository _organizationUserRepository;
|
||||
|
||||
public CollectController(
|
||||
ICurrentContext currentContext,
|
||||
IEventService eventService,
|
||||
ICipherRepository cipherRepository,
|
||||
IOrganizationRepository organizationRepository)
|
||||
IOrganizationRepository organizationRepository,
|
||||
IOrganizationUserRepository organizationUserRepository
|
||||
)
|
||||
{
|
||||
_currentContext = currentContext;
|
||||
_eventService = eventService;
|
||||
_cipherRepository = cipherRepository;
|
||||
_organizationRepository = organizationRepository;
|
||||
_organizationUserRepository = organizationUserRepository;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
@@ -54,6 +58,24 @@ public class CollectController : Controller
|
||||
await _eventService.LogUserEventAsync(_currentContext.UserId.Value, eventModel.Type, eventModel.Date);
|
||||
break;
|
||||
|
||||
case EventType.Organization_ItemOrganization_Accepted:
|
||||
case EventType.Organization_ItemOrganization_Declined:
|
||||
if (!eventModel.OrganizationId.HasValue || !_currentContext.UserId.HasValue)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var orgUser = await _organizationUserRepository.GetByOrganizationAsync(eventModel.OrganizationId.Value, _currentContext.UserId.Value);
|
||||
|
||||
if (orgUser == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
await _eventService.LogOrganizationUserEventAsync(orgUser, eventModel.Type, eventModel.Date);
|
||||
|
||||
continue;
|
||||
|
||||
// Cipher events
|
||||
case EventType.Cipher_ClientAutofilled:
|
||||
case EventType.Cipher_ClientCopiedHiddenField:
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using AutoFixture.Xunit2;
|
||||
using Bit.Core.AdminConsole.Entities;
|
||||
using Bit.Core.Context;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Repositories;
|
||||
using Bit.Core.Services;
|
||||
@@ -9,6 +10,7 @@ using Bit.Core.Vault.Models.Data;
|
||||
using Bit.Core.Vault.Repositories;
|
||||
using Bit.Events.Controllers;
|
||||
using Bit.Events.Models;
|
||||
using Bit.Test.Common.AutoFixture.Attributes;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using NSubstitute;
|
||||
|
||||
@@ -21,6 +23,7 @@ public class CollectControllerTests
|
||||
private readonly IEventService _eventService;
|
||||
private readonly ICipherRepository _cipherRepository;
|
||||
private readonly IOrganizationRepository _organizationRepository;
|
||||
private readonly IOrganizationUserRepository _organizationUserRepository;
|
||||
|
||||
public CollectControllerTests()
|
||||
{
|
||||
@@ -28,12 +31,14 @@ public class CollectControllerTests
|
||||
_eventService = Substitute.For<IEventService>();
|
||||
_cipherRepository = Substitute.For<ICipherRepository>();
|
||||
_organizationRepository = Substitute.For<IOrganizationRepository>();
|
||||
_organizationUserRepository = Substitute.For<IOrganizationUserRepository>();
|
||||
|
||||
_sut = new CollectController(
|
||||
_currentContext,
|
||||
_eventService,
|
||||
_cipherRepository,
|
||||
_organizationRepository
|
||||
_organizationRepository,
|
||||
_organizationUserRepository
|
||||
);
|
||||
}
|
||||
|
||||
@@ -74,6 +79,32 @@ public class CollectControllerTests
|
||||
await _eventService.Received(1).LogUserEventAsync(userId, EventType.User_ClientExportedVault, eventDate);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[BitAutoData(EventType.Organization_ItemOrganization_Accepted)]
|
||||
[BitAutoData(EventType.Organization_ItemOrganization_Declined)]
|
||||
public async Task Post_Organization_ItemOrganization_LogsOrganizationUserEvent(
|
||||
EventType type, Guid userId, Guid orgId, OrganizationUser orgUser)
|
||||
{
|
||||
_currentContext.UserId.Returns(userId);
|
||||
orgUser.OrganizationId = orgId;
|
||||
_organizationUserRepository.GetByOrganizationAsync(orgId, userId).Returns(orgUser);
|
||||
var eventDate = DateTime.UtcNow;
|
||||
var events = new List<EventModel>
|
||||
{
|
||||
new EventModel
|
||||
{
|
||||
Type = type,
|
||||
OrganizationId = orgId,
|
||||
Date = eventDate
|
||||
}
|
||||
};
|
||||
|
||||
var result = await _sut.Post(events);
|
||||
|
||||
Assert.IsType<OkResult>(result);
|
||||
await _eventService.Received(1).LogOrganizationUserEventAsync(orgUser, type, eventDate);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[AutoData]
|
||||
public async Task Post_CipherAutofilled_WithValidCipher_LogsCipherEvent(Guid userId, Guid cipherId, CipherDetails cipherDetails)
|
||||
|
||||
Reference in New Issue
Block a user