From 7180a6618ea2e047f4c3790e4866e130ee80e007 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rui=20Tom=C3=A9?= <108268980+r-tome@users.noreply.github.com> Date: Tue, 30 Jan 2024 16:18:18 +0000 Subject: [PATCH] [PM-5873 / PM-5932] Fix collection creation by users other than the Organization owner (#3721) * [AC-2106] Add check for providers and additional check for null response * [PM-5873] Separated CollectionsController.Post flexible collections logic from non-migrated orgs --------- Co-authored-by: Shane Melton --- src/Api/Controllers/CollectionsController.cs | 58 ++++++++++++++------ 1 file changed, 41 insertions(+), 17 deletions(-) diff --git a/src/Api/Controllers/CollectionsController.cs b/src/Api/Controllers/CollectionsController.cs index 6c10805035..ba3647b25e 100644 --- a/src/Api/Controllers/CollectionsController.cs +++ b/src/Api/Controllers/CollectionsController.cs @@ -213,12 +213,15 @@ public class CollectionsController : Controller [HttpPost("")] public async Task Post(Guid orgId, [FromBody] CollectionRequestModel model) { + if (await FlexibleCollectionsIsEnabledAsync(orgId)) + { + // New flexible collections logic + return await Post_vNext(orgId, model); + } + var collection = model.ToCollection(orgId); - var flexibleCollectionsIsEnabled = await FlexibleCollectionsIsEnabledAsync(orgId); - var authorized = flexibleCollectionsIsEnabled - ? (await _authorizationService.AuthorizeAsync(User, collection, BulkCollectionOperations.Create)).Succeeded - : await CanCreateCollection(orgId, collection.Id) || await CanEditCollectionAsync(orgId, collection.Id); + var authorized = await CanCreateCollection(orgId, collection.Id) || await CanEditCollectionAsync(orgId, collection.Id); if (!authorized) { throw new NotFoundException(); @@ -229,7 +232,6 @@ public class CollectionsController : Controller // 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; @@ -251,16 +253,7 @@ public class CollectionsController : Controller await _collectionService.SaveAsync(collection, groups, users); - if (!_currentContext.UserId.HasValue) - { - return new CollectionResponseModel(collection); - } - - // If we have a user, fetch the collection to get the latest permission details - var userCollectionDetails = await _collectionRepository.GetByIdAsync(collection.Id, - _currentContext.UserId.Value, await FlexibleCollectionsIsEnabledAsync(collection.OrganizationId)); - - return new CollectionDetailsResponseModel(userCollectionDetails); + return new CollectionResponseModel(collection); } [HttpPut("{id}")] @@ -616,6 +609,35 @@ public class CollectionsController : Controller return responses; } + private async Task Post_vNext(Guid orgId, [FromBody] CollectionRequestModel model) + { + var collection = model.ToCollection(orgId); + + var authorized = (await _authorizationService.AuthorizeAsync(User, collection, BulkCollectionOperations.Create)).Succeeded; + if (!authorized) + { + throw new NotFoundException(); + } + + var groups = model.Groups?.Select(g => g.ToSelectionReadOnly()); + var users = model.Users?.Select(g => g.ToSelectionReadOnly()).ToList() ?? new List(); + + await _collectionService.SaveAsync(collection, groups, users); + + if (!_currentContext.UserId.HasValue || await _currentContext.ProviderUserForOrgAsync(orgId)) + { + return new CollectionResponseModel(collection); + } + + // If we have a user, fetch the collection to get the latest permission details + var userCollectionDetails = await _collectionRepository.GetByIdAsync(collection.Id, + _currentContext.UserId.Value, await FlexibleCollectionsIsEnabledAsync(collection.OrganizationId)); + + return userCollectionDetails == null + ? new CollectionResponseModel(collection) + : new CollectionDetailsResponseModel(userCollectionDetails); + } + private async Task Put_vNext(Guid id, CollectionRequestModel model) { var collection = await _collectionRepository.GetByIdAsync(id); @@ -629,7 +651,7 @@ public class CollectionsController : Controller var users = model.Users?.Select(g => g.ToSelectionReadOnly()); await _collectionService.SaveAsync(model.ToCollection(collection), groups, users); - if (!_currentContext.UserId.HasValue) + if (!_currentContext.UserId.HasValue || await _currentContext.ProviderUserForOrgAsync(collection.OrganizationId)) { return new CollectionResponseModel(collection); } @@ -637,7 +659,9 @@ public class CollectionsController : Controller // If we have a user, fetch the collection details to get the latest permission details for the user var updatedCollectionDetails = await _collectionRepository.GetByIdAsync(id, _currentContext.UserId.Value, await FlexibleCollectionsIsEnabledAsync(collection.OrganizationId)); - return new CollectionDetailsResponseModel(updatedCollectionDetails); + return updatedCollectionDetails == null + ? new CollectionResponseModel(collection) + : new CollectionDetailsResponseModel(updatedCollectionDetails); } private async Task PutUsers_vNext(Guid id, IEnumerable model)