using System;
using System.Linq;
using System.Threading.Tasks;
using Abp.Collections.Extensions;
using Abp.Dependency;
using Abp.Localization;
using Abp.Threading;
namespace Abp.Authorization
{
///
/// Extension methods for
///
public static class PermissionCheckerExtensions
{
///
/// Checks if current user is granted for a permission.
///
/// Permission checker
/// Name of the permission
public static bool IsGranted(this IPermissionChecker permissionChecker, string permissionName)
{
return AsyncHelper.RunSync(() => permissionChecker.IsGrantedAsync(permissionName));
}
///
/// Checks if a user is granted for a permission.
///
/// Permission checker
/// User to check
/// Name of the permission
public static bool IsGranted(this IPermissionChecker permissionChecker, UserIdentifier user, string permissionName)
{
return AsyncHelper.RunSync(() => permissionChecker.IsGrantedAsync(user, permissionName));
}
///
/// Checks if given user is granted for given permission.
///
/// Permission checker
/// User
/// True, to require all given permissions are granted. False, to require one or more.
/// Name of the permissions
public static bool IsGranted(this IPermissionChecker permissionChecker, UserIdentifier user, bool requiresAll, params string[] permissionNames)
{
return AsyncHelper.RunSync(() => IsGrantedAsync(permissionChecker, user, requiresAll, permissionNames));
}
///
/// Checks if given user is granted for given permission.
///
/// Permission checker
/// User
/// True, to require all given permissions are granted. False, to require one or more.
/// Name of the permissions
public static async Task IsGrantedAsync(this IPermissionChecker permissionChecker, UserIdentifier user, bool requiresAll, params string[] permissionNames)
{
if (permissionNames.IsNullOrEmpty())
{
return true;
}
if (requiresAll)
{
foreach (var permissionName in permissionNames)
{
if (!(await permissionChecker.IsGrantedAsync(user, permissionName)))
{
return false;
}
}
return true;
}
else
{
foreach (var permissionName in permissionNames)
{
if (await permissionChecker.IsGrantedAsync(user, permissionName))
{
return true;
}
}
return false;
}
}
///
/// Checks if current user is granted for given permission.
///
/// Permission checker
/// True, to require all given permissions are granted. False, to require one or more.
/// Name of the permissions
public static bool IsGranted(this IPermissionChecker permissionChecker, bool requiresAll, params string[] permissionNames)
{
return AsyncHelper.RunSync(() => IsGrantedAsync(permissionChecker, requiresAll, permissionNames));
}
///
/// Checks if current user is granted for given permission.
///
/// Permission checker
/// True, to require all given permissions are granted. False, to require one or more.
/// Name of the permissions
public static async Task IsGrantedAsync(this IPermissionChecker permissionChecker, bool requiresAll, params string[] permissionNames)
{
if (permissionNames.IsNullOrEmpty())
{
return true;
}
if (requiresAll)
{
foreach (var permissionName in permissionNames)
{
if (!(await permissionChecker.IsGrantedAsync(permissionName)))
{
return false;
}
}
return true;
}
else
{
foreach (var permissionName in permissionNames)
{
if (await permissionChecker.IsGrantedAsync(permissionName))
{
return true;
}
}
return false;
}
}
///
/// Authorizes current user for given permission or permissions,
/// throws if not authorized.
/// User it authorized if any of the are granted.
///
/// Permission checker
/// Name of the permissions to authorize
/// Throws authorization exception if
public static void Authorize(this IPermissionChecker permissionChecker, params string[] permissionNames)
{
Authorize(permissionChecker, false, permissionNames);
}
///
/// Authorizes current user for given permission or permissions,
/// throws if not authorized.
/// User it authorized if any of the are granted.
///
/// Permission checker
///
/// If this is set to true, all of the must be granted.
/// If it's false, at least one of the must be granted.
///
/// Name of the permissions to authorize
/// Throws authorization exception if
public static void Authorize(this IPermissionChecker permissionChecker, bool requireAll, params string[] permissionNames)
{
AsyncHelper.RunSync(() => AuthorizeAsync(permissionChecker, requireAll, permissionNames));
}
///
/// Authorizes current user for given permission or permissions,
/// throws if not authorized.
/// User it authorized if any of the are granted.
///
/// Permission checker
/// Name of the permissions to authorize
/// Throws authorization exception if
public static Task AuthorizeAsync(this IPermissionChecker permissionChecker, params string[] permissionNames)
{
return AuthorizeAsync(permissionChecker, false, permissionNames);
}
///
/// Authorizes current user for given permission or permissions,
/// throws if not authorized.
///
/// Permission checker
///
/// If this is set to true, all of the must be granted.
/// If it's false, at least one of the must be granted.
///
/// Name of the permissions to authorize
/// Throws authorization exception if
public static async Task AuthorizeAsync(this IPermissionChecker permissionChecker, bool requireAll, params string[] permissionNames)
{
if (await IsGrantedAsync(permissionChecker, requireAll, permissionNames))
{
return;
}
var localizedPermissionNames = LocalizePermissionNames(permissionChecker, permissionNames);
if (requireAll)
{
throw new AbpAuthorizationException(
string.Format(
L(
permissionChecker,
"AllOfThesePermissionsMustBeGranted",
"Required permissions are not granted. All of these permissions must be granted: {0}"
),
string.Join(", ", localizedPermissionNames)
)
);
}
else
{
throw new AbpAuthorizationException(
string.Format(
L(
permissionChecker,
"AtLeastOneOfThesePermissionsMustBeGranted",
"Required permissions are not granted. At least one of these permissions must be granted: {0}"
),
string.Join(", ", localizedPermissionNames)
)
);
}
}
public static string L(IPermissionChecker permissionChecker, string name, string defaultValue)
{
if (!(permissionChecker is IIocManagerAccessor))
{
return defaultValue;
}
var iocManager = (permissionChecker as IIocManagerAccessor).IocManager;
using (var localizationManager = iocManager.ResolveAsDisposable())
{
return localizationManager.Object.GetString(AbpConsts.LocalizationSourceName, name);
}
}
public static string[] LocalizePermissionNames(IPermissionChecker permissionChecker, string[] permissionNames)
{
if (!(permissionChecker is IIocManagerAccessor))
{
return permissionNames;
}
var iocManager = (permissionChecker as IIocManagerAccessor).IocManager;
using (var localizationContext = iocManager.ResolveAsDisposable())
{
using (var permissionManager = iocManager.ResolveAsDisposable())
{
return permissionNames.Select(permissionName =>
{
var permission = permissionManager.Object.GetPermissionOrNull(permissionName);
return permission?.DisplayName == null
? permissionName
: permission.DisplayName.Localize(localizationContext.Object);
}).ToArray();
}
}
}
}
}