// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Linq;
using System.Threading.Tasks;
namespace Microsoft.AspNetCore.Authorization
{
///
/// Base class for authorization handlers that need to be called for a specific requirement type.
///
/// The type of the requirement to handle.
public abstract class AuthorizationHandler : IAuthorizationHandler
where TRequirement : IAuthorizationRequirement
{
///
/// Makes a decision if authorization is allowed.
///
/// The authorization context.
public virtual async Task HandleAsync(AuthorizationHandlerContext context)
{
foreach (var req in context.Requirements.OfType())
{
await HandleRequirementAsync(context, req);
}
}
///
/// Makes a decision if authorization is allowed based on a specific requirement.
///
/// The authorization context.
/// The requirement to evaluate.
protected abstract Task HandleRequirementAsync(AuthorizationHandlerContext context, TRequirement requirement);
}
///
/// Base class for authorization handlers that need to be called for specific requirement and
/// resource types.
///
/// The type of the requirement to evaluate.
/// The type of the resource to evaluate.
public abstract class AuthorizationHandler : IAuthorizationHandler
where TRequirement : IAuthorizationRequirement
{
///
/// Makes a decision if authorization is allowed.
///
/// The authorization context.
public virtual async Task HandleAsync(AuthorizationHandlerContext context)
{
if (context.Resource is TResource)
{
foreach (var req in context.Requirements.OfType())
{
await HandleRequirementAsync(context, req, (TResource)context.Resource);
}
}
}
///
/// Makes a decision if authorization is allowed based on a specific requirement and resource.
///
/// The authorization context.
/// The requirement to evaluate.
/// The resource to evaluate.
protected abstract Task HandleRequirementAsync(AuthorizationHandlerContext context, TRequirement requirement, TResource resource);
}
}