Pratham Khodwe
08/07/2025, 12:22 PMAsk Inkeep
08/07/2025, 12:22 PMAsk Inkeep
08/07/2025, 12:22 PMconst checkIfPermitted = async (user, resourcesAndActions) => {
let isPermittedList = [];
const promises = resourcesAndActions.map(async (resourceAndAction, index) => {
const { resource, action, resourceAttributes } = resourceAndAction;
const resourceObj = { type: resource, attributes: resourceAttributes };
const permitted = await permit.check(user, action, resourceObj) ;
console.log(permitted, "bulk") ;
isPermittedList = [...isPermittedList, { index, permitted }];
}) ;
await Promise.all(promises) ;
return isPermittedList
.sort((a, b) => a.index - b.index)
.map(item => item.permitted) ;
};
// add route that get user and list of resources and actions object as post parameters and check if user is permitted
<http://app.post|app.post>("/bulk", async (req, res) => {
const { user, resourcesAndActions } = req.body;
res.send(await checkIfPermitted(user, resourcesAndActions) ) ;
}) ;
Alternative Approach with Permit Frontend SDK
For Vue.js applications, you can use the Permit Frontend SDK with a bulk loading approach (2) :
import { PureAbility } from '@casl/ability';
import { Permit } from 'permit-fe-sdk';
import { defineStore } from 'pinia';
import { onMounted, ref, watch } from 'vue';
import { useUserStore } from './user';
export const useAbilitiesStore = defineStore('abilities', () => {
const current = ref(new PureAbility([]) ) ;
const user = useUserStore() ;
const permit = ref(
Permit({
loggedInUser: user.current,
backendUrl: '<http://localhost:3000/abilities>'
})
) ;
const setAbility = async () => {
permit.value = Permit({
loggedInUser: user.current,
backendUrl: '<http://localhost:3000/abilities>'
}) ;
permit.value.reset() ;
await permit.value.loadLocalStateBulk([
{ action: 'create', resource: 'Meal' },
{ action: 'read', resource: 'Meal' },
{ action: 'delete', resource: 'Meal' },
{ action: 'create', resource: 'Order' },
{ action: 'read', resource: 'Order' },
{ action: 'fulfill', resource: 'Order' },
{ action: 'assign-rider', resource: 'Order' },
{ action: 'deliver', resource: 'Order' }
]) ;
const caslConfig = permit.value.getCaslJson() ;
current.value = new PureAbility(
caslConfig && caslConfig.length ? caslConfig : []
) ;
};
onMounted(async () => {
await setAbility() ;
watch(
() => user.current,
async (_) => await setAbility()
) ;
document.addEventListener(
'visibilitychange',
async (_) => await setAbility()
) ;
}) ;
return { current, permit };
}) ;
Frontend Usage
Once you have the permissions loaded, you can conditionally render UI elements based on the permissions (1) :
<div className="bg-white p-6 rounded shadow">
<div className="flex justify-between items-center mb-4">
<h2 className="text-xl font-semibold">Projects</h2>
{permissions["create:projects"] && (
<button className="px-3 py-1 bg-blue-600 text-white text-sm rounded hover:bg-bluAsk Inkeep
08/07/2025, 12:22 PMAsk Inkeep
08/07/2025, 12:27 PMOr Weis
08/07/2025, 12:30 PM