remove permission control
This commit is contained in:
parent
b0f58ee31e
commit
b6147d33f3
|
@ -12,7 +12,7 @@
|
||||||
:collapse-transition="false"
|
:collapse-transition="false"
|
||||||
mode="vertical"
|
mode="vertical"
|
||||||
>
|
>
|
||||||
<sidebar-item v-for="route in permission_routes" :key="route.path" :item="route" :base-path="route.path" />
|
<sidebar-item v-for="route in routes" :key="route.path" :item="route" :base-path="route.path" />
|
||||||
</el-menu>
|
</el-menu>
|
||||||
</el-scrollbar>
|
</el-scrollbar>
|
||||||
</div>
|
</div>
|
||||||
|
@ -28,9 +28,11 @@ export default {
|
||||||
components: { SidebarItem, Logo },
|
components: { SidebarItem, Logo },
|
||||||
computed: {
|
computed: {
|
||||||
...mapGetters([
|
...mapGetters([
|
||||||
'permission_routes',
|
|
||||||
'sidebar'
|
'sidebar'
|
||||||
]),
|
]),
|
||||||
|
routes() {
|
||||||
|
return this.$router.options.routes
|
||||||
|
},
|
||||||
activeMenu() {
|
activeMenu() {
|
||||||
const route = this.$route
|
const route = this.$route
|
||||||
const { meta, path } = route
|
const { meta, path } = route
|
||||||
|
|
|
@ -26,25 +26,15 @@ router.beforeEach(async(to, from, next) => {
|
||||||
next({ path: '/' })
|
next({ path: '/' })
|
||||||
NProgress.done()
|
NProgress.done()
|
||||||
} else {
|
} else {
|
||||||
// determine whether the user has obtained his permission roles through getInfo
|
const hasGetUserInfo = store.getters.name
|
||||||
const hasRoles = store.getters.roles && store.getters.roles.length > 0
|
if (hasGetUserInfo) {
|
||||||
if (hasRoles) {
|
|
||||||
next()
|
next()
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
// get user info
|
// get user info
|
||||||
// note: roles must be a object array! such as: ['admin'] or ,['developer','editor']
|
await store.dispatch('user/getInfo')
|
||||||
const { roles } = await store.dispatch('user/getInfo')
|
|
||||||
|
|
||||||
// generate accessible routes map based on roles
|
next()
|
||||||
const accessRoutes = await store.dispatch('permission/generateRoutes', roles)
|
|
||||||
|
|
||||||
// dynamically add accessible routes
|
|
||||||
router.addRoutes(accessRoutes)
|
|
||||||
|
|
||||||
// hack method to ensure that addRoutes is complete
|
|
||||||
// set the replace: true, so the navigation will not leave a history record
|
|
||||||
next({ ...to, replace: true })
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// remove token and go to login page to re-login
|
// remove token and go to login page to re-login
|
||||||
await store.dispatch('user/resetToken')
|
await store.dispatch('user/resetToken')
|
||||||
|
|
|
@ -88,14 +88,8 @@ export const constantRoutes = [
|
||||||
meta: { title: 'Form', icon: 'form' }
|
meta: { title: 'Form', icon: 'form' }
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
},
|
||||||
]
|
|
||||||
|
|
||||||
/**
|
|
||||||
* asyncRoutes
|
|
||||||
* the routes that need to be dynamically loaded based on user roles
|
|
||||||
*/
|
|
||||||
export const asyncRoutes = [
|
|
||||||
{
|
{
|
||||||
path: '/nested',
|
path: '/nested',
|
||||||
component: Layout,
|
component: Layout,
|
||||||
|
|
|
@ -3,8 +3,6 @@ const getters = {
|
||||||
device: state => state.app.device,
|
device: state => state.app.device,
|
||||||
token: state => state.user.token,
|
token: state => state.user.token,
|
||||||
avatar: state => state.user.avatar,
|
avatar: state => state.user.avatar,
|
||||||
name: state => state.user.name,
|
name: state => state.user.name
|
||||||
roles: state => state.user.roles,
|
|
||||||
permission_routes: state => state.permission.routes
|
|
||||||
}
|
}
|
||||||
export default getters
|
export default getters
|
||||||
|
|
|
@ -2,7 +2,6 @@ import Vue from 'vue'
|
||||||
import Vuex from 'vuex'
|
import Vuex from 'vuex'
|
||||||
import getters from './getters'
|
import getters from './getters'
|
||||||
import app from './modules/app'
|
import app from './modules/app'
|
||||||
import permission from './modules/permission'
|
|
||||||
import settings from './modules/settings'
|
import settings from './modules/settings'
|
||||||
import user from './modules/user'
|
import user from './modules/user'
|
||||||
|
|
||||||
|
@ -11,7 +10,6 @@ Vue.use(Vuex)
|
||||||
const store = new Vuex.Store({
|
const store = new Vuex.Store({
|
||||||
modules: {
|
modules: {
|
||||||
app,
|
app,
|
||||||
permission,
|
|
||||||
settings,
|
settings,
|
||||||
user
|
user
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,69 +0,0 @@
|
||||||
import { asyncRoutes, constantRoutes } from '@/router'
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Use meta.role to determine if the current user has permission
|
|
||||||
* @param roles
|
|
||||||
* @param route
|
|
||||||
*/
|
|
||||||
function hasPermission(roles, route) {
|
|
||||||
if (route.meta && route.meta.roles) {
|
|
||||||
return roles.some(role => route.meta.roles.includes(role))
|
|
||||||
} else {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Filter asynchronous routing tables by recursion
|
|
||||||
* @param routes asyncRoutes
|
|
||||||
* @param roles
|
|
||||||
*/
|
|
||||||
export function filterAsyncRoutes(routes, roles) {
|
|
||||||
const res = []
|
|
||||||
|
|
||||||
routes.forEach(route => {
|
|
||||||
const tmp = { ...route }
|
|
||||||
if (hasPermission(roles, tmp)) {
|
|
||||||
if (tmp.children) {
|
|
||||||
tmp.children = filterAsyncRoutes(tmp.children, roles)
|
|
||||||
}
|
|
||||||
res.push(tmp)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
|
|
||||||
const state = {
|
|
||||||
routes: [],
|
|
||||||
addRoutes: []
|
|
||||||
}
|
|
||||||
|
|
||||||
const mutations = {
|
|
||||||
SET_ROUTES: (state, routes) => {
|
|
||||||
state.addRoutes = routes
|
|
||||||
state.routes = constantRoutes.concat(routes)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const actions = {
|
|
||||||
generateRoutes({ commit }, roles) {
|
|
||||||
return new Promise(resolve => {
|
|
||||||
let accessedRoutes
|
|
||||||
if (roles.includes('admin')) {
|
|
||||||
accessedRoutes = asyncRoutes || []
|
|
||||||
} else {
|
|
||||||
accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
|
|
||||||
}
|
|
||||||
commit('SET_ROUTES', accessedRoutes)
|
|
||||||
resolve(accessedRoutes)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default {
|
|
||||||
namespaced: true,
|
|
||||||
state,
|
|
||||||
mutations,
|
|
||||||
actions
|
|
||||||
}
|
|
|
@ -5,8 +5,7 @@ import { resetRouter } from '@/router'
|
||||||
const state = {
|
const state = {
|
||||||
token: getToken(),
|
token: getToken(),
|
||||||
name: '',
|
name: '',
|
||||||
avatar: '',
|
avatar: ''
|
||||||
roles: []
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const mutations = {
|
const mutations = {
|
||||||
|
@ -18,9 +17,6 @@ const mutations = {
|
||||||
},
|
},
|
||||||
SET_AVATAR: (state, avatar) => {
|
SET_AVATAR: (state, avatar) => {
|
||||||
state.avatar = avatar
|
state.avatar = avatar
|
||||||
},
|
|
||||||
SET_ROLES: (state, roles) => {
|
|
||||||
state.roles = roles
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,14 +46,8 @@ const actions = {
|
||||||
reject('Verification failed, please Login again.')
|
reject('Verification failed, please Login again.')
|
||||||
}
|
}
|
||||||
|
|
||||||
const { roles, name, avatar } = data
|
const { name, avatar } = data
|
||||||
|
|
||||||
// roles must be a non-empty array
|
|
||||||
if (!roles || roles.length <= 0) {
|
|
||||||
reject('getInfo: roles must be a non-null array!')
|
|
||||||
}
|
|
||||||
|
|
||||||
commit('SET_ROLES', roles)
|
|
||||||
commit('SET_NAME', name)
|
commit('SET_NAME', name)
|
||||||
commit('SET_AVATAR', avatar)
|
commit('SET_AVATAR', avatar)
|
||||||
resolve(data)
|
resolve(data)
|
||||||
|
@ -72,7 +62,6 @@ const actions = {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
logout(state.token).then(() => {
|
logout(state.token).then(() => {
|
||||||
commit('SET_TOKEN', '')
|
commit('SET_TOKEN', '')
|
||||||
commit('SET_ROLES', [])
|
|
||||||
removeToken()
|
removeToken()
|
||||||
resetRouter()
|
resetRouter()
|
||||||
resolve()
|
resolve()
|
||||||
|
@ -86,7 +75,6 @@ const actions = {
|
||||||
resetToken({ commit }) {
|
resetToken({ commit }) {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
commit('SET_TOKEN', '')
|
commit('SET_TOKEN', '')
|
||||||
commit('SET_ROLES', [])
|
|
||||||
removeToken()
|
removeToken()
|
||||||
resolve()
|
resolve()
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
<template>
|
<template>
|
||||||
<div class="dashboard-container">
|
<div class="dashboard-container">
|
||||||
<div class="dashboard-text">name: {{ name }}</div>
|
<div class="dashboard-text">name: {{ name }}</div>
|
||||||
<div class="dashboard-text">roles: <span v-for="role in roles" :key="role">{{ role }}</span></div>
|
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
@ -12,8 +11,7 @@ export default {
|
||||||
name: 'Dashboard',
|
name: 'Dashboard',
|
||||||
computed: {
|
computed: {
|
||||||
...mapGetters([
|
...mapGetters([
|
||||||
'name',
|
'name'
|
||||||
'roles'
|
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue