Skip to content

Commit

Permalink
feat: Add generic casbin authorize attribute
Browse files Browse the repository at this point in the history
Signed-off-by: sagilio <sagilio@outlook.com>
  • Loading branch information
sagilio committed Jul 31, 2022
1 parent 5e816c3 commit b1bb3e7
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace Casbin.AspNetCore.Authorization
{
public abstract class CasbinAuthorizeBaseAttribute : Attribute
{
public virtual string? Issuer { get; set; }
public virtual string? PreferSubClaimType { get; set; }
public virtual Type? RequestTransformerType { get; set; }
public virtual string? AuthenticationSchemes { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<IsPackable>true</IsPackable>
<PackageTags>aspnetcore;authorization;security</PackageTags>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<RootNamespace>Casbin.AspNetCore.Authorization</RootNamespace>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
using System;
using Casbin.Model;

namespace Casbin.AspNetCore.Authorization;

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class CasbinAuthorizeAttribute<T> : CasbinAuthorizeBaseAttribute, ICasbinAuthorizationData<RequestValues<T>>
{
private RequestValues<T> _values;
public CasbinAuthorizeAttribute(T value)
{
_values = Request.CreateValues(value);
}
public ref RequestValues<T> Values => ref _values;
}

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class CasbinAuthorizeAttribute<T1, T2> : CasbinAuthorizeBaseAttribute, ICasbinAuthorizationData<RequestValues<T1, T2>>
{
private RequestValues<T1, T2> _values;
public CasbinAuthorizeAttribute(T1 value1, T2 value2)
{
_values = Request.CreateValues(value1, value2);
}
public ref RequestValues<T1, T2> Values => ref _values;
}

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class CasbinAuthorizeAttribute<T1, T2, T3> : CasbinAuthorizeBaseAttribute, ICasbinAuthorizationData<RequestValues<T1, T2, T3>>
{
private RequestValues<T1, T2, T3> _values;
public CasbinAuthorizeAttribute(T1 value1, T2 value2, T3 value3)
{
_values = Request.CreateValues(value1, value2, value3);
}
public ref RequestValues<T1, T2, T3> Values => ref _values;
}

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class CasbinAuthorizeAttribute<T1, T2, T3, T4> : CasbinAuthorizeBaseAttribute, ICasbinAuthorizationData<RequestValues<T1, T2, T3, T4>>
{
private RequestValues<T1, T2, T3, T4> _values;
public CasbinAuthorizeAttribute(T1 value1, T2 value2, T3 value3, T4 value4)
{
_values = Request.CreateValues(value1, value2, value3, value4);
}
public ref RequestValues<T1, T2, T3, T4> Values => ref _values;
}

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class CasbinAuthorizeAttribute<T1, T2, T3, T4, T5> : CasbinAuthorizeBaseAttribute, ICasbinAuthorizationData<RequestValues<T1, T2, T3, T4, T5>>
{
private RequestValues<T1, T2, T3, T4, T5> _values;
public CasbinAuthorizeAttribute(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5)
{
_values = Request.CreateValues(value1, value2, value3, value4, value5);
}
public ref RequestValues<T1, T2, T3, T4, T5> Values => ref _values;
}

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class CasbinAuthorizeAttribute<T1, T2, T3, T4, T5, T6> : CasbinAuthorizeBaseAttribute, ICasbinAuthorizationData<RequestValues<T1, T2, T3, T4, T5, T6>>
{
private RequestValues<T1, T2, T3, T4, T5, T6> _values;
public CasbinAuthorizeAttribute(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6)
{
_values = Request.CreateValues(value1, value2, value3, value4, value5, value6);
}
public ref RequestValues<T1, T2, T3, T4, T5, T6> Values => ref _values;
}

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class CasbinAuthorizeAttribute<T1, T2, T3, T4, T5, T6, T7> : CasbinAuthorizeBaseAttribute, ICasbinAuthorizationData<RequestValues<T1, T2, T3, T4, T5, T6, T7>>
{
private RequestValues<T1, T2, T3, T4, T5, T6, T7> _values;
public CasbinAuthorizeAttribute(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7)
{
_values = Request.CreateValues(value1, value2, value3, value4, value5, value6, value7);
}
public ref RequestValues<T1, T2, T3, T4, T5, T6, T7> Values => ref _values;
}

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class CasbinAuthorizeAttribute<T1, T2, T3, T4, T5, T6, T7, T8> : CasbinAuthorizeBaseAttribute, ICasbinAuthorizationData<RequestValues<T1, T2, T3, T4, T5, T6, T7, T8>>
{
private RequestValues<T1, T2, T3, T4, T5, T6, T7, T8> _values;
public CasbinAuthorizeAttribute(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7, T8 value8)
{
_values = Request.CreateValues(value1, value2, value3, value4, value5, value6, value7, value8);
}
public ref RequestValues<T1, T2, T3, T4, T5, T6, T7, T8> Values => ref _values;
}

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class CasbinAuthorizeAttribute<T1, T2, T3, T4, T5, T6, T7, T8, T9> : CasbinAuthorizeBaseAttribute, ICasbinAuthorizationData<RequestValues<T1, T2, T3, T4, T5, T6, T7, T8, T9>>
{
private RequestValues<T1, T2, T3, T4, T5, T6, T7, T8, T9> _values;
public CasbinAuthorizeAttribute(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7, T8 value8, T9 value9)
{
_values = Request.CreateValues(value1, value2, value3, value4, value5, value6, value7, value8, value9);
}
public ref RequestValues<T1, T2, T3, T4, T5, T6, T7, T8, T9> Values => ref _values;
}

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class CasbinAuthorizeAttribute<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> : CasbinAuthorizeBaseAttribute, ICasbinAuthorizationData<RequestValues<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>>
{
private RequestValues<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> _values;
public CasbinAuthorizeAttribute(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7, T8 value8, T9 value9, T10 value10)
{
_values = Request.CreateValues(value1, value2, value3, value4, value5, value6, value7, value8, value9, value10);
}
public ref RequestValues<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> Values => ref _values;
}

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class CasbinAuthorizeAttribute<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> : CasbinAuthorizeBaseAttribute, ICasbinAuthorizationData<RequestValues<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>>
{
private RequestValues<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> _values;
public CasbinAuthorizeAttribute(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7, T8 value8, T9 value9, T10 value10, T11 value11)
{
_values = Request.CreateValues(value1, value2, value3, value4, value5, value6, value7, value8, value9, value10, value11);
}
public ref RequestValues<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> Values => ref _values;
}

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class CasbinAuthorizeAttribute<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> : CasbinAuthorizeBaseAttribute, ICasbinAuthorizationData<RequestValues<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>>
{
private RequestValues<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> _values;
public CasbinAuthorizeAttribute(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5, T6 value6, T7 value7, T8 value8, T9 value9, T10 value10, T11 value11, T12 value12)
{
_values = Request.CreateValues(value1, value2, value3, value4, value5, value6, value7, value8, value9, value10, value11, value12);
}
public ref RequestValues<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> Values => ref _values;
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Casbin.AspNetCore.Authorization;
/// Specifies that the class or method that this attribute is applied to requires the specified authorization.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class CasbinAuthorizeAttribute : Attribute, ICasbinAuthorizationData<RequestValues<string, string, string, string, string>>
public class CasbinAuthorizeAttribute : CasbinAuthorizeBaseAttribute, ICasbinAuthorizationData<RequestValues<string, string, string, string, string>>
{
private RequestValues<string, string, string, string, string> _values;
public CasbinAuthorizeAttribute()
Expand Down Expand Up @@ -41,8 +41,4 @@ public CasbinAuthorizeAttribute(string value1, string value2, string value3, str
}

public ref RequestValues<string, string, string, string, string> Values => ref _values;
public string? Issuer { get; set; }
public string? PreferSubClaimType { get; set; }
public Type? RequestTransformerType { get; set; }
public string? AuthenticationSchemes { get; set; }
}
2 changes: 1 addition & 1 deletion src/Casbin.AspNetCore/Policy/CasbinPolicyCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private static void AddAuthenticationSchemes(ICollection<string> authenticationS
return;
}

foreach (var authType in authTypesSplit)
foreach (string authType in authTypesSplit)
{
if (string.IsNullOrWhiteSpace(authType) is false)
{
Expand Down

0 comments on commit b1bb3e7

Please sign in to comment.