Selector/Selector.Model/Authorisation/Watcher/WatcherIsAdminAuthHandler.cs

35 lines
951 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 WatcherIsAdminAuthHandler
: AuthorizationHandler<OperationAuthorizationRequirement, Watcher>
{
protected override Task HandleRequirementAsync(
AuthorizationHandlerContext context,
OperationAuthorizationRequirement requirement,
Watcher resource
) {
if (context.User == null || resource == null)
{
return Task.CompletedTask;
}
if (context.User.IsInRole(Constants.AdminRole))
{
context.Succeed(requirement);
}
return Task.CompletedTask;
}
}
}