eastern-cyan
eastern-cyan8mo ago

resource names casing sometimes get changes when read in Access Control

Sometimes resouce names can get change to lower case even though they are casesed Camel. { name: "auditLogs", list: "/administration/auditLogs", show: "/administration/auditLogs/show/:id", meta: { icon: <></>, parent: "administration", label: "Audit Logs", }, }, will then appear as auditlogs in the access control Can method
10 Replies
kapa.ai
kapa.ai8mo ago
Hi @Joe Gaffney. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- It appears that you are experiencing an issue where resource names are being converted to lowercase when accessed in the can method of your AccessControlProvider. This is a known issue that has been discussed in the community and on GitHub. Workaround To handle this issue, you can use the params object passed to the can method to get the resource name with the correct casing. Here is an example of how you can implement this:
export const accessControlProvider = {
can: async ({ resource, action, params }) => {
const resourceName = params?.resource?.name;
const anyUsefulMeta = params?.resource?.meta?.yourUsefulMeta;

if (
resourceName === "auditLogs" &&
anyUsefulMeta === true &&
action === "edit"
) {
return {
can: false,
reason: "Unauthorized",
};
}
},
};
export const accessControlProvider = {
can: async ({ resource, action, params }) => {
const resourceName = params?.resource?.name;
const anyUsefulMeta = params?.resource?.meta?.yourUsefulMeta;

if (
resourceName === "auditLogs" &&
anyUsefulMeta === true &&
action === "edit"
) {
return {
can: false,
reason: "Unauthorized",
};
}
},
};
Additional Context This issue has been discussed in various forums and GitHub issues. For example, in a Discord support thread (https://discord.com/channels/837692625737613362/1199127587629301891), a community member mentioned that the resource name vehicleTypes was being converted to vehicletypes in some requests. Similarly, a GitHub issue (https://github.com/refinedev/refine/issues/4852) discussed how resource names are usually defined in lowercase and plural, and a workaround was suggested to handle this. Documentation For more detailed information, you can refer to the Access Control Provider documentation (https://refine.dev/docs/authorization/access-control-provider#meta-access), which explains how to use the meta property and access the full resource object in the can method. If you continue to experience issues, consider opening a new issue on the refine GitHub repository (https://github.com/refinedev/refine/issues) for further assistance. Example Resource Definition Here is an example of how you might define your resource to ensure it is correctly recognized:
{
name: "auditLogs",
list: "/administration/auditLogs",
show: "/administration/auditLogs/show/:id",
meta: {
icon: <></>,
parent: "administration",
label: "Audit Logs",
},
}
{
name: "auditLogs",
list: "/administration/auditLogs",
show: "/administration/auditLogs/show/:id",
meta: {
icon: <></>,
parent: "administration",
label: "Audit Logs",
},
}
By following these steps, you should be able to handle the casing issue in your access control logic effectively. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai8mo ago
<@422289470487003153> kapa.ai is still learning and improving, please let us know how it did by reacting below
eastern-cyan
eastern-cyanOP8mo ago
@kapa.ai its strange as for some it does not change the casing I have others in this format. I guess is something that probably needs looking into what pattern is causing it.
kapa.ai
kapa.ai8mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- It is indeed peculiar that the casing issue occurs inconsistently. This inconsistency can make it challenging to pinpoint the exact cause. However, based on the information available, here are some steps and considerations that might help you identify the pattern causing this issue: Steps to Investigate 1. Check Resource Definitions: Ensure that all your resource definitions are consistent in terms of naming conventions. For example, if you have resources defined in CamelCase, make sure they are consistently defined that way across your application.
{{
name: "auditLogs",
list: "/administration/auditLogs",
show: "/administration/auditLogs/show/:id",
meta: {{
icon: <></>,
parent: "administration",
label: "Audit Logs",
}},
}},

{{
name: "auditLogs",
list: "/administration/auditLogs",
show: "/administration/auditLogs/show/:id",
meta: {{
icon: <></>,
parent: "administration",
label: "Audit Logs",
}},
}},

2. Inspect can Method: Use logging within your can method to capture the resource names being passed. This can help you identify when and where the casing changes occur.
export const accessControlProvider = {{
can: async ({{ resource, action, params }}) => {{
console.log("Resource Name:", params?.resource?.name);
const resourceName = params?.resource?.name;
const anyUsefulMeta = params?.resource?.meta?.yourUsefulMeta;

if (
resourceName === "auditLogs" &&
anyUsefulMeta === true &&
action === "edit"
) {{
return {{
can: false,
reason: "Unauthorized",
}};
}}
}},
}};

export const accessControlProvider = {{
can: async ({{ resource, action, params }}) => {{
console.log("Resource Name:", params?.resource?.name);
const resourceName = params?.resource?.name;
const anyUsefulMeta = params?.resource?.meta?.yourUsefulMeta;

if (
resourceName === "auditLogs" &&
anyUsefulMeta === true &&
action === "edit"
) {{
return {{
can: false,
reason: "Unauthorized",
}};
}}
}},
}};

3. Review Related Discussions: Look into related discussions and issues reported by other users. For instance, in a Discord support thread (https://discord.com/channels/837692625737613362/1199127587629301891), a community member mentioned a similar issue with the resource name vehicleTypes being converted to vehicletypes. 4. Open a GitHub Issue: If the issue persists and you cannot identify the pattern, consider opening a new issue on the refine GitHub repository (https://github.com/refinedev/refine/issues). Provide detailed information about your resource definitions and the inconsistent behavior you are experiencing. This will help the maintainers investigate and potentially resolve the issue. Example of Logging in can Method
export const accessControlProvider = {{
can: async ({{ resource, action, params }}) => {{
console.log("Resource Name:", params?.resource?.name);
const resourceName = params?.resource?.name;
const anyUsefulMeta = params?.resource?.meta?.yourUsefulMeta;

if (
resourceName === "auditLogs" &&
anyUsefulMeta === true &&
action === "edit"
) {{
return {{
can: false,
reason: "Unauthorized",
}};
}}
}},
}};
export const accessControlProvider = {{
can: async ({{ resource, action, params }}) => {{
console.log("Resource Name:", params?.resource?.name);
const resourceName = params?.resource?.name;
const anyUsefulMeta = params?.resource?.meta?.yourUsefulMeta;

if (
resourceName === "auditLogs" &&
anyUsefulMeta === true &&
action === "edit"
) {{
return {{
can: false,
reason: "Unauthorized",
}};
}}
}},
}};
By following these steps, you should be able to gather more information about the pattern causing the casing issue and take appropriate action to address it. If you need further assistance, the refine community and maintainers are usually very responsive and can provide additional support. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai8mo ago
<@422289470487003153> kapa.ai is still learning and improving, please let us know how it did by reacting below
harsh-harlequin
harsh-harlequin8mo ago
Hey @Joe Gaffney can you spot which calls to the can method sends lowercase resources? Do you have KBar setup? Can you share a bit more about your project setup 🙏
eastern-cyan
eastern-cyanOP8mo ago
Hey, @aliemir it was for list actions KbarProviider is setup, but not really utilizing it. "auditLogs" was the resource name but also seemed to happen with others. But not consistently.
harsh-harlequin
harsh-harlequin8mo ago
Found the issue, can you create a github issue for this? I think we can work on the fix for the next release 🤔 BTW, are you using Refine Devtools? useCan and CanAccess calls can be tracked in the Devtools Monitor.
eastern-cyan
eastern-cyanOP7mo ago
Hey, put a ticket in. I am using devTools on one refine project but not this particular one. @Jay Bhensdadia this was some info from when the issue was found. There is also more info on the jira ticket.
other-emerald
other-emerald7mo ago
okay thanks!

Did you find this page helpful?