mirror of
https://github.com/bitwarden/server
synced 2026-01-15 06:53:26 +00:00
Refactor IPremiumAccessQuery and PremiumAccessQuery to remove the overloaded CanAccessPremiumAsync method. Update related methods to streamline premium access checks using the User object directly. Enhance test coverage by removing obsolete tests and ensuring proper functionality with the new method signatures.
This commit is contained in:
@@ -22,15 +22,6 @@ public interface IPremiumAccessQuery
|
||||
/// <returns>True if user can access premium features; false otherwise</returns>
|
||||
Task<bool> CanAccessPremiumAsync(User user);
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a user has access to premium features (personal subscription or organization).
|
||||
/// Use this overload when you already know the personal premium status and only need to check organization premium.
|
||||
/// </summary>
|
||||
/// <param name="userId">The user ID to check for premium access</param>
|
||||
/// <param name="hasPersonalPremium">Whether the user has a personal premium subscription</param>
|
||||
/// <returns>True if user can access premium features; false otherwise</returns>
|
||||
Task<bool> CanAccessPremiumAsync(Guid userId, bool hasPersonalPremium);
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a user has access to premium features through organization membership only.
|
||||
/// This is useful for determining the source of premium access (personal vs organization).
|
||||
|
||||
@@ -22,17 +22,12 @@ public class PremiumAccessQuery : IPremiumAccessQuery
|
||||
|
||||
public async Task<bool> CanAccessPremiumAsync(User user)
|
||||
{
|
||||
return await CanAccessPremiumAsync(user.Id, user.Premium);
|
||||
}
|
||||
|
||||
public async Task<bool> CanAccessPremiumAsync(Guid userId, bool hasPersonalPremium)
|
||||
{
|
||||
if (hasPersonalPremium)
|
||||
if (user.Premium)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return await HasPremiumFromOrganizationAsync(userId);
|
||||
return await HasPremiumFromOrganizationAsync(user.Id);
|
||||
}
|
||||
|
||||
public async Task<bool> HasPremiumFromOrganizationAsync(Guid userId)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Bit.Core.Auth.Models;
|
||||
using Bit.Core.Entities;
|
||||
|
||||
namespace Bit.Core.Auth.UserFeatures.TwoFactorAuth.Interfaces;
|
||||
|
||||
@@ -42,5 +43,5 @@ public interface ITwoFactorIsEnabledQuery
|
||||
/// This version uses PremiumAccessQuery with cached organization abilities for better performance.
|
||||
/// </summary>
|
||||
/// <param name="user">The user to check.</param>
|
||||
Task<bool> TwoFactorIsEnabledVNextAsync(ITwoFactorProvidersUser user);
|
||||
Task<bool> TwoFactorIsEnabledVNextAsync(User user);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ using Bit.Core.Auth.Models;
|
||||
using Bit.Core.Auth.UserFeatures.PremiumAccess;
|
||||
using Bit.Core.Auth.UserFeatures.TwoFactorAuth.Interfaces;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.Models.Data.Organizations.OrganizationUsers;
|
||||
using Bit.Core.Repositories;
|
||||
|
||||
namespace Bit.Core.Auth.UserFeatures.TwoFactorAuth;
|
||||
@@ -147,38 +146,11 @@ public class TwoFactorIsEnabledQuery : ITwoFactorIsEnabledQuery
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<bool> TwoFactorIsEnabledVNextAsync(ITwoFactorProvidersUser user)
|
||||
public async Task<bool> TwoFactorIsEnabledVNextAsync(User user)
|
||||
{
|
||||
var userId = user.GetUserId();
|
||||
if (!userId.HasValue)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Try to get premium status without fetching User entity if possible
|
||||
bool hasPersonalPremium;
|
||||
if (user is User userEntity)
|
||||
{
|
||||
hasPersonalPremium = userEntity.Premium;
|
||||
}
|
||||
else if (user is OrganizationUserUserDetails orgUserDetails)
|
||||
{
|
||||
hasPersonalPremium = orgUserDetails.Premium.GetValueOrDefault(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Fallback: fetch the User entity
|
||||
var fetchedUser = await _userRepository.GetByIdAsync(userId.Value);
|
||||
if (fetchedUser == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
hasPersonalPremium = fetchedUser.Premium;
|
||||
}
|
||||
|
||||
return await TwoFactorEnabledAsync(
|
||||
user.GetTwoFactorProviders(),
|
||||
async () => await _premiumAccessQuery.CanAccessPremiumAsync(userId.Value, hasPersonalPremium));
|
||||
async () => await _premiumAccessQuery.CanAccessPremiumAsync(user));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user