Coder Social home page Coder Social logo

imp2002 / vue-authz Goto Github PK

View Code? Open in Web Editor NEW

This project forked from casbin-js/vue-authz

0.0 0.0 0.0 412 KB

Vue wrapper for Casbin.js

Home Page: https://github.com/casbin/casbin.js

License: Apache License 2.0

JavaScript 21.78% TypeScript 75.74% HTML 2.48%

vue-authz's Introduction

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.

vue-authz's People

Contributors

imp2002 avatar zxilly avatar hsluoyz avatar jakiuncle avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.