1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-03 17:12:49 -05:00

[AC-1139] Created AuthorizationServiceExtensions to have an extension method for AuthorizeAsync where the resource is null

This commit is contained in:
Rui Tome
2023-10-30 12:59:34 +00:00
parent 91de2d892e
commit 4642b7360e
4 changed files with 39 additions and 4 deletions

View File

@ -0,0 +1,32 @@
using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
namespace Bit.Api.Utilities;
public static class AuthorizationServiceExtensions
{
/// <summary>
/// Checks if a user meets a specific requirement.
/// </summary>
/// <param name="service">The <see cref="IAuthorizationService"/> providing authorization.</param>
/// <param name="user">The user to evaluate the policy against.</param>
/// <param name="requirement">The requirement to evaluate the policy against.</param>
/// <returns>
/// A flag indicating whether requirement evaluation has succeeded or failed.
/// This value is <value>true</value> when the user fulfills the policy, otherwise <value>false</value>.
/// </returns>
public static Task<AuthorizationResult> AuthorizeAsync(this IAuthorizationService service, ClaimsPrincipal user, IAuthorizationRequirement requirement)
{
if (service == null)
{
throw new ArgumentNullException(nameof(service));
}
if (requirement == null)
{
throw new ArgumentNullException(nameof(requirement));
}
return service.AuthorizeAsync(user, resource: null, new[] { requirement });
}
}