1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2024-11-22 08:42:44 +01:00
Minecraft-Phantom-Panel/Server/Phantom.Server.Web.Identity/Authorization/PermissionBasedPolicyHandler.cs

23 lines
789 B
C#

using Microsoft.AspNetCore.Authorization;
namespace Phantom.Server.Web.Identity.Authorization;
sealed class PermissionBasedPolicyHandler : AuthorizationHandler<PermissionBasedPolicyRequirement> {
private readonly PermissionManager permissionManager;
public PermissionBasedPolicyHandler(PermissionManager permissionManager) {
this.permissionManager = permissionManager;
}
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, PermissionBasedPolicyRequirement requirement) {
if (permissionManager.CheckPermission(context.User, requirement.Permission)) {
context.Succeed(requirement);
}
else {
context.Fail(new AuthorizationFailureReason(this, "Missing permission: " + requirement.Permission.Id));
}
return Task.CompletedTask;
}
}