From 2f974d6b061c0fb62837659c5380503161e447c5 Mon Sep 17 00:00:00 2001 From: Rui Tome Date: Thu, 19 Oct 2023 12:33:32 +0100 Subject: [PATCH] [AC-1748] Updated CurrentContext EditAssignedCollections, DeleteAssignedCollections, ViewAssignedCollections to check for flexible collections feature flag --- src/Core/Context/CurrentContext.cs | 25 ++++++++++++++++++++++++- src/Notifications/NotificationsHub.cs | 4 ++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Core/Context/CurrentContext.cs b/src/Core/Context/CurrentContext.cs index 1def551a8c..551df2dc23 100644 --- a/src/Core/Context/CurrentContext.cs +++ b/src/Core/Context/CurrentContext.cs @@ -2,9 +2,11 @@ using Bit.Core.Entities; using Bit.Core.Enums; using Bit.Core.Enums.Provider; +using Bit.Core.Exceptions; using Bit.Core.Identity; using Bit.Core.Models.Data; using Bit.Core.Repositories; +using Bit.Core.Services; using Bit.Core.Settings; using Bit.Core.Utilities; using Microsoft.AspNetCore.Http; @@ -15,11 +17,14 @@ public class CurrentContext : ICurrentContext { private readonly IProviderOrganizationRepository _providerOrganizationRepository; private readonly IProviderUserRepository _providerUserRepository; + private readonly IFeatureService _featureService; private bool _builtHttpContext; private bool _builtClaimsPrincipal; private IEnumerable _providerOrganizationProviderDetails; private IEnumerable _providerUserOrganizations; + private bool FlexibleCollectionsIsEnabled => _featureService.IsEnabled(FeatureFlagKeys.FlexibleCollections, this); + public virtual HttpContext HttpContext { get; set; } public virtual Guid? UserId { get; set; } public virtual User User { get; set; } @@ -41,10 +46,12 @@ public class CurrentContext : ICurrentContext public CurrentContext( IProviderOrganizationRepository providerOrganizationRepository, - IProviderUserRepository providerUserRepository) + IProviderUserRepository providerUserRepository, + IFeatureService featureService) { _providerOrganizationRepository = providerOrganizationRepository; _providerUserRepository = providerUserRepository; + _featureService = featureService; } public async virtual Task BuildAsync(HttpContext httpContext, GlobalSettings globalSettings) @@ -335,12 +342,22 @@ public class CurrentContext : ICurrentContext public async Task EditAssignedCollections(Guid orgId) { + if (FlexibleCollectionsIsEnabled) + { + throw new FeatureUnavailableException("Flexible Collections is ON when it should be OFF."); + } + return await OrganizationManager(orgId) || (Organizations?.Any(o => o.Id == orgId && (o.Permissions?.EditAssignedCollections ?? false)) ?? false); } public async Task DeleteAssignedCollections(Guid orgId) { + if (FlexibleCollectionsIsEnabled) + { + throw new FeatureUnavailableException("Flexible Collections is ON when it should be OFF."); + } + return await OrganizationManager(orgId) || (Organizations?.Any(o => o.Id == orgId && (o.Permissions?.DeleteAssignedCollections ?? false)) ?? false); } @@ -352,6 +369,12 @@ public class CurrentContext : ICurrentContext * Owner, Admin, Manager, and Provider checks are handled via the EditAssigned/DeleteAssigned context calls. * This entire method will be moved to the CollectionAuthorizationHandler in the future */ + + if (FlexibleCollectionsIsEnabled) + { + throw new FeatureUnavailableException("Flexible Collections is ON when it should be OFF."); + } + var canCreateNewCollections = false; var org = GetOrganization(orgId); if (org != null) diff --git a/src/Notifications/NotificationsHub.cs b/src/Notifications/NotificationsHub.cs index a86cf329c5..d529ee1a06 100644 --- a/src/Notifications/NotificationsHub.cs +++ b/src/Notifications/NotificationsHub.cs @@ -18,7 +18,7 @@ public class NotificationsHub : Microsoft.AspNetCore.SignalR.Hub public override async Task OnConnectedAsync() { - var currentContext = new CurrentContext(null, null); + var currentContext = new CurrentContext(null, null, null); await currentContext.BuildAsync(Context.User, _globalSettings); if (currentContext.Organizations != null) { @@ -33,7 +33,7 @@ public class NotificationsHub : Microsoft.AspNetCore.SignalR.Hub public override async Task OnDisconnectedAsync(Exception exception) { - var currentContext = new CurrentContext(null, null); + var currentContext = new CurrentContext(null, null, null); await currentContext.BuildAsync(Context.User, _globalSettings); if (currentContext.Organizations != null) {