Vue 2 & 3 wrapper for Casbin.js

Clone this repo:
  1. e10a590 feat: support Vue2 (#16) by imp2002 · 3 years, 6 months ago master v0.2.0
  2. a275b30 fix: fix README typo (#15) by imp2002 · 3 years, 6 months ago v0.1.1
  3. f501bb1 fix: fix CI error (#12) by imp2002 · 3 years, 6 months ago v0.1.0
  4. c852b0e feat: setup GitHub Actions CI and semantic-release (#10) by imp2002 · 3 years, 6 months ago
  5. a50050e fix: casbin.js version upgrade (#8) by jakiuncle · 3 years, 6 months ago

vue-authz

NPM version NPM download CI Coverage Status

This package allows you to integrate Casbin.js (An authorization library) with your Vue 3 application.

Installation

npm install vue-authz
# or
yarn add vue-authz
# or
pnpm add vue-authz

Getting started

This package provides a Vue plugin, several hooks for new Vue Composition API

The plugin

import { createApp } from 'vue';
import CasbinPlugin from 'vue-authz';
import { Authorizer } from 'casbin.js';

const permission = {
    "read": ['data1', 'data2'],
    "write": ['data1']
}

// Run casbin.js in manual mode, which requires you to set the permission manually.
const authorizer = new casbinjs.Authorizer("manual");

authorizer.setPermission(permission);

createApp()
    .use(CasbinPlugin, authorizer, {
        useGlobalProperties: true
    }).mount('#app');

After that, you can use $authorizer and $can in every component.


<template> <p v-if='$can("read","post")' > Post content. </p> </template>

useGlobalProperties will mount $can and $authorizer on every component. Sometimes, you may want to use some other function as $can. In this condition, you can use provide/inject API in Vue 3 to get the $authorizer.

createApp()
    .use(CasbinPlugin, authorizer)
    .mount('#app');

And inject it with AUTHORIZER_KEY


<template> <p v-if="$whatyouwant.can('read', 'Post')"> Post content. </p> </template> <script> import { AUTHORIZER_KEY } from 'vue-authz'; export default { inject: { $whatyouwant: { from: AUTHORIZER_KEY } } } </script>

You can also use useAuthorizer function.


<template> <p v-if="whatyouwant.can('read', 'Post')"> Post content. </p> </template> <script> import { useAuthorizer } from 'vue-authz'; export default { setup() { const { whatyouwant } = useAuthorizer(); return { whatyouwant }; } } </script>

License

This project is licensed under the Apache 2.0 license.

Contact

If you have any issues or feature requests, please contact us. PR is welcomed.