Skip to content
This repository has been archived by the owner on Oct 23, 2020. It is now read-only.

Allow change to check new generated acl rule #89

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,15 @@ export default new Router({
name: 'notfound',
component: NotFound,
meta: {
rule: '*'
rule: ['*']
}
}
]
})
```

More details:
- Define `rule` meta for link a route with a permission, your can use name of the global rule e.g `isPublic` or use `AclRule` for create new rule orr use `*` for define allowed route.
- Define `rule` meta for link a route with a permission, your can use name of the global rule e.g `isPublic` or use `AclRule` for create new rule or use `[*]` for define allowed route.

For finish, in your `main.js` import the `acl` and pass to Vue root instance:

Expand Down
57 changes: 51 additions & 6 deletions example/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,61 @@
<button @click="$acl.change('read')">Turn public</button>
</section>

<p style="padding: 10px">Current permission: {{ $acl.get }}</p>
<p style="padding: 10px">
Current permission: {{ $acl.get }}.
<span v-if="$acl.check('isLocalRule')">This span can be seen if have 'write' permission.</span>
</p>

<hr>
<p><small>Page content:</small></p>
<router-view/>
<p style="padding: 10px" v-if="$acl.check($router.currentRoute.meta.rule)">Always Ture
</p>

<section style="display: flex; padding: 10px">
<button @click="assignCheckConditionToWriteOnly()">Change check condition to 'write only'</button>
<button
@click="assignCheckConditionToReadAndWrite()"
>Change check condition to 'read and write'</button>
<button @click="assignCheckConditionToReadOrWrite()">Change check condition to 'read or write'</button>
</section>

<p style="padding-left: 10px">Current check condition is: '{{ checkRuleInfo }}'</p>
<p style="padding-left: 10px">Check result is: '{{ $acl.check(checkRuleVariable) }}'</p>

<hr />
<p>
<small>Page content:</small>
</p>
<router-view />
</div>
</template>

<script>
import { AclRule } from "../../source";
export default {
name: 'App'
}
name: "App",
data() {
return {
checkRuleVariable: null,
checkRuleInfo: ""
};
},
computed: {
isLocalRule() {
return new AclRule("write").generate();
}
},
methods: {
assignCheckConditionToWriteOnly() {
this.checkRuleVariable = new AclRule("write");
this.checkRuleInfo = "write only";
},
assignCheckConditionToReadAndWrite() {
this.checkRuleVariable = new AclRule("read").and("write");
this.checkRuleInfo = "read and write";
},
assignCheckConditionToReadOrWrite() {
this.checkRuleVariable = new AclRule("read").or("write");
this.checkRuleInfo = "read or write";
}
}
};
</script>
19 changes: 18 additions & 1 deletion example/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { AclRule } from '../../source'
import Public from './views/Public.vue'
import Admin from './views/Admin.vue'
import NotFound from './views/NotFound.vue'
import Asterisk from './views/Asterisk.vue'

Vue.use(Router)

Expand Down Expand Up @@ -33,6 +34,22 @@ export default new Router({
meta: {
rule: '*'
}
}
},
{
path: '/asterisk-invalid',
name: 'asterisk',
component: Asterisk,
meta: {
rule: '*'
}
},
{
path: '/asterisk-valid',
name: 'asterisk',
component: Asterisk,
meta: {
rule: ['*']
}
},
]
})
5 changes: 5 additions & 0 deletions example/src/views/Asterisk.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<div class="asterisk">
<h1>This is an asterisk page</h1>
</div>
</template>
3 changes: 2 additions & 1 deletion source/checker.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { AclRule } from './index'

/**
* Test a rule with a permission group
* @param {Array} current current permissions
* @param {Array} rules rule to test
* @param {Array|AclRule} rules rule to test
* @return {boolean} valided rule
*/
export const testPermission =(current, rules) => {
Expand Down
20 changes: 12 additions & 8 deletions source/mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import Vue from 'vue'

import { testPermission } from './checker'

import { AclRule } from './index'



Expand Down Expand Up @@ -84,24 +84,28 @@ export const register = (initial, acceptLocalRules, globalRules, router, notfoun

/**
* Check if rule is valid currently
* @param {string} ruleName rule name
* @param {string|Array|AclRule} rule rule
*/
check(ruleName) {
check(rule) {
const hasNot = not
not = false

if (ruleName in globalRules) {
const result = testPermission(this.get, globalRules[ruleName])
if (typeof rule === 'string' && rule in globalRules) {
const result = testPermission(this.get, globalRules[rule])
return hasNot ? !result : result
}


if (ruleName in self) {
if (typeof rule === 'string' && rule in self) {
if (!acceptLocalRules) {
return console.error('[vue-acl] acceptLocalRules is not enabled')
}

const result = testPermission(this.get, self[ruleName])
const result = testPermission(this.get, self[rule])
return hasNot ? !result : result
}

if (Array.isArray(rule) || rule instanceof AclRule) {
const result = testPermission(this.get, rule)
return hasNot ? !result : result
}

Expand Down