Skip to content

Instantly share code, notes, and snippets.

@Luchanso
Last active July 30, 2024 17:59
Show Gist options
  • Save Luchanso/8309a73aa00532b94653d46991c4ca88 to your computer and use it in GitHub Desktop.
Save Luchanso/8309a73aa00532b94653d46991c4ca88 to your computer and use it in GitHub Desktop.
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;
import '@/casbin';
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