ln.http/ln.http/RoleAuthorization.cs

38 lines
1.2 KiB
C#
Raw Permalink Normal View History

2022-02-07 09:29:30 +01:00
using System.Reflection;
using ln.http.router;
namespace ln.http
{
public static class RoleAuthorization
{
2022-05-28 19:25:25 +02:00
public static HttpAuthorizationDelegate Require(string roleName)
2022-02-07 09:29:30 +01:00
{
2022-05-28 19:25:25 +02:00
return context => context.AuthenticatedPrincipal?.HasRole(roleName) ?? false;
2022-02-07 09:29:30 +01:00
}
public static HttpAuthorizationDelegate RequireAll(params HttpAuthorizationDelegate[] authorizationDelegates)
{
return context =>
{
foreach (HttpAuthorizationDelegate authorizationDelegate in authorizationDelegates)
{
if (!authorizationDelegate(context))
return false;
}
return true;
};
}
public static HttpAuthorizationDelegate RequireOneOf(params HttpAuthorizationDelegate[] authorizationDelegates)
{
return context =>
{
foreach (HttpAuthorizationDelegate authorizationDelegate in authorizationDelegates)
{
if (authorizationDelegate(context))
return true;
}
return false;
};
}
}
}