-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Zxilly/master
feat: first version of vue-authz
- Loading branch information
Showing
33 changed files
with
9,093 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# don't ever lint node_modules | ||
node_modules | ||
# don't lint build output (make sure it's set to your correct build folder name) | ||
lib | ||
dist | ||
example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module.exports = { | ||
root: true, | ||
parser: '@typescript-eslint/parser', | ||
plugins: [ | ||
'@typescript-eslint', | ||
], | ||
env: { | ||
"browser": true, | ||
"node": true | ||
}, | ||
extends: [ | ||
'eslint:recommended', | ||
'plugin:@typescript-eslint/recommended', | ||
'prettier' | ||
], | ||
rules: { | ||
'@typescript-eslint/no-var-requires': 0, | ||
'@typescript-eslint/explicit-module-boundary-types': 0, | ||
'@typescript-eslint/no-unused-vars': 0, | ||
'no-useless-catch': 0, | ||
'no-multiple-empty-lines': ["error", { "max": 2, "maxEOF": 1 }], | ||
"no-trailing-spaces": ["error", {"skipBlankLines": true}], | ||
"require-await": 2, | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# dependencies | ||
node_modules/ | ||
|
||
# Src and test suites | ||
src/ | ||
|
||
# Other extensions | ||
extensions/ | ||
|
||
.idea/ | ||
package-lock.json | ||
yarn.lock | ||
|
||
# webpack configurations | ||
webpack.* | ||
|
||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"semi": true, | ||
"singleQuote": true, | ||
"printWidth": 140, | ||
"tabWidth": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,122 @@ | ||
# vue-authz | ||
# vue-authz | ||
|
||
This package allows you to integrate [Casbin.js](https://github.com/casbin/casbin.js) (An authorization library) with | ||
your Vue 3 application. | ||
|
||
## Installation | ||
|
||
```bash | ||
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](https://v3.vuejs.org/guide/composition-api-introduction.html) | ||
|
||
### The plugin | ||
|
||
```typescript | ||
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. | ||
|
||
```vue | ||
<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](https://v3.vuejs.org/guide/component-provide-inject.html) in Vue 3 to get the `$authorizer`. | ||
|
||
```typescript | ||
createApp() | ||
.use(CasbinPlugin, authorizer) | ||
.mount('#app'); | ||
``` | ||
|
||
And inject it with `AUTHORIZER_KEY` | ||
|
||
```vue | ||
<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. | ||
|
||
```vue | ||
<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](LICENSE). | ||
|
||
## Contact | ||
|
||
If you have any issues or feature requests, please contact us. PR is welcomed. | ||
|
||
- https://github.com/casbin.js/vue-authz/issues | ||
- hsluoyz@gmail.com | ||
- Tencent QQ group: [546057381](//shang.qq.com/wpa/qunwpa?idkey=8ac8b91fc97ace3d383d0035f7aa06f7d670fd8e8d4837347354a31c18fac885) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"transform": { | ||
"^.+\\.(t|j)sx?$": "ts-jest" | ||
}, | ||
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$", | ||
"moduleFileExtensions": [ | ||
"ts", | ||
"tsx", | ||
"js", | ||
"jsx", | ||
"json", | ||
"node" | ||
], | ||
"coveragePathIgnorePatterns": [ | ||
"/node_modules/" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export { install } from './install'; | ||
export * from './useAuthorizer'; | ||
import { install } from './install'; | ||
export default install; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var install_1 = require("./install"); | ||
Object.defineProperty(exports, "install", { enumerable: true, get: function () { return install_1.install; } }); | ||
__exportStar(require("./useAuthorizer"), exports); | ||
var install_2 = require("./install"); | ||
exports.default = install_2.install; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { App } from 'vue'; | ||
import { Authorizer } from 'casbin.js'; | ||
export interface CasbinPluginOptions { | ||
useGlobalProperties?: boolean; | ||
customProperties?: Array<string>; | ||
} | ||
declare const install: (app: App, authorizer: Authorizer, options?: CasbinPluginOptions) => void; | ||
export { install }; |
Oops, something went wrong.