hurt-tomatoH
Refineโ€ข3y agoโ€ข
13 replies
hurt-tomato

Casbin's newEnforcer crash the app

I'm trying to configure a RBAC using Casbin following the Doc.

Creating the AccessControlProvider, the newEnforcer function causes the error in the image.

I have checked the where is the index file from, and it is from csv-parse/lib package. I'm not using that library, I guess the casbin library uses it, but I don't know what to do to solve it.

here is my accessControl file

import { newModel, StringAdapter } from "casbin";

export const model = newModel(`
[request_definition]
r = sub, obj, act

[policy_definition]
p = sub, obj, act, eft

[role_definition]
g = _, _

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

[matchers]
m = g(r.sub, p.sub) && keyMatch(r.obj, p.obj) && regexMatch(r.act, p.act)
`);

export const adapter = new StringAdapter(`
p, admin, users, (list)|(create)
p, admin, users/*, (edit)|(show)|(delete)

p, admin, orders, (list)
`);


And here is the casbin configuration in the app file:

{...}
import { newEnforcer } from "casbin";
import { model, adapter } from "./accessControl";
{...}
function App() {
  const role = localStorage.getItem("role") ?? "admin";
  return (
          {...}
          <Refine
            accessControlProvider={{
              can: async ({ resource, action, params }) => {
                const enforcer = await newEnforcer(model, adapter);

                const idActions = ["edit", "show", "delete"];

                if (idActions.includes(action)) {
                  const can = await enforcer.enforce(
                    role,
                    `${resource}/${params?.id}`,
                    action,
                  );

                  return { can };
                }

                const can = await enforcer.enforce(role, resource, action);

                return { can };
              },
            }}
          >
image.png
Was this page helpful?