fair-rose
fair-rose11mo ago

AccessControl casbin MemoryAdapter

I'm following tutorial from this https://refine.dev/docs/advanced-tutorials/access-control/#adding-policy-and-model However, in Adding Policy and Model.
export const adapter = new MemoryAdapter(`
p, editor, posts, list
`);
export const adapter = new MemoryAdapter(`
p, editor, posts, list
`);
Where does the MemoryAdapter come from?
6 Replies
kapa.ai
kapa.ai11mo ago
Hi @tads8799. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- I'm sorry for the confusion, but it seems there's a mistake in the documentation you're following. The MemoryAdapter should be imported from the "casbin" library, similar to how the newModel function is imported. However, this import statement is not shown in the provided extracts. I recommend checking the official Casbin documentation or their GitHub repository for the correct usage and import statements. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai11mo ago
<@577213996344803338> kapa.ai is still learning and improving, please let us know how it did by reacting below
fair-rose
fair-rose11mo ago
The full example code is like this:
import { newModel, StringAdapter } from "casbin";

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

[policy_definition]
p = sub, obj, act

[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 MemoryAdapter(`
p, editor, posts, list
`);
import { newModel, StringAdapter } from "casbin";

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

[policy_definition]
p = sub, obj, act

[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 MemoryAdapter(`
p, editor, posts, list
`);
@kapa.ai
kapa.ai
kapa.ai11mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- The MemoryAdapter in your code is used to define the policy for the Casbin enforcer. It seems like there's a mistake in your code. You've imported StringAdapter from "casbin" but you're using MemoryAdapter which is not imported. If you want to use MemoryAdapter, you should import it from "casbin". If MemoryAdapter is not available in "casbin", you might want to use StringAdapter instead, as you've already imported it. Here's how your code should look like if you want to use StringAdapter:
import { newModel, StringAdapter } from "casbin";

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

[policy_definition]
p = sub, obj, act

[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, editor, posts, list
`);
import { newModel, StringAdapter } from "casbin";

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

[policy_definition]
p = sub, obj, act

[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, editor, posts, list
`);
Please check the official Casbin documentation for the correct usage and import statements. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai11mo ago
<@577213996344803338> kapa.ai is still learning and improving, please let us know how it did by reacting below
fair-rose
fair-rose11mo ago
I understand