firm-tan
firm-tan2y ago

Casbin causes severe lag

after adding access control using Casbin, my app becomes very laggy. code snippet of my accessControlProvider.ts:
import { AccessControlProvider } from "@refinedev/core";
import { newModel, StringAdapter, newEnforcer } from "casbin";
import { IAccessControlRule } from "interfaces";
import { store } from "redux/store";

const model = newModel(`
[request_definition]
r = subject, object, action

[policy_definition]
p = subject, object, action, eft

[role_definition]
g = _, _

[policy_effect]
e = some(where (p.eft == allow)) \
&& !some(where (p.eft == deny))

[matchers]
m = g(r.subject, p.subject) \
&& regexMatch(r.object, p.object) \
&& regexMatch(r.action, p.action)
`);

export const accessControlProvider: AccessControlProvider = {
can: async ({ resource, action, params }) => {
const role = store?.getState()?.user?.role;
const accessControlRules = store?.getState()?.temp?.accessControlRules;

const ruleString = accessControlRules
?.map((item: IAccessControlRule) => {
return `p, ${item?.role}, ${item?.route}, ${item?.permissions?.join(
"|"
)}`;
})
?.join("\n");

let targetUrl = params?.route || resource;
if (targetUrl?.charAt(0) === "/") targetUrl = targetUrl.slice(1);

const adapter = new StringAdapter(ruleString || `p, guest, /, *`);
const enforcer = await newEnforcer(model, adapter);
const can = await enforcer.enforce(role, targetUrl, action);

return Promise.resolve({ can });
},
};
import { AccessControlProvider } from "@refinedev/core";
import { newModel, StringAdapter, newEnforcer } from "casbin";
import { IAccessControlRule } from "interfaces";
import { store } from "redux/store";

const model = newModel(`
[request_definition]
r = subject, object, action

[policy_definition]
p = subject, object, action, eft

[role_definition]
g = _, _

[policy_effect]
e = some(where (p.eft == allow)) \
&& !some(where (p.eft == deny))

[matchers]
m = g(r.subject, p.subject) \
&& regexMatch(r.object, p.object) \
&& regexMatch(r.action, p.action)
`);

export const accessControlProvider: AccessControlProvider = {
can: async ({ resource, action, params }) => {
const role = store?.getState()?.user?.role;
const accessControlRules = store?.getState()?.temp?.accessControlRules;

const ruleString = accessControlRules
?.map((item: IAccessControlRule) => {
return `p, ${item?.role}, ${item?.route}, ${item?.permissions?.join(
"|"
)}`;
})
?.join("\n");

let targetUrl = params?.route || resource;
if (targetUrl?.charAt(0) === "/") targetUrl = targetUrl.slice(1);

const adapter = new StringAdapter(ruleString || `p, guest, /, *`);
const enforcer = await newEnforcer(model, adapter);
const can = await enforcer.enforce(role, targetUrl, action);

return Promise.resolve({ can });
},
};
1 Reply
kapa.ai
kapa.ai2y ago
Hi @killerudin. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: Sorry @killerudin, I seem to be having a bit of trouble. Don't worry though, I've pinged my supprt and they will try to fix me up! :technologist: Perhaps try again in 15mins?