mirror of
https://github.com/bitwarden/server
synced 2025-12-12 22:33:45 +00:00
[PM-22442] Remove CollectionService (#6015)
* Refactor Collections and OrganizationExport Controllers to Remove ICollectionService Dependency * Remove ICollectionService registration from ServiceCollectionExtensions * Remove CollectionServiceTests file as part of the ongoing refactor to eliminate ICollectionService. * Remove ICollectionService and its implementation in CollectionService as part of the ongoing refactor to eliminate the service.
This commit is contained in:
@@ -19,7 +19,6 @@ namespace Bit.Api.Controllers;
|
|||||||
public class CollectionsController : Controller
|
public class CollectionsController : Controller
|
||||||
{
|
{
|
||||||
private readonly ICollectionRepository _collectionRepository;
|
private readonly ICollectionRepository _collectionRepository;
|
||||||
private readonly ICollectionService _collectionService;
|
|
||||||
private readonly ICreateCollectionCommand _createCollectionCommand;
|
private readonly ICreateCollectionCommand _createCollectionCommand;
|
||||||
private readonly IUpdateCollectionCommand _updateCollectionCommand;
|
private readonly IUpdateCollectionCommand _updateCollectionCommand;
|
||||||
private readonly IDeleteCollectionCommand _deleteCollectionCommand;
|
private readonly IDeleteCollectionCommand _deleteCollectionCommand;
|
||||||
@@ -30,7 +29,6 @@ public class CollectionsController : Controller
|
|||||||
|
|
||||||
public CollectionsController(
|
public CollectionsController(
|
||||||
ICollectionRepository collectionRepository,
|
ICollectionRepository collectionRepository,
|
||||||
ICollectionService collectionService,
|
|
||||||
ICreateCollectionCommand createCollectionCommand,
|
ICreateCollectionCommand createCollectionCommand,
|
||||||
IUpdateCollectionCommand updateCollectionCommand,
|
IUpdateCollectionCommand updateCollectionCommand,
|
||||||
IDeleteCollectionCommand deleteCollectionCommand,
|
IDeleteCollectionCommand deleteCollectionCommand,
|
||||||
@@ -40,7 +38,6 @@ public class CollectionsController : Controller
|
|||||||
IBulkAddCollectionAccessCommand bulkAddCollectionAccessCommand)
|
IBulkAddCollectionAccessCommand bulkAddCollectionAccessCommand)
|
||||||
{
|
{
|
||||||
_collectionRepository = collectionRepository;
|
_collectionRepository = collectionRepository;
|
||||||
_collectionService = collectionService;
|
|
||||||
_createCollectionCommand = createCollectionCommand;
|
_createCollectionCommand = createCollectionCommand;
|
||||||
_updateCollectionCommand = updateCollectionCommand;
|
_updateCollectionCommand = updateCollectionCommand;
|
||||||
_deleteCollectionCommand = deleteCollectionCommand;
|
_deleteCollectionCommand = deleteCollectionCommand;
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ public class OrganizationExportController : Controller
|
|||||||
{
|
{
|
||||||
private readonly ICurrentContext _currentContext;
|
private readonly ICurrentContext _currentContext;
|
||||||
private readonly IUserService _userService;
|
private readonly IUserService _userService;
|
||||||
private readonly ICollectionService _collectionService;
|
|
||||||
private readonly ICipherService _cipherService;
|
private readonly ICipherService _cipherService;
|
||||||
private readonly GlobalSettings _globalSettings;
|
private readonly GlobalSettings _globalSettings;
|
||||||
private readonly IFeatureService _featureService;
|
private readonly IFeatureService _featureService;
|
||||||
@@ -30,7 +29,6 @@ public class OrganizationExportController : Controller
|
|||||||
public OrganizationExportController(
|
public OrganizationExportController(
|
||||||
ICurrentContext currentContext,
|
ICurrentContext currentContext,
|
||||||
ICipherService cipherService,
|
ICipherService cipherService,
|
||||||
ICollectionService collectionService,
|
|
||||||
IUserService userService,
|
IUserService userService,
|
||||||
GlobalSettings globalSettings,
|
GlobalSettings globalSettings,
|
||||||
IFeatureService featureService,
|
IFeatureService featureService,
|
||||||
@@ -40,7 +38,6 @@ public class OrganizationExportController : Controller
|
|||||||
{
|
{
|
||||||
_currentContext = currentContext;
|
_currentContext = currentContext;
|
||||||
_cipherService = cipherService;
|
_cipherService = cipherService;
|
||||||
_collectionService = collectionService;
|
|
||||||
_userService = userService;
|
_userService = userService;
|
||||||
_globalSettings = globalSettings;
|
_globalSettings = globalSettings;
|
||||||
_featureService = featureService;
|
_featureService = featureService;
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
using Bit.Core.Entities;
|
|
||||||
|
|
||||||
namespace Bit.Core.Services;
|
|
||||||
|
|
||||||
public interface ICollectionService
|
|
||||||
{
|
|
||||||
Task DeleteUserAsync(Collection collection, Guid organizationUserId);
|
|
||||||
}
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
#nullable enable
|
|
||||||
|
|
||||||
using Bit.Core.Entities;
|
|
||||||
using Bit.Core.Exceptions;
|
|
||||||
using Bit.Core.Repositories;
|
|
||||||
|
|
||||||
namespace Bit.Core.Services;
|
|
||||||
|
|
||||||
public class CollectionService : ICollectionService
|
|
||||||
{
|
|
||||||
private readonly IEventService _eventService;
|
|
||||||
private readonly IOrganizationUserRepository _organizationUserRepository;
|
|
||||||
private readonly ICollectionRepository _collectionRepository;
|
|
||||||
|
|
||||||
public CollectionService(
|
|
||||||
IEventService eventService,
|
|
||||||
IOrganizationUserRepository organizationUserRepository,
|
|
||||||
ICollectionRepository collectionRepository)
|
|
||||||
{
|
|
||||||
_eventService = eventService;
|
|
||||||
_organizationUserRepository = organizationUserRepository;
|
|
||||||
_collectionRepository = collectionRepository;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task DeleteUserAsync(Collection collection, Guid organizationUserId)
|
|
||||||
{
|
|
||||||
if (collection.Type == Enums.CollectionType.DefaultUserCollection)
|
|
||||||
{
|
|
||||||
throw new BadRequestException("You cannot modify member access for collections with the type as DefaultUserCollection.");
|
|
||||||
}
|
|
||||||
|
|
||||||
var orgUser = await _organizationUserRepository.GetByIdAsync(organizationUserId);
|
|
||||||
if (orgUser == null || orgUser.OrganizationId != collection.OrganizationId)
|
|
||||||
{
|
|
||||||
throw new NotFoundException();
|
|
||||||
}
|
|
||||||
await _collectionRepository.DeleteUserAsync(collection.Id, organizationUserId);
|
|
||||||
await _eventService.LogOrganizationUserEventAsync(orgUser, Enums.EventType.OrganizationUser_Updated);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -117,7 +117,6 @@ public static class ServiceCollectionExtensions
|
|||||||
services.AddTrialInitiationServices();
|
services.AddTrialInitiationServices();
|
||||||
services.AddOrganizationServices(globalSettings);
|
services.AddOrganizationServices(globalSettings);
|
||||||
services.AddPolicyServices();
|
services.AddPolicyServices();
|
||||||
services.AddScoped<ICollectionService, CollectionService>();
|
|
||||||
services.AddScoped<IGroupService, GroupService>();
|
services.AddScoped<IGroupService, GroupService>();
|
||||||
services.AddScoped<IEventService, EventService>();
|
services.AddScoped<IEventService, EventService>();
|
||||||
services.AddScoped<IEmergencyAccessService, EmergencyAccessService>();
|
services.AddScoped<IEmergencyAccessService, EmergencyAccessService>();
|
||||||
|
|||||||
@@ -1,70 +0,0 @@
|
|||||||
using Bit.Core.AdminConsole.Entities;
|
|
||||||
using Bit.Core.Entities;
|
|
||||||
using Bit.Core.Enums;
|
|
||||||
using Bit.Core.Exceptions;
|
|
||||||
using Bit.Core.Repositories;
|
|
||||||
using Bit.Core.Services;
|
|
||||||
using Bit.Core.Test.AutoFixture.OrganizationFixtures;
|
|
||||||
using Bit.Test.Common.AutoFixture;
|
|
||||||
using Bit.Test.Common.AutoFixture.Attributes;
|
|
||||||
using NSubstitute;
|
|
||||||
using Xunit;
|
|
||||||
|
|
||||||
namespace Bit.Core.Test.Services;
|
|
||||||
|
|
||||||
[SutProviderCustomize]
|
|
||||||
[OrganizationCustomize]
|
|
||||||
public class CollectionServiceTest
|
|
||||||
{
|
|
||||||
[Theory, BitAutoData]
|
|
||||||
public async Task DeleteUserAsync_DeletesValidUserWhoBelongsToCollection(Collection collection,
|
|
||||||
Organization organization, OrganizationUser organizationUser, SutProvider<CollectionService> sutProvider)
|
|
||||||
{
|
|
||||||
collection.OrganizationId = organization.Id;
|
|
||||||
organizationUser.OrganizationId = organization.Id;
|
|
||||||
sutProvider.GetDependency<IOrganizationUserRepository>().GetByIdAsync(organizationUser.Id)
|
|
||||||
.Returns(organizationUser);
|
|
||||||
|
|
||||||
await sutProvider.Sut.DeleteUserAsync(collection, organizationUser.Id);
|
|
||||||
|
|
||||||
await sutProvider.GetDependency<ICollectionRepository>().Received()
|
|
||||||
.DeleteUserAsync(collection.Id, organizationUser.Id);
|
|
||||||
await sutProvider.GetDependency<IEventService>().Received().LogOrganizationUserEventAsync(organizationUser, EventType.OrganizationUser_Updated);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Theory, BitAutoData]
|
|
||||||
public async Task DeleteUserAsync_InvalidUser_ThrowsNotFound(Collection collection, Organization organization,
|
|
||||||
OrganizationUser organizationUser, SutProvider<CollectionService> sutProvider)
|
|
||||||
{
|
|
||||||
collection.OrganizationId = organization.Id;
|
|
||||||
sutProvider.GetDependency<IOrganizationUserRepository>().GetByIdAsync(organizationUser.Id)
|
|
||||||
.Returns(organizationUser);
|
|
||||||
|
|
||||||
// user not in organization
|
|
||||||
await Assert.ThrowsAsync<NotFoundException>(() =>
|
|
||||||
sutProvider.Sut.DeleteUserAsync(collection, organizationUser.Id));
|
|
||||||
// invalid user
|
|
||||||
await Assert.ThrowsAsync<NotFoundException>(() => sutProvider.Sut.DeleteUserAsync(collection, Guid.NewGuid()));
|
|
||||||
await sutProvider.GetDependency<ICollectionRepository>().DidNotReceiveWithAnyArgs().DeleteUserAsync(default, default);
|
|
||||||
await sutProvider.GetDependency<IEventService>().DidNotReceiveWithAnyArgs()
|
|
||||||
.LogOrganizationUserEventAsync<OrganizationUser>(default, default);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Theory, BitAutoData]
|
|
||||||
public async Task DeleteUserAsync_WithDefaultUserCollectionType_ThrowsBadRequest(Collection collection,
|
|
||||||
Organization organization, OrganizationUser organizationUser, SutProvider<CollectionService> sutProvider)
|
|
||||||
{
|
|
||||||
collection.Type = CollectionType.DefaultUserCollection;
|
|
||||||
collection.OrganizationId = organization.Id;
|
|
||||||
organizationUser.OrganizationId = organization.Id;
|
|
||||||
|
|
||||||
var exception = await Assert.ThrowsAsync<BadRequestException>(() =>
|
|
||||||
sutProvider.Sut.DeleteUserAsync(collection, organizationUser.Id));
|
|
||||||
Assert.Contains("You cannot modify member access for collections with the type as DefaultUserCollection.", exception.Message);
|
|
||||||
|
|
||||||
await sutProvider.GetDependency<IOrganizationUserRepository>().DidNotReceiveWithAnyArgs().GetByIdAsync(default);
|
|
||||||
await sutProvider.GetDependency<ICollectionRepository>().DidNotReceiveWithAnyArgs().DeleteUserAsync(default, default);
|
|
||||||
await sutProvider.GetDependency<IEventService>().DidNotReceiveWithAnyArgs()
|
|
||||||
.LogOrganizationUserEventAsync<OrganizationUser>(default, default);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user