Compare commits

...

21 Commits

Author SHA1 Message Date
花裤衩 0dce769533 Merge branch 'master' into permission-control 2020-06-23 21:15:05 +08:00
花裤衩 7bc8ff75bb Merge branch 'master' into permission-control 2020-06-21 22:05:17 +08:00
花裤衩 4d858104e6 Merge branch 'master' into permission-control 2020-06-21 21:55:10 +08:00
花裤衩 bbabeda803 [release] 4.3.0 2020-06-21 21:54:51 +08:00
花裤衩 d3d51d55d3 Merge branch 'master' into permission-control 2020-06-15 17:16:05 +08:00
花裤衩 897681f46e Merge branch 'master' into permission-control 2020-06-09 16:02:15 +08:00
潘嘉晨 9d50637a92 Merge branch 'master' into permission-control 2020-06-04 21:26:34 +08:00
花裤衩 bfa2065ba3 Merge branch 'master' into permission-control 2020-01-09 20:58:53 +08:00
花裤衩 61d305b8dd merge master 2020-01-09 20:58:40 +08:00
花裤衩 d22a1ae14d fix: logout reset state 2019-12-29 14:33:13 +08:00
花裤衩 86a516aa2e Merge branch 'master' into permission-control 2019-10-22 20:30:41 +08:00
Pan 1a4e34beba Merge branch 'master' into permission-control 2019-07-04 16:40:29 +08:00
Pan dc41791863 Merge branch 'master' into permission-control 2019-05-27 17:02:36 +08:00
Pan 7f921f08fc Merge branch 'master' into permission-control 2019-05-24 16:59:57 +08:00
Pan fca7bfd749 Merge branch 'master' into permission-control 2019-05-22 18:22:16 +08:00
Pan 6d449fe778 Merge branch 'master' into permission-control 2019-05-13 12:57:54 +08:00
Pan 49645aef79 Merge branch 'master' into permission-control 2019-05-08 18:12:14 +08:00
Pan 27d06dd662 Merge branch 'master' into permission-control 2019-05-05 15:55:56 +08:00
Pan 434a04ee3f Merge branch 'master' into permission-control 2019-04-23 09:53:37 +08:00
Pan 5b77ad6f79 Merge branch 'master' into permission-control 2019-04-22 12:42:26 +08:00
Pan 882ba9d148 Revert "remove permission control"
This reverts commit b6147d33f3.
2019-04-21 16:03:05 +08:00
8 changed files with 112 additions and 13 deletions

View File

@ -12,7 +12,7 @@
:collapse-transition="false"
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-scrollbar>
</div>
@ -28,11 +28,9 @@ export default {
components: { SidebarItem, Logo },
computed: {
...mapGetters([
'permission_routes',
'sidebar'
]),
routes() {
return this.$router.options.routes
},
activeMenu() {
const route = this.$route
const { meta, path } = route

View File

@ -26,15 +26,25 @@ router.beforeEach(async(to, from, next) => {
next({ path: '/' })
NProgress.done()
} else {
const hasGetUserInfo = store.getters.name
if (hasGetUserInfo) {
// determine whether the user has obtained his permission roles through getInfo
const hasRoles = store.getters.roles && store.getters.roles.length > 0
if (hasRoles) {
next()
} else {
try {
// 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) {
// remove token and go to login page to re-login
await store.dispatch('user/resetToken')

View File

@ -88,8 +88,14 @@ export const constantRoutes = [
meta: { title: 'Form', icon: 'form' }
}
]
},
}
]
/**
* asyncRoutes
* the routes that need to be dynamically loaded based on user roles
*/
export const asyncRoutes = [
{
path: '/nested',
component: Layout,

View File

@ -3,6 +3,8 @@ const getters = {
device: state => state.app.device,
token: state => state.user.token,
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

View File

@ -2,6 +2,7 @@ import Vue from 'vue'
import Vuex from 'vuex'
import getters from './getters'
import app from './modules/app'
import permission from './modules/permission'
import settings from './modules/settings'
import user from './modules/user'
@ -10,6 +11,7 @@ Vue.use(Vuex)
const store = new Vuex.Store({
modules: {
app,
permission,
settings,
user
},

View File

@ -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
}

View File

@ -6,7 +6,8 @@ const getDefaultState = () => {
return {
token: getToken(),
name: '',
avatar: ''
avatar: '',
roles: []
}
}
@ -24,6 +25,9 @@ const mutations = {
},
SET_AVATAR: (state, avatar) => {
state.avatar = avatar
},
SET_ROLES: (state, roles) => {
state.roles = roles
}
}
@ -53,8 +57,14 @@ const actions = {
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_AVATAR', avatar)
resolve(data)

View File

@ -1,6 +1,7 @@
<template>
<div class="dashboard-container">
<div class="dashboard-text">name: {{ name }}</div>
<div class="dashboard-text">roles: <span v-for="role in roles" :key="role">{{ role }}</span></div>
</div>
</template>
@ -11,7 +12,8 @@ export default {
name: 'Dashboard',
computed: {
...mapGetters([
'name'
'name',
'roles'
])
}
}