using System;
using System.Threading.Tasks;
using Abp.Authorization;
using Abp.Collections.Extensions;
using Abp.Runtime.Session;
using Abp.Threading;
namespace Abp.Application.Features
{
///
/// Some extension methods for .
///
public static class FeatureCheckerExtensions
{
///
/// Gets the value of a feature by its name. This is the sync version of
///
/// This is a shortcut for that uses .
/// Note: This method should be used only if the TenantId can be obtained from the session.
///
/// instance
/// Unique feature name
/// Feature's current value
public static string GetValue(this IFeatureChecker featureChecker, string featureName)
{
return AsyncHelper.RunSync(() => featureChecker.GetValueAsync(featureName));
}
///
/// Gets the value of a feature by its name. This is the sync version of
///
/// instance
/// Tenant's Id
/// Unique feature name
/// Feature's current value
public static string GetValue(this IFeatureChecker featureChecker, int tenantId, string featureName)
{
return AsyncHelper.RunSync(() => featureChecker.GetValueAsync(tenantId, featureName));
}
///
/// Checks if a given feature is enabled.
/// This should be used for boolean-value features.
///
/// This is a shortcut for that uses .
/// Note: This method should be used only if the TenantId can be obtained from the session.
///
/// instance
/// Unique feature name
/// True, if the current feature's value is "true".
public static async Task IsEnabledAsync(this IFeatureChecker featureChecker, string featureName)
{
return string.Equals(await featureChecker.GetValueAsync(featureName), "true", StringComparison.OrdinalIgnoreCase);
}
///
/// Checks if a given feature is enabled.
/// This should be used for boolean-value features.
///
/// instance
/// Tenant's Id
/// Unique feature name
/// True, if the current feature's value is "true".
public static async Task IsEnabledAsync(this IFeatureChecker featureChecker, int tenantId, string featureName)
{
return string.Equals(await featureChecker.GetValueAsync(tenantId, featureName), "true", StringComparison.OrdinalIgnoreCase);
}
///
/// Checks if a given feature is enabled.
/// This should be used for boolean-value features.
///
/// This is a shortcut for that uses .
/// Note: This method should be used only if the TenantId can be obtained from the session.
///
/// instance
/// Unique feature name
/// True, if the current feature's value is "true".
public static bool IsEnabled(this IFeatureChecker featureChecker, string name)
{
return AsyncHelper.RunSync(() => featureChecker.IsEnabledAsync(name));
}
///
/// Checks if a given feature is enabled.
/// This should be used for boolean-value features.
///
/// instance
/// Tenant's Id
/// Unique feature name
/// True, if the current feature's value is "true".
public static bool IsEnabled(this IFeatureChecker featureChecker, int tenantId, string featureName)
{
return AsyncHelper.RunSync(() => featureChecker.IsEnabledAsync(tenantId, featureName));
}
///
/// Used to check if one or all of the given features are enabled.
///
/// instance
/// True, to require that all the given features are enabled. False, to require one or more.
/// Names of the features
public static async Task IsEnabledAsync(this IFeatureChecker featureChecker, bool requiresAll, params string[] featureNames)
{
if (featureNames.IsNullOrEmpty())
{
return true;
}
if (requiresAll)
{
foreach (var featureName in featureNames)
{
if (!(await featureChecker.IsEnabledAsync(featureName)))
{
return false;
}
}
return true;
}
foreach (var featureName in featureNames)
{
if (await featureChecker.IsEnabledAsync(featureName))
{
return true;
}
}
return false;
}
///
/// Used to check if one or all of the given features are enabled.
///
/// instance
/// Tenant id
/// True, to require that all the given features are enabled. False, to require one or more.
/// Names of the features
public static async Task IsEnabledAsync(this IFeatureChecker featureChecker, int tenantId, bool requiresAll, params string[] featureNames)
{
if (featureNames.IsNullOrEmpty())
{
return true;
}
if (requiresAll)
{
foreach (var featureName in featureNames)
{
if (!(await featureChecker.IsEnabledAsync(tenantId, featureName)))
{
return false;
}
}
return true;
}
foreach (var featureName in featureNames)
{
if (await featureChecker.IsEnabledAsync(tenantId, featureName))
{
return true;
}
}
return false;
}
///
/// Used to check if one or all of the given features are enabled.
///
/// instance
/// True, to require that all the given features are enabled. False, to require one or more.
/// Names of the features
public static bool IsEnabled(this IFeatureChecker featureChecker, bool requiresAll, params string[] featureNames)
{
return AsyncHelper.RunSync(() => featureChecker.IsEnabledAsync(requiresAll, featureNames));
}
///
/// Used to check if one or all of the given features are enabled.
///
/// instance
/// Tenant id
/// True, to require that all the given features are enabled. False, to require one or more.
/// Names of the features
public static bool IsEnabled(this IFeatureChecker featureChecker, int tenantId, bool requiresAll, params string[] featureNames)
{
return AsyncHelper.RunSync(() => featureChecker.IsEnabledAsync(tenantId, requiresAll, featureNames));
}
///
/// Checks if a given feature is enabled. Throws if not.
///
/// instance
/// Unique feature name
public static async Task CheckEnabledAsync(this IFeatureChecker featureChecker, string featureName)
{
if (!(await featureChecker.IsEnabledAsync(featureName)))
{
throw new AbpAuthorizationException("Feature is not enabled: " + featureName);
}
}
///
/// Checks if a given feature is enabled. Throws if not.
///
/// instance
/// Unique feature name
public static void CheckEnabled(this IFeatureChecker featureChecker, string featureName)
{
if (!featureChecker.IsEnabled(featureName))
{
throw new AbpAuthorizationException("Feature is not enabled: " + featureName);
}
}
///
/// Checks if one or all of the given features are enabled. Throws if not.
///
/// instance
/// True, to require that all the given features are enabled. False, to require one or more.
/// Names of the features
public static async Task CheckEnabledAsync(this IFeatureChecker featureChecker, bool requiresAll, params string[] featureNames)
{
if (featureNames.IsNullOrEmpty())
{
return;
}
if (requiresAll)
{
foreach (var featureName in featureNames)
{
if (!(await featureChecker.IsEnabledAsync(featureName)))
{
throw new AbpAuthorizationException(
"Required features are not enabled. All of these features must be enabled: " +
string.Join(", ", featureNames)
);
}
}
}
else
{
foreach (var featureName in featureNames)
{
if (await featureChecker.IsEnabledAsync(featureName))
{
return;
}
}
throw new AbpAuthorizationException(
"Required features are not enabled. At least one of these features must be enabled: " +
string.Join(", ", featureNames)
);
}
}
///
/// Checks if one or all of the given features are enabled. Throws if not.
///
/// instance
/// Tenant id
/// True, to require that all the given features are enabled. False, to require one or more.
/// Names of the features
public static async Task CheckEnabledAsync(this IFeatureChecker featureChecker, int tenantId, bool requiresAll, params string[] featureNames)
{
if (featureNames.IsNullOrEmpty())
{
return;
}
if (requiresAll)
{
foreach (var featureName in featureNames)
{
if (!(await featureChecker.IsEnabledAsync(tenantId, featureName)))
{
throw new AbpAuthorizationException(
"Required features are not enabled. All of these features must be enabled: " +
string.Join(", ", featureNames)
);
}
}
}
else
{
foreach (var featureName in featureNames)
{
if (await featureChecker.IsEnabledAsync(tenantId, featureName))
{
return;
}
}
throw new AbpAuthorizationException(
"Required features are not enabled. At least one of these features must be enabled: " +
string.Join(", ", featureNames)
);
}
}
///
/// Checks if one or all of the given features are enabled. Throws if not.
///
/// instance
/// True, to require that all the given features are enabled. False, to require one or more.
/// Names of the features
public static void CheckEnabled(this IFeatureChecker featureChecker, bool requiresAll, params string[] featureNames)
{
AsyncHelper.RunSync(() => featureChecker.CheckEnabledAsync(requiresAll, featureNames));
}
///
/// Checks if one or all of the given features are enabled. Throws if not.
///
/// instance
/// Tenant id
/// True, to require that all the given features are enabled. False, to require one or more.
/// Names of the features
public static void CheckEnabled(this IFeatureChecker featureChecker, int tenantId, bool requiresAll, params string[] featureNames)
{
AsyncHelper.RunSync(() => featureChecker.CheckEnabledAsync(tenantId, requiresAll, featureNames));
}
}
}