@nangazaki/vue-rbac
Version:
Role-Based Access Control (RBAC) implementation for Vue.js
190 lines (141 loc) โข 3.33 kB
Markdown
# Vue RBAC
Vue RBAC is a flexible and lightweight Role-Based Access Control (RBAC) library for Vue 3 applications. It supports static and dynamic role configurations, including role inheritance and directive-based permission control.
## ๐ Features
- โ
Role and permission system with inheritance
- ๐ก Supports static, dynamic, and hybrid config modes
- ๐ Custom directives (`v-rbac`, `v-rbac:role`, `v-rbac:any`)
- ๐ง Programmatic access to permissions and roles
- ๐ API integration for dynamic role loading
- ๐ช Built-in Vue plugin and easy setup
## ๐ฆ Installation
```bash
pnpm add @nangazaki/vue-rbac
```
## ๐ง Usage
### Basic Setup (Static Configuration)
```ts
// main.ts
import { createApp } from 'vue';
import App from './App.vue';
import { VueRBAC, CONFIG_MODE } from '@nangazaki/vue-rbac';
const app = createApp(App);
app.use(VueRBAC, {
config: {
mode: CONFIG_MODE.STATIC,
autoInit: true,
roles: {
admin: {
permissions: ['users:create', 'posts:create'],
inherits: ['editor'],
},
editor: {
permissions: ['posts:edit'],
},
viewer: {
permissions: ['posts:view'],
},
},
},
});
app.mount('#app');
```
### Dynamic Configuration (From API)
```ts
app.use(VueRBAC, {
config: {
mode: CONFIG_MODE.DYNAMIC,
apiEndpoint: 'https://api.example.com/roles',
autoInit: true,
fetchOptions: {
method: "GET",
headers: {
Authorization: "Bearer your-token",
},
},
transformResponse(data) {
return {
roles: data.roles,
};
},
},
});
```
## โจ Directives
### `v-rbac`
Check for a single permission:
```vue
<button v-rbac="'users:create'">Add User</button>
```
### `v-rbac:role`
Check for a specific role:
```vue
<div v-rbac:role="'admin'">Admin Panel</div>
```
### `v-rbac:any`
Check for any permission in a list:
```vue
<div v-rbac:any="['posts:edit', 'posts:create']">
Editor or Admin Access
</div>
```
## ๐ง Programmatic Access
```ts
const { setUserRoles, hasPermission } = useRBAC();
setUserRoles("admin")
if (hasPermission('posts:create')) {
console.log('User can create posts');
}
```
## ๐ Nuxt Integration
### Add Plugin to `plugins/vue-rbac.client.ts`
```ts
import { defineNuxtPlugin } from '#app';
import { VueRBAC, CONFIG_MODE } from '@nangazaki/vue-rbac';
export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.vueApp.use(VueRBAC, {
config: {
mode: CONFIG_MODE.DYNAMIC,
apiEndpoint: '/api/roles',
autoInit: true,
transformResponse: (data) => ({ roles: data.roles }),
},
});
});
```
## ๐ Types & IntelliSense
For full TypeScript support, ensure your app includes a declaration:
```ts
// shims-vue.d.ts
import type { RBAC } from '@nangazaki/vue-rbac';
declare module 'vue' {
interface ComponentCustomProperties {
$rbac: RBAC;
}
}
```
## ๐งช Example
```vue
<template>
<div>
<button v-rbac="'users:create'">Add User</button>
<div v-rbac:role="'admin'">Admin Panel</div>
<div v-rbac:any="['posts:edit', 'posts:create']">Post Management</div>
</div>
</template>
```
## ๐ Development
```bash
pnpm install
pnpm build
```
## ๐ License
MIT License ยฉ 2025 [@nangazaki](https://github.com/nangazaki)