1
0
mirror of https://github.com/bitwarden/server.git synced 2025-06-30 07:36:14 -05:00

[AC-1873] Fix: restore logic assigning Managers to new collections server-side (#3498)

* Restore pre-flexible collections logic to assign managers to new collections

* Dont overwrite existing access

* Fix and add tests
This commit is contained in:
Thomas Rittson
2023-12-02 01:28:10 +10:00
committed by GitHub
parent f46ea0bf3b
commit 519b3dea24
2 changed files with 86 additions and 4 deletions

View File

@ -4,7 +4,9 @@ using Bit.Api.Vault.AuthorizationHandlers.Collections;
using Bit.Core;
using Bit.Core.Context;
using Bit.Core.Entities;
using Bit.Core.Enums;
using Bit.Core.Exceptions;
using Bit.Core.Models.Data;
using Bit.Core.OrganizationFeatures.OrganizationCollections.Interfaces;
using Bit.Core.Repositories;
using Bit.Core.Services;
@ -26,6 +28,7 @@ public class CollectionsController : Controller
private readonly ICurrentContext _currentContext;
private readonly IBulkAddCollectionAccessCommand _bulkAddCollectionAccessCommand;
private readonly IFeatureService _featureService;
private readonly IOrganizationUserRepository _organizationUserRepository;
public CollectionsController(
ICollectionRepository collectionRepository,
@ -35,7 +38,8 @@ public class CollectionsController : Controller
IAuthorizationService authorizationService,
ICurrentContext currentContext,
IBulkAddCollectionAccessCommand bulkAddCollectionAccessCommand,
IFeatureService featureService)
IFeatureService featureService,
IOrganizationUserRepository organizationUserRepository)
{
_collectionRepository = collectionRepository;
_collectionService = collectionService;
@ -45,6 +49,7 @@ public class CollectionsController : Controller
_currentContext = currentContext;
_bulkAddCollectionAccessCommand = bulkAddCollectionAccessCommand;
_featureService = featureService;
_organizationUserRepository = organizationUserRepository;
}
private bool FlexibleCollectionsIsEnabled => _featureService.IsEnabled(FeatureFlagKeys.FlexibleCollections, _currentContext);
@ -168,7 +173,29 @@ public class CollectionsController : Controller
}
var groups = model.Groups?.Select(g => g.ToSelectionReadOnly());
var users = model.Users?.Select(g => g.ToSelectionReadOnly());
var users = model.Users?.Select(g => g.ToSelectionReadOnly()).ToList() ?? new List<CollectionAccessSelection>();
// Pre-flexible collections logic assigned Managers to collections they create
var assignUserToCollection =
!FlexibleCollectionsIsEnabled &&
!await _currentContext.EditAnyCollection(orgId) &&
await _currentContext.EditAssignedCollections(orgId);
var isNewCollection = collection.Id == default;
if (assignUserToCollection && isNewCollection && _currentContext.UserId.HasValue)
{
var orgUser = await _organizationUserRepository.GetByOrganizationAsync(orgId, _currentContext.UserId.Value);
// don't add duplicate access if the user has already specified it themselves
var existingAccess = users.Any(u => u.Id == orgUser.Id);
if (orgUser is { Status: OrganizationUserStatusType.Confirmed } && !existingAccess)
{
users.Add(new CollectionAccessSelection
{
Id = orgUser.Id,
ReadOnly = false
});
}
}
await _collectionService.SaveAsync(collection, groups, users);
return new CollectionResponseModel(collection);