Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
W
web-monitor
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
web-monitor
Commits
b6147d33
Commit
b6147d33
authored
Apr 19, 2019
by
Pan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
remove permission control
parent
b0f58ee3
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
13 additions
and
114 deletions
+13
-114
index.vue
src/layout/components/Sidebar/index.vue
+4
-2
permission.js
src/permission.js
+4
-14
index.js
src/router/index.js
+1
-7
getters.js
src/store/getters.js
+1
-3
index.js
src/store/index.js
+0
-2
permission.js
src/store/modules/permission.js
+0
-69
user.js
src/store/modules/user.js
+2
-14
index.vue
src/views/dashboard/index.vue
+1
-3
No files found.
src/layout/components/Sidebar/index.vue
View file @
b6147d33
...
...
@@ -12,7 +12,7 @@
:collapse-transition=
"false"
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-scrollbar>
</div>
...
...
@@ -28,9 +28,11 @@ 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
...
...
src/permission.js
View file @
b6147d33
...
...
@@ -26,25 +26,15 @@ router.beforeEach(async(to, from, next) => {
next
({
path
:
'/'
})
NProgress
.
done
()
}
else
{
// determine whether the user has obtained his permission roles through getInfo
const
hasRoles
=
store
.
getters
.
roles
&&
store
.
getters
.
roles
.
length
>
0
if
(
hasRoles
)
{
const
hasGetUserInfo
=
store
.
getters
.
name
if
(
hasGetUserInfo
)
{
next
()
}
else
{
try
{
// get user info
// note: roles must be a object array! such as: ['admin'] or ,['developer','editor']
const
{
roles
}
=
await
store
.
dispatch
(
'user/getInfo'
)
await
store
.
dispatch
(
'user/getInfo'
)
// 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
})
next
()
}
catch
(
error
)
{
// remove token and go to login page to re-login
await
store
.
dispatch
(
'user/resetToken'
)
...
...
src/router/index.js
View file @
b6147d33
...
...
@@ -88,14 +88,8 @@ 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
,
...
...
src/store/getters.js
View file @
b6147d33
...
...
@@ -3,8 +3,6 @@ const getters = {
device
:
state
=>
state
.
app
.
device
,
token
:
state
=>
state
.
user
.
token
,
avatar
:
state
=>
state
.
user
.
avatar
,
name
:
state
=>
state
.
user
.
name
,
roles
:
state
=>
state
.
user
.
roles
,
permission_routes
:
state
=>
state
.
permission
.
routes
name
:
state
=>
state
.
user
.
name
}
export
default
getters
src/store/index.js
View file @
b6147d33
...
...
@@ -2,7 +2,6 @@ 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'
...
...
@@ -11,7 +10,6 @@ Vue.use(Vuex)
const
store
=
new
Vuex
.
Store
({
modules
:
{
app
,
permission
,
settings
,
user
},
...
...
src/store/modules/permission.js
deleted
100644 → 0
View file @
b0f58ee3
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
}
src/store/modules/user.js
View file @
b6147d33
...
...
@@ -5,8 +5,7 @@ import { resetRouter } from '@/router'
const
state
=
{
token
:
getToken
(),
name
:
''
,
avatar
:
''
,
roles
:
[]
avatar
:
''
}
const
mutations
=
{
...
...
@@ -18,9 +17,6 @@ const mutations = {
},
SET_AVATAR
:
(
state
,
avatar
)
=>
{
state
.
avatar
=
avatar
},
SET_ROLES
:
(
state
,
roles
)
=>
{
state
.
roles
=
roles
}
}
...
...
@@ -50,14 +46,8 @@ const actions = {
reject
(
'Verification failed, please Login again.'
)
}
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!'
)
}
const
{
name
,
avatar
}
=
data
commit
(
'SET_ROLES'
,
roles
)
commit
(
'SET_NAME'
,
name
)
commit
(
'SET_AVATAR'
,
avatar
)
resolve
(
data
)
...
...
@@ -72,7 +62,6 @@ const actions = {
return
new
Promise
((
resolve
,
reject
)
=>
{
logout
(
state
.
token
).
then
(()
=>
{
commit
(
'SET_TOKEN'
,
''
)
commit
(
'SET_ROLES'
,
[])
removeToken
()
resetRouter
()
resolve
()
...
...
@@ -86,7 +75,6 @@ const actions = {
resetToken
({
commit
})
{
return
new
Promise
(
resolve
=>
{
commit
(
'SET_TOKEN'
,
''
)
commit
(
'SET_ROLES'
,
[])
removeToken
()
resolve
()
})
...
...
src/views/dashboard/index.vue
View file @
b6147d33
<
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
>
...
...
@@ -12,8 +11,7 @@ export default {
name
:
'Dashboard'
,
computed
:
{
...
mapGetters
([
'name'
,
'roles'
'name'
])
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment