Last active
July 30, 2024 17:59
-
-
Save Luchanso/8309a73aa00532b94653d46991c4ca88 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Enforcer, newEnforcer, StringAdapter } from "casbin"; | |
import { join } from "path"; | |
import { cwd } from "process"; | |
declare global { | |
var __casbin: Enforcer; | |
} | |
await (async function getEnforcer() { | |
if (global.__casbin) { | |
return global.__casbin; | |
} | |
const sa = new StringAdapter(` | |
p, alice, data1, read | |
p, bob, data2, write | |
p, data2_admin, data2, read | |
p, data2_admin, data2, write | |
g, alice, data2_admin | |
`); | |
const filePath = join(cwd(), 'rbac-model.conf'); | |
const enforcer = await newEnforcer(filePath, sa); | |
global.__casbin = enforcer; | |
return enforcer; | |
})(); | |
export const enforcer = global.__casbin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import '@/casbin'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { enforcer } from "@/casbin"; | |
export default async function Home() { | |
const res = await enforcer.enforce('alice', 'data1', 'read'); | |
return ( | |
<main className="flex min-h-screen flex-col items-center justify-between p-24"> | |
File Status 200 | |
{JSON.stringify(res, null, 2)} | |
</main> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment