Guides
Roles and permissions
Check permissions in pages, menus and buttons, and add your own modules and roles.
A permission is a module.action string. The current user's role decides which ones they have.
export const PERMISSION_MODULES = [
'dashboard', 'orders', 'customers', 'products', 'invoices', 'courses', 'settings', 'roles',
] as const
export const PERMISSION_ACTIONS = ['view', 'create', 'update', 'delete'] as const
export type Permission = `${PermissionModule}.${PermissionAction}`
Permission is a typed union, so a typo such as 'order.view' is a compile error.
Check a permission
In a page
definePageMeta({ permission: 'invoices.view' })
middleware/permission.global.ts aborts the navigation with a 403 when the user lacks it.
In the menu
Give the menu item the same permission in useNavigation.ts. filterNavigation() hides it from the sidebar, the horizontal menu and the command palette, and drops a group when none of its children remain.
In a component
<template>
<BaseButton v-if="can('products.create')" button-type="primary" @click="create">
{{ $t('products.create') }}
</BaseButton>
</template>
<script setup lang="ts">
const { can } = useCan()
</script>
useCan() also returns role, the full role of the current user.
Where roles come from
The role id is auth.user.role. useRoles() holds the roles and team members (mocks/roles.mock.ts) and resolves the role with findRole(). The four system roles are admin, editor, viewer and billing. Their names come from roles.system.<id> in auth.json, so mocks keep ids, never labels.
For the demo, auth.setDemoRole(id) overrides the role through the auth-demo-role cookie. View as this role in Team and roles uses it to show how the app looks for each role.
Add a module
- Add the module to
PERMISSION_MODULES. - Add its label as
roles.modules.<module>and a hint asroles.moduleHints.<module>in everyauth.json. The matrix in Team and roles shows every module automatically. - Grant the actions to the roles that need them in
mocks/roles.mock.ts. - Use
reports.viewin your page, menu and buttons.
Connect it to your backend
Keep the client checks for the interface and enforce the same rules on the server: hiding a button is not security. Load the roles from your API in useRoles(), and the role of the user in auth.user, keeping the RoleModel shape (id, name, permissions, system…).
Rules for editing roles
The roles editor follows a few rules, implemented in utils/team.ts and unit tested:
- System roles cannot be edited, only duplicated.
- Any action of a module implies View of that module (
togglePermission). - Selecting a whole row or column goes through
togglePermissionModuleandtogglePermissionColumn. permissionSummary()turns a role into a readable sentence for the form footer.