This repository has been archived by the owner on Oct 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathmixin.js
128 lines (105 loc) · 3.03 KB
/
mixin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// @ts-check
import Vue from 'vue'
import { testPermission } from './checker'
const EventBus = new Vue()
let currentGlobal = []
let not = false
export const register = (initial, acceptLocalRules, globalRules, router, notfound, middleware) => {
currentGlobal = Array.isArray(initial) ? initial : [initial]
if (router !== null) {
router.beforeEach(async (to, from, next) => {
if (middleware) {
await middleware({change (a) {
currentGlobal = a
}})
}
// to be backwards compatible (notfound could be string)
const notFoundPath = notfound.path || notfound;
if (to.path === notFoundPath) return next()
/** @type {Array} */
if (!('rule' in to.meta)) {
return console.error(`[vue-acl] ${to.path} not have rule`)
}
let routePermission = to.meta.rule
if (routePermission in globalRules) {
routePermission = globalRules[routePermission]
}
if (!testPermission(currentGlobal, routePermission)) {
// check if forwardQueryParams is set
if (notfound.forwardQueryParams) {
return next({path: notFoundPath, query: to.query})
}
return next(notFoundPath)
}
return next()
})
}
return {
/**
* Called before create component
*/
beforeCreate () {
const self = this
this.$acl = {
/**
* Change current permission
* @param {string|Array} param
*/
change(param) {
param = Array.isArray(param) ? param : [param]
if (currentGlobal.toString() !== param.toString()) {
EventBus.$emit('vueacl-permission-changed', param)
}
},
/**
* get current permission
*/
get get () {
return currentGlobal
},
/**
* reverse current acl check
*/
get not() {
not = true
return this
},
/**
* Check if rule is valid currently
* @param {string} ruleName rule name
*/
check(ruleName) {
const hasNot = not
not = false
if (ruleName in globalRules) {
const result = testPermission(this.get, globalRules[ruleName])
return hasNot ? !result : result
}
if (ruleName in self) {
if (!acceptLocalRules) {
return console.error('[vue-acl] acceptLocalRules is not enabled')
}
const result = testPermission(this.get, self[ruleName])
return hasNot ? !result : result
}
return false
}
}
},
created () {
EventBus.$on('vueacl-permission-changed', this.vue_aclOnChange)
},
destroyed() {
EventBus.$off('vueacl-permission-changed', this.vue_aclOnChange)
},
methods: {
vue_aclOnChange (newPermission) {
currentGlobal = newPermission
if ('onChange' in this.$acl) {
this.$acl.onChange(currentGlobal)
}
this.$forceUpdate()
}
}
}
}