From 361ea39dd137474cb2539108b0a0fc3c71fb5897 Mon Sep 17 00:00:00 2001 From: shiziyuan9527 Date: Fri, 14 Feb 2020 21:18:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0v-permission=E6=8C=87?= =?UTF-8?q?=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/settings/SettingMenu.vue | 6 ++--- frontend/src/performance/main.js | 6 ++++- frontend/src/performance/permission.js | 23 +++++++++++++++++-- frontend/src/performance/store.js | 13 ++++++++++- 4 files changed, 41 insertions(+), 7 deletions(-) diff --git a/frontend/src/performance/components/settings/SettingMenu.vue b/frontend/src/performance/components/settings/SettingMenu.vue index 0fd7cd8bf1..c0f3bf6b0f 100644 --- a/frontend/src/performance/components/settings/SettingMenu.vue +++ b/frontend/src/performance/components/settings/SettingMenu.vue @@ -5,9 +5,9 @@ 账号 - 用户 - 测试资源池 - 组织 + 用户 + 测试资源池 + 组织 工作空间 API Keys diff --git a/frontend/src/performance/main.js b/frontend/src/performance/main.js index bfc8063d6f..b89abecb2b 100644 --- a/frontend/src/performance/main.js +++ b/frontend/src/performance/main.js @@ -6,10 +6,11 @@ import filters from "../common/filter"; import ajax from "../common/ajax"; import App from './App.vue'; import router from "./components/router/router"; -import store from './store' import './permission' // permission control import i18n from "../i18n/i18n"; import timestampFormatDate from "./components/common/filter/TimestampFormatDateFilter"; +import store from "./store"; +import {permission} from './permission' Vue.config.productionTip = false; Vue.use(icon); @@ -22,6 +23,9 @@ Vue.use(ajax); // filter Vue.filter('timestampFormatDate', timestampFormatDate); +// v-permission +Vue.directive('permission', permission) + new Vue({ el: '#app', router, diff --git a/frontend/src/performance/permission.js b/frontend/src/performance/permission.js index c24a67dc53..b54706c887 100644 --- a/frontend/src/performance/permission.js +++ b/frontend/src/performance/permission.js @@ -1,10 +1,29 @@ import router from './components/router/router' import Cookies from 'js-cookie' // get token from cookie import {TokenKey} from '../common/constants'; - +import store from "./store"; const whiteList = ['/login']; // no redirect whitelist +export const permission = { + inserted(el, binding) { + const { value } = binding + // user role list + const roles = store.state.roles + if (value && value instanceof Array && value.length > 0) { + const permissionRoles = value + const hasPermission = roles.some(role => { + return permissionRoles.includes(role) + }) + if (!hasPermission) { + el.parentNode && el.parentNode.removeChild(el) + } + } else { + throw new Error(`need roles! Like v-permission="['admin','editor']"`) + } + } +} + router.beforeEach(async (to, from, next) => { // determine whether the user has logged in @@ -16,7 +35,7 @@ router.beforeEach(async (to, from, next) => { } else { // const roles = user.roles.filter(r => r.id); // TODO 设置路由的权限 - + store.commit("setRoles", user.roles.map(r => r.id)) next() } } else { diff --git a/frontend/src/performance/store.js b/frontend/src/performance/store.js index 428440427a..d873f2eaf2 100644 --- a/frontend/src/performance/store.js +++ b/frontend/src/performance/store.js @@ -3,6 +3,17 @@ import Vuex from 'vuex' Vue.use(Vuex); -export default new Vuex.Store({}) +const store = new Vuex.Store({ + state: { + roles: [] + }, + mutations: { + setRoles(state, data) { + state.roles = data + } + } +}) + +export default store