Selector/Selector.Model/Authorisation/User/UserIsAdminAuthHandler.cs

35 lines
963 B
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization.Infrastructure;
using Microsoft.AspNetCore.Identity;
namespace Selector.Model.Authorisation
{
public class UserIsAdminAuthHandler
: AuthorizationHandler<OperationAuthorizationRequirement, ApplicationUser>
{
protected override Task HandleRequirementAsync(
AuthorizationHandlerContext context,
OperationAuthorizationRequirement requirement,
ApplicationUser resource
) {
if (context.User == null || resource == null)
{
return Task.CompletedTask;
}
if (context.User.IsInRole(Constants.AdminRole))
{
context.Succeed(requirement);
}
return Task.CompletedTask;
}
}
}