Skip to content

Commit

Permalink
feat: add sample for migrating efcore adapter.
Browse files Browse the repository at this point in the history
  • Loading branch information
AsakusaRinne committed Sep 12, 2022
1 parent e73d261 commit c047b35
Show file tree
Hide file tree
Showing 71 changed files with 40,589 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Casbin.AspNetCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Casbin.AspNetCore.Performan
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Casbin.AspNetCore.Abstractions", "src\Casbin.AspNetCore.Abstractions\Casbin.AspNetCore.Abstractions.csproj", "{C6A00589-D105-452E-ADAC-16F19B2B3B2D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApplicationWithEfcoreSample", "samples\WebApplicationWithEfcoreSample\WebApplicationWithEfcoreSample.csproj", "{F84722DB-CB4C-4C84-A412-0B5E0C2D91EF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -70,6 +72,10 @@ Global
{C6A00589-D105-452E-ADAC-16F19B2B3B2D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C6A00589-D105-452E-ADAC-16F19B2B3B2D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C6A00589-D105-452E-ADAC-16F19B2B3B2D}.Release|Any CPU.Build.0 = Release|Any CPU
{F84722DB-CB4C-4C84-A412-0B5E0C2D91EF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F84722DB-CB4C-4C84-A412-0B5E0C2D91EF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F84722DB-CB4C-4C84-A412-0B5E0C2D91EF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F84722DB-CB4C-4C84-A412-0B5E0C2D91EF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -82,6 +88,7 @@ Global
{D28D5C8B-87BC-4BB4-8EE3-4266195F02B3} = {E8E8CE11-04F7-4F61-B5CF-F58852E8052B}
{82166036-4E1D-44EA-B6C2-190C5BD4EBFB} = {E8E8CE11-04F7-4F61-B5CF-F58852E8052B}
{C6A00589-D105-452E-ADAC-16F19B2B3B2D} = {B3DE00B2-3985-40B5-AFCF-4466733B9A35}
{F84722DB-CB4C-4C84-A412-0B5E0C2D91EF} = {17AA8DCA-8B6E-42B4-B251-BBE13189853E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2A11ADB2-E18C-492D-B72E-F718510D5E6F}
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@{
Layout = "/Views/Shared/_Layout.cshtml";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[request_definition]
r = sub, obj, act

[policy_definition]
p = sub, obj, act

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = r.sub == p.sub && r.obj == p.obj && r.act == p.act
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Policy below will only match using both BasicRequestTransformer and CustomRequestTransformer
p, alice@example.com, BasicTest, Get

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

# Policy below will only match if using the CustomRequestTransformer
#p, alice@example.com, /home/privacy, GET
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Casbin.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace WebApplicationSample.Controllers
{
[ApiController]
[Route("/api")]
public class ApiController : Controller
{

[HttpGet("index")]
[CasbinAuthorize]
public IActionResult Index()
{
return new JsonResult(new
{
Message = "You passed the casbin authorize."
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Diagnostics;
using Casbin.AspNetCore.Authorization;
using Casbin.AspNetCore.Authorization.Transformers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;
using Microsoft.Extensions.Logging;
using WebApplicationSample.Models;

namespace WebApplicationSample.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;

public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}

public IActionResult Index()
{
return View();
}

[HttpGet("attribtest")]
[CasbinAuthorize("/attribtest", "GET")]
public IActionResult AttribRouteTest(string tenantId)
{
return View();
}

[CasbinAuthorize(nameof(BasicTest), nameof(HttpMethod.Get))]
public IActionResult BasicTest()
{
return View();
}

[CasbinAuthorize]
public IActionResult Privacy()
{
return View();
}

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace WebApplicationSample.Data
{
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
}
22 changes: 22 additions & 0 deletions samples/WebApplicationWithEfcoreSample/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["samples/WebApplicationSample/WebApplicationSample.csproj", "samples/WebApplicationSample/"]
RUN dotnet restore "samples/WebApplicationSample/WebApplicationSample.csproj"
COPY . .
WORKDIR "/src/samples/WebApplicationSample"
RUN dotnet build "WebApplicationSample.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "WebApplicationSample.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplicationSample.dll"]
Loading

0 comments on commit c047b35

Please sign in to comment.