using Bit.Core.AdminConsole.Enums.Provider; using Bit.Core.AdminConsole.Models.Data.Provider; using Bit.Core.AdminConsole.Repositories; namespace Bit.Api.AdminConsole.Authorization; public static class ProviderOrganizationHttpContextFeature { /// /// Returns the ProviderUserOrganizations for a user. These are the organizations the ProviderUser manages via their Provider, if any. /// This data is fetched from the database and cached as a HttpContext Feature for the lifetime of the request. /// /// /// /// /// private static async Task> GetProviderUserOrganizationsAsync( this HttpContext httpContext, IProviderUserRepository providerUserRepository, Guid userId) { var providerUserOrganizations = httpContext.Features.Get>(); if (providerUserOrganizations != null) { return providerUserOrganizations; } providerUserOrganizations = (await providerUserRepository.GetManyOrganizationDetailsByUserAsync( userId, ProviderUserStatusType.Confirmed)).ToList(); httpContext.Features.Set(providerUserOrganizations); return providerUserOrganizations; } /// /// Returns true if the user is a ProviderUser for a Provider which manages the specified organization, otherwise false. /// This data is fetched from the database and cached as a HttpContext Feature for the lifetime of the request. /// public static async Task IsProviderUserForOrgAsync( this HttpContext httpContext, IProviderUserRepository providerUserRepository, Guid userId, Guid organizationId) { var organizations = await httpContext.GetProviderUserOrganizationsAsync(providerUserRepository, userId); return organizations.Any(o => o.OrganizationId == organizationId); } }