Compare commits
21 Commits
master
...
permission
Author | SHA1 | Date |
---|---|---|
|
0dce769533 | |
|
7bc8ff75bb | |
|
4d858104e6 | |
|
bbabeda803 | |
|
d3d51d55d3 | |
|
897681f46e | |
|
9d50637a92 | |
|
bfa2065ba3 | |
|
61d305b8dd | |
|
d22a1ae14d | |
|
86a516aa2e | |
|
1a4e34beba | |
|
dc41791863 | |
|
7f921f08fc | |
|
fca7bfd749 | |
|
6d449fe778 | |
|
49645aef79 | |
|
27d06dd662 | |
|
434a04ee3f | |
|
5b77ad6f79 | |
|
882ba9d148 |
|
@ -12,7 +12,7 @@
|
||||||
:collapse-transition="false"
|
:collapse-transition="false"
|
||||||
mode="vertical"
|
mode="vertical"
|
||||||
>
|
>
|
||||||
<sidebar-item v-for="route in routes" :key="route.path" :item="route" :base-path="route.path" />
|
<sidebar-item v-for="route in permission_routes" :key="route.path" :item="route" :base-path="route.path" />
|
||||||
</el-menu>
|
</el-menu>
|
||||||
</el-scrollbar>
|
</el-scrollbar>
|
||||||
</div>
|
</div>
|
||||||
|
@ -28,11 +28,9 @@ 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,15 +26,25 @@ router.beforeEach(async(to, from, next) => {
|
||||||
next({ path: '/' })
|
next({ path: '/' })
|
||||||
NProgress.done()
|
NProgress.done()
|
||||||
} else {
|
} else {
|
||||||
const hasGetUserInfo = store.getters.name
|
// determine whether the user has obtained his permission roles through getInfo
|
||||||
if (hasGetUserInfo) {
|
const hasRoles = store.getters.roles && store.getters.roles.length > 0
|
||||||
|
if (hasRoles) {
|
||||||
next()
|
next()
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
// get user info
|
// get user info
|
||||||
await store.dispatch('user/getInfo')
|
// note: roles must be a object array! such as: ['admin'] or ,['developer','editor']
|
||||||
|
const { roles } = await store.dispatch('user/getInfo')
|
||||||
|
|
||||||
next()
|
// generate accessible routes map based on roles
|
||||||
|
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,8 +88,14 @@ 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,6 +3,8 @@ 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,6 +2,7 @@ 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'
|
||||||
|
|
||||||
|
@ -10,6 +11,7 @@ Vue.use(Vuex)
|
||||||
const store = new Vuex.Store({
|
const store = new Vuex.Store({
|
||||||
modules: {
|
modules: {
|
||||||
app,
|
app,
|
||||||
|
permission,
|
||||||
settings,
|
settings,
|
||||||
user
|
user
|
||||||
},
|
},
|
||||||
|
|
|
@ -0,0 +1,69 @@
|
||||||
|
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
|
||||||
|
}
|
|
@ -6,7 +6,8 @@ const getDefaultState = () => {
|
||||||
return {
|
return {
|
||||||
token: getToken(),
|
token: getToken(),
|
||||||
name: '',
|
name: '',
|
||||||
avatar: ''
|
avatar: '',
|
||||||
|
roles: []
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,6 +25,9 @@ const mutations = {
|
||||||
},
|
},
|
||||||
SET_AVATAR: (state, avatar) => {
|
SET_AVATAR: (state, avatar) => {
|
||||||
state.avatar = avatar
|
state.avatar = avatar
|
||||||
|
},
|
||||||
|
SET_ROLES: (state, roles) => {
|
||||||
|
state.roles = roles
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,8 +57,14 @@ const actions = {
|
||||||
reject('Verification failed, please Login again.')
|
reject('Verification failed, please Login again.')
|
||||||
}
|
}
|
||||||
|
|
||||||
const { name, avatar } = data
|
const { roles, 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)
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
<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>
|
||||||
|
|
||||||
|
@ -11,7 +12,8 @@ export default {
|
||||||
name: 'Dashboard',
|
name: 'Dashboard',
|
||||||
computed: {
|
computed: {
|
||||||
...mapGetters([
|
...mapGetters([
|
||||||
'name'
|
'name',
|
||||||
|
'roles'
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue