Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions FeatureMasterX.XUnitTest/BaseUnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@
public Task DisposeAsync() =>
Task.CompletedTask;

/// <summary>
/// Initialize the test class.
/// </summary>
/// <returns></returns>
public async Task InitializeAsync()

Check warning on line 30 in FeatureMasterX.XUnitTest/BaseUnitTest.cs

View workflow job for this annotation

GitHub Actions / run_test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 30 in FeatureMasterX.XUnitTest/BaseUnitTest.cs

View workflow job for this annotation

GitHub Actions / create_nuget

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 30 in FeatureMasterX.XUnitTest/BaseUnitTest.cs

View workflow job for this annotation

GitHub Actions / create_nuget

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
_instance = CreateServiceCollection();

Expand Down
4 changes: 3 additions & 1 deletion FeatureMasterX/FeatureMasterX.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
<PackageProjectUrl>https://github.com/SkJonko/FeatureMasterX</PackageProjectUrl>
<Title>FeatureMasterX</Title>
<Description>A powerful and easy-to-use extension for Microsoft's FeatureManagement library</Description>
<Version>9.0.1</Version>
<Version>9.0.2</Version>
<Authors>SKJonko</Authors>
<Copyright>Copyright © 2025</Copyright>
<PackageReleaseNotes>
9.0.2
- Feature: Add Scoped Dependency Injection of FeatureMasterX
9.0.1
- Feature: Multitargets
9.0.0
Expand Down
32 changes: 32 additions & 0 deletions FeatureMasterX/FeatureMasterXExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
using FeatureMasterX.Filters;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.FeatureManagement;
using System.Diagnostics.CodeAnalysis;

namespace FeatureMasterX
{
/// <summary>
/// Extensions for FeatureMasterX.
/// </summary>
public static class FeatureMasterXExtensions
{
#region PUBLIC

/// <summary>
/// The name of the feature filter that checks if the user is in a list of allowed users.
/// </summary>
public const string ListCheck = "ListCheck";

/// <summary>
/// The name of the configuration key that contains the list of allowed users.
/// </summary>
public const string AllowedUsers = "AllowedUsers";

#endregion

/// <summary>
/// Adds the FeatureMasterX services to the specified <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
[ExcludeFromCodeCoverage]
public static IServiceCollection AddFeatureMasterX(this IServiceCollection services)
{
services.AddSingleton<IFeatureDefinitionProvider, ListCheckFeatureFilterProvider>()
Expand All @@ -21,5 +38,20 @@ public static IServiceCollection AddFeatureMasterX(this IServiceCollection servi

return services;
}

/// <summary>
/// Adds the Scoped FeatureMasterX services to the specified <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
[ExcludeFromCodeCoverage]
public static IServiceCollection AddScopedFeatureMasterX(this IServiceCollection services)
{
services.AddSingleton<IFeatureDefinitionProvider, ListCheckFeatureFilterProvider>()
.AddScopedFeatureManagement()
.AddFeatureFilter<ListCheckFeatureFilter>();

return services;
}
}
}
8 changes: 4 additions & 4 deletions FeatureMasterX/Filters/ListCheckFeatureFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@
/// Evaluate the feature filter
/// </summary>
/// <param name="context"></param>
/// <param name="userEmail"></param>
/// <param name="appsettingsData">The data that stored under the EnabledFor of appsettings.</param>
/// <returns></returns>
public Task<bool> EvaluateAsync(FeatureFilterEvaluationContext context, string userEmail)
public Task<bool> EvaluateAsync(FeatureFilterEvaluationContext context, string appsettingsData)
{
var allowedUsers = JsonSerializer.Deserialize<List<string>>(context?.Parameters?.GetSection(FeatureMasterXExtensions.AllowedUsers)?.Get<string>());

Check warning on line 21 in FeatureMasterX/Filters/ListCheckFeatureFilter.cs

View workflow job for this annotation

GitHub Actions / run_test

Possible null reference argument for parameter 'json' in 'List<string>? JsonSerializer.Deserialize<List<string>>(string json, JsonSerializerOptions? options = null)'.

Check warning on line 21 in FeatureMasterX/Filters/ListCheckFeatureFilter.cs

View workflow job for this annotation

GitHub Actions / run_test

Possible null reference argument for parameter 'json' in 'List<string>? JsonSerializer.Deserialize<List<string>>(string json, JsonSerializerOptions? options = null)'.

Check warning on line 21 in FeatureMasterX/Filters/ListCheckFeatureFilter.cs

View workflow job for this annotation

GitHub Actions / run_test

Possible null reference argument for parameter 'json' in 'List<string>? JsonSerializer.Deserialize<List<string>>(string json, JsonSerializerOptions? options = null)'.

Check warning on line 21 in FeatureMasterX/Filters/ListCheckFeatureFilter.cs

View workflow job for this annotation

GitHub Actions / create_nuget

Possible null reference argument for parameter 'json' in 'List<string>? JsonSerializer.Deserialize<List<string>>(string json, JsonSerializerOptions? options = null)'.

Check warning on line 21 in FeatureMasterX/Filters/ListCheckFeatureFilter.cs

View workflow job for this annotation

GitHub Actions / create_nuget

Possible null reference argument for parameter 'json' in 'List<string>? JsonSerializer.Deserialize<List<string>>(string json, JsonSerializerOptions? options = null)'.

Check warning on line 21 in FeatureMasterX/Filters/ListCheckFeatureFilter.cs

View workflow job for this annotation

GitHub Actions / create_nuget

Possible null reference argument for parameter 'json' in 'List<string>? JsonSerializer.Deserialize<List<string>>(string json, JsonSerializerOptions? options = null)'.

Check warning on line 21 in FeatureMasterX/Filters/ListCheckFeatureFilter.cs

View workflow job for this annotation

GitHub Actions / create_nuget

Possible null reference argument for parameter 'json' in 'List<string>? JsonSerializer.Deserialize<List<string>>(string json, JsonSerializerOptions? options = null)'.

Check warning on line 21 in FeatureMasterX/Filters/ListCheckFeatureFilter.cs

View workflow job for this annotation

GitHub Actions / create_nuget

Possible null reference argument for parameter 'json' in 'List<string>? JsonSerializer.Deserialize<List<string>>(string json, JsonSerializerOptions? options = null)'.

Check warning on line 21 in FeatureMasterX/Filters/ListCheckFeatureFilter.cs

View workflow job for this annotation

GitHub Actions / create_nuget

Possible null reference argument for parameter 'json' in 'List<string>? JsonSerializer.Deserialize<List<string>>(string json, JsonSerializerOptions? options = null)'.

if (allowedUsers?.Contains("ALL") ?? false)
return Task.FromResult(true);

if (string.IsNullOrEmpty(userEmail))
if (string.IsNullOrEmpty(appsettingsData))
return Task.FromResult(false);

if (allowedUsers?.Contains(userEmail) ?? false)
if (allowedUsers?.Contains(appsettingsData) ?? false)
return Task.FromResult(true);

return Task.FromResult(false);
Expand Down
15 changes: 11 additions & 4 deletions FeatureMasterX/Filters/ListCheckFeatureFilterProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace FeatureMasterX.Filters
{
/// <summary>
/// Provides a feature filter that checks if the user is in a list of allowed users.
/// </summary>
public class ListCheckFeatureFilterProvider : IFeatureDefinitionProvider
{
#region Private Values
Expand All @@ -23,6 +26,7 @@ public ListCheckFeatureFilterProvider(IConfiguration configuration)
_configuration = configuration;
}

/// <inheritdoc/>
public Task<FeatureDefinition> GetFeatureDefinitionAsync(string featureName)
{
var section = _configuration.GetSection($"FeatureManagement:{FeatureMasterXExtensions.ListCheck}:{featureName}:EnabledFor");
Expand All @@ -32,7 +36,7 @@ public Task<FeatureDefinition> GetFeatureDefinitionAsync(string featureName)
return Task.FromResult(new FeatureDefinition
{
Name = featureName,
EnabledFor = allowedUsers.Select(user => new FeatureFilterConfiguration
EnabledFor = [.. allowedUsers.Select(user => new FeatureFilterConfiguration
{
Name = FeatureMasterXExtensions.ListCheck,
Parameters = new ConfigurationBuilder()
Expand All @@ -41,11 +45,14 @@ public Task<FeatureDefinition> GetFeatureDefinitionAsync(string featureName)
{ FeatureMasterXExtensions.AllowedUsers, JsonSerializer.Serialize(allowedUsers) }
})
.Build()
}).ToList()
})]
});
}

/// <inheritdoc/>
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
public async IAsyncEnumerable<FeatureDefinition> GetAllFeatureDefinitionsAsync()
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
{
var featureManagementSection = _configuration.GetSection("FeatureManagement");

Expand All @@ -58,7 +65,7 @@ public async IAsyncEnumerable<FeatureDefinition> GetAllFeatureDefinitionsAsync()
yield return new FeatureDefinition
{
Name = feature.Key,
EnabledFor = allowedUsers.Select(user => new FeatureFilterConfiguration
EnabledFor = [.. allowedUsers.Select(user => new FeatureFilterConfiguration
{
Name = FeatureMasterXExtensions.ListCheck,
Parameters = new ConfigurationBuilder()
Expand All @@ -67,7 +74,7 @@ public async IAsyncEnumerable<FeatureDefinition> GetAllFeatureDefinitionsAsync()
{ FeatureMasterXExtensions.AllowedUsers, JsonSerializer.Serialize(allowedUsers) }
})
.Build()
}).ToList()
})]
};
}
}
Expand Down
Loading