Proposal: Generic type's policy and request
@sagilio がすでに取り組んでいます。
2021年11月21日 から。
評価
この issue はまだ評価されていません。
説明
Context and purposes
Casbin uses a string array to store all policies, so we have ensured the request is always a string. In some situations, we need additional operations to "parse" request values: https://github.com/casbin/casbin/issues/868.
However, this does bring some benefits, all as strings can greatly reduce the complexity of development. like we can provide a clear signature for the custom function:
// golang
func (key1 string, key2 string) bool
// csharp
Func<string, string, bool>
So this proposal does not completely make the parameters strongly typed, currently only for the following purposes :
- Validate matcher when loading model.
- Lower memory allocation and boxed conversion.
- Provider better ABAC support, like higher performance.
- Prepare for future semantic expansion, like auto-type parse.
Roadmap
- https://github.com/casbin/Casbin.NET/issues/94 (Will be included to v2.0)
- https://github.com/casbin/Casbin.NET/issues/226
- https://github.com/casbin/Casbin.NET/issues/227
- https://github.com/casbin/Casbin.NET/issues/228
Changes:
1. Generic request and policy values
Request and policy value is like a read-only tuple. We can use the record Request<T1, T2 ...> type to define them. In order to deal with the different numbers and usage scenarios of policies and requests (the request will be much more than policy), so the request should be lower allocated than the policy instance and try best to avoid boxed-transform.
Policy Sample:
public record Policy<T1, T2, T3>(T1 Value1, T2 Value2, T3 Value3) : IPolicyValues
{
public string this[int index] => index switch
{
0 => Policy.ToString(Value1),
1 => Policy.ToString(Value2),
2 => Policy.ToString(Value3),
_ => throw new ArgumentOutOfRangeException(nameof(index))
};
public int Count => 3;
public IEnumerator<string> GetEnumerator() => new PolicyEnumerator<Policy<T1, T2, T3>>(this);
IEnumerator IEnumerable.GetEnumerator() => new PolicyEnumerator<Policy<T1, T2, T3>>(this);
};
Request sample:
public readonly record struct Request<T1, T2, T3>(T1 Value1, T2 Value2, T3 Value3) : IRequestValues
{
public int Count => 3;
public static implicit operator Request<T1, T2, T3>((T1, T2, T3) tuple)
{
return Request.Create(tuple.Item1, tuple.Item2, tuple.Item3);
}
public static implicit operator (T1, T2, T3)(Request<T1, T2, T3> r)
{
return (r.Value1, r.Value2, r.Value3);
}
}
2. Expression and matcher
These will help implement a “real matcher”, we do not need the escaping:
https://github.com/casbin/Casbin.NET/blob/bb8b51b43ed2c49f6e8ca70929f609a71cef2f74/NetCasbin/Util/StringUtil.cs#L18-L27
Sample test:
[Fact]
public void TestGenericEnforce()
{
var r = Request.Create("A", 1);
var p = Policy.Create("A", 1);
var func1 = Compile("r.Value1 == p.Value1 && r.Value2 == p.Value2",
nameof(r), in r, nameof(p), in p);
Assert.True(func1(Request.Create("A", 1), Policy.Create("A", 1)));
Assert.False(func1(Request.Create("A", 1), Policy.Create("A", 2)));
Assert.False(func1(Request.Create("B", 1), Policy.Create("B", 2)));
var r2 = Request.Create("A", 1, "read");
var p2 = Policy.Create("A", 1, "read");
var func2 = Compile("r2.Value1 == p2.Value1 && r2.Value2 == p2.Value2 && r2.Value3 == p2.Value3",
nameof(r2), in r2, nameof(p2), in p2);
Assert.True(func2(Request.Create("A", 1, "read"), Policy.Create("A", 1, "read")));
Assert.False(func2(Request.Create("A", 1, "read"), Policy.Create("A", 2, "read")));
Assert.False(func2(Request.Create("B", 1, "read"), Policy.Create("B", 2, "read")));
}
- 主要言語
- C#
- スター
- 1.3k
- フォーク
- 129
- PR マージ指標
- 30日以内にマージされた PR はありません
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
apache/casbin-Casbin.NET のほかの issue
-
Refactor default role manager再び着手できるかも @hsluoyz が 1670 日前に担当しましたが、オープン中のプルリクエストはありません。 オープンenhancement help wanted
apache/casbin-Casbin.NET#250 · コメント 1 件 · 担当者 2 名 ·
-
Improve policy synchronization workflow of IAdapter再び着手できるかも @sagilio が 1945 日前に担当しましたが、オープン中のプルリクエストはありません。 オープンenhancement
apache/casbin-Casbin.NET#184 · 担当者 1 名 ·
-
Improve the unit test coverage再び着手できるかも @sagilio が 2031 日前に担当しましたが、オープン中のプルリクエストはありません。 オープンenhancement help wanted
apache/casbin-Casbin.NET#151 · コメント 1 件 · 担当者 1 名 ·
-
Add cancellation token optional argument support再び着手できるかも @sagilio が 2043 日前に担当しましたが、オープン中のプルリクエストはありません。 オープンnew feature
apache/casbin-Casbin.NET#145 · 担当者 1 名 ·
-
Rule Indexing feature再び着手できるかも @sagilio が 2075 日前に担当しましたが、オープン中のプルリクエストはありません。 オープンnew feature
apache/casbin-Casbin.NET#132 · 担当者 1 名 ·
apache/casbin-Casbin.NET の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 64/100
HicServices/RDMP#2398 ·
-
is:bug is:FQNIssue
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
nunit/nunit3-vs-adapter#1527 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
bug effort:S P3
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
nightscout/nocturne#2840 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
fluentmigrator/fluentmigrator#2424 ·