Proposal: Generic type's policy and request
@sagilio đang làm issue này rồi.
Từ ngày 21/11/2021.
Đánh giá
Issue này chưa được đánh giá.
Mô tả
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")));
}
- Ngôn ngữ chính
- C#
- Star
- 1.3k
- Fork
- 129
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của apache/casbin-Casbin.NET
-
Refactor default role managerCó thể làm lại được @hsluoyz đã nhận 1668 ngày trước và không có pull request nào đang mở. Đang mởenhancement help wanted
apache/casbin-Casbin.NET#250 · 1 bình luận · 2 người được giao ·
-
Improve policy synchronization workflow of IAdapterCó thể làm lại được @sagilio đã nhận 1943 ngày trước và không có pull request nào đang mở. Đang mởenhancement
apache/casbin-Casbin.NET#184 · 1 người được giao ·
-
Improve the unit test coverageCó thể làm lại được @sagilio đã nhận 2029 ngày trước và không có pull request nào đang mở. Đang mởenhancement help wanted
apache/casbin-Casbin.NET#151 · 1 bình luận · 1 người được giao ·
-
Add cancellation token optional argument supportCó thể làm lại được @sagilio đã nhận 2041 ngày trước và không có pull request nào đang mở. Đang mởnew feature
apache/casbin-Casbin.NET#145 · 1 người được giao ·
-
Rule Indexing featureCó thể làm lại được @sagilio đã nhận 2073 ngày trước và không có pull request nào đang mở. Đang mởnew feature
apache/casbin-Casbin.NET#132 · 1 người được giao ·
Tất cả issue của apache/casbin-Casbin.NET
Issue tương tự
-
area-multitenancy area-security p1-recommended priority-high technical-debt
Độ khó 2/5 Dưới một giờ Mức phù hợp với người mới 75/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
-
help wanted needs-unity ready for a contributor
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
eyalgolan/statecraft-israel#1542 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
kind/enhancement Provider Upgrade pulumi/pulumi-azure R1 Task
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
pulumi/pulumi-azure#3669 ·
-
type/bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày