Skip to content

Commit

Permalink
fix: Not use ref values to transform
Browse files Browse the repository at this point in the history
Signed-off-by: sagilio <sagilio@outlook.com>
  • Loading branch information
sagilio committed Apr 1, 2023
1 parent 85ad73c commit 6a397ff
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
p, alice@example.com, BasicTest, Get

# Policy below will only match if using the CustomRequestTransformer
p, alice@example.com, /attribtest, GET
p, alice@example.com, /AttributeTest, GET

# Policy below will only match if using the CustomRequestTransformer
#p, alice@example.com, /home/privacy, GET
8 changes: 4 additions & 4 deletions samples/WebApplicationSample/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public IActionResult Index()
return View();
}

[HttpGet("attribtest")]
[CasbinAuthorize]
public IActionResult AttribRouteTest(string tenantId)
[HttpGet("AttributeTest")]
[CasbinAuthorize(RequestTransformerType = typeof(KeyMatchRequestTransformer))]
public IActionResult AttributeTest(string tenantId)
{
return View();
}

[CasbinAuthorize(nameof(BasicTest), nameof(HttpMethod.Get))]
[CasbinAuthorize(nameof(BasicTest), nameof(HttpMethod.Get), RequestTransformerType = typeof(BasicRequestTransformer))]
public IActionResult BasicTest()
{
return View();
Expand Down

This file was deleted.

Empty file.
4 changes: 2 additions & 2 deletions samples/WebApplicationSample/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Basic Model : Policy is "p, alice@example.com, BasicTest, GET" - <a asp-controller="Home" asp-action="BasicTest">Test</a>.</p>
<p>Attrib Route : Policy is "p, alice@example.com, /attribtest, GET" - <a asp-controller="Home" asp-action="AttribRouteTest">Attribute Route Test</a>.</p>
<p>Basic Model : Policy is "p, alice@example.com, BasicTest, GET" - <a asp-controller="Home" asp-action="BasicTest">Basic Test</a>.</p>
<p>Attrib Route : Policy is "p, alice@example.com, /Attribute, GET" - <a asp-controller="Home" asp-action="AttributeTest">Attribute Test</a>.</p>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ public override ValueTask<StringRequestValues> TransformAsync(
ICasbinAuthorizationContext<StringRequestValues> context,
ICasbinAuthorizationData<StringRequestValues> data)
{
ref var values = ref data.Values;
var values = data.Values;
string? value1 = values.Value1;
string? value2 = values.Value2;
values.TrySetValue(0, SubTransform(context, data));
values.TrySetValue(1, value1);
values.TrySetValue(2, value2);
return new ValueTask<StringRequestValues>(data.Values);
return new ValueTask<StringRequestValues>(values);
}

protected virtual string SubTransform<TRequest>(ICasbinAuthorizationContext<TRequest> context, ICasbinAuthorizationData<TRequest> data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ public override ValueTask<StringRequestValues> TransformAsync(
ICasbinAuthorizationContext<StringRequestValues> context,
ICasbinAuthorizationData<StringRequestValues> data)
{
ref var values = ref data.Values;
var values = data.Values;
values.TrySetValue(0, SubTransform(context, data));
values.TrySetValue(1, context.HttpContext.Request.Path.Value);
values.TrySetValue(2, context.HttpContext.Request.Method);
return new ValueTask<StringRequestValues>(data.Values);
return new ValueTask<StringRequestValues>(values);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ public override ValueTask<StringRequestValues> TransformAsync(
ICasbinAuthorizationContext<StringRequestValues> context,
ICasbinAuthorizationData<StringRequestValues> data)
{
ref var values = ref data.Values;
var values = data.Values;
string? value1 = values.Value1;
string? value2 = values.Value2;
string? value3 = values.Value3;
values.TrySetValue(0, SubTransform(context, data));
values.TrySetValue(1, value1);
values.TrySetValue(2, value2);
values.TrySetValue(3, value3);
return new ValueTask<StringRequestValues>(data.Values);
return new ValueTask<StringRequestValues>(values);
}
}
}

0 comments on commit 6a397ff

Please sign in to comment.