Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: first version of vue-authz #1

Merged
merged 11 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .eslintignore
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
25 changes: 25 additions & 0 deletions .eslintrc.js
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,
}
};
127 changes: 124 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,104 @@
# Created by .ignore support plugin (hsz.mobi)
### Windows template
# Windows thumbnail cache files
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db

# Dump file
*.stackdump

# Folder config file
[Dd]esktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp

# Windows shortcuts
*.lnk

### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

### Node template
# Logs
logs
*.log
Expand Down Expand Up @@ -41,8 +142,8 @@ build/Release
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo
Expand Down Expand Up @@ -74,17 +175,19 @@ typings/

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

Expand All @@ -102,3 +205,21 @@ dist

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

### Example user template template
### Example user template

# IntelliJ project files
.idea
*.iml
gen
17 changes: 17 additions & 0 deletions .npmignore
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
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"semi": true,
"singleQuote": true,
"printWidth": 140,
"tabWidth": 4
}
123 changes: 122 additions & 1 deletion README.md
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)
17 changes: 17 additions & 0 deletions jestconfig.json
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/"
]
}
4 changes: 4 additions & 0 deletions lib/index.d.ts
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;
17 changes: 17 additions & 0 deletions lib/index.js
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;
8 changes: 8 additions & 0 deletions lib/install.d.ts
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 };
Loading