8000 debug access control · pycoder404/blog-vue@9dc0eb6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9dc0eb6

Browse files
committed
debug access control
1 parent 9faeae6 commit 9dc0eb6

File tree

4 files changed

+63
-64
lines changed

4 files changed

+63
-64
lines changed

src/permission.js

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,22 @@ import getPageTitle from '@/utils/get-page-title'
88

99
NProgress.configure({showSpinner: false}) // NProgress Configuration
1010
// todo permission,博客相对后台管理系统只需要管理部分的路由即可
11-
const whiteList = ['/login', '/article/list','/auth-redirect','/login/github'] // no redirect whitelist
11+
// const whiteList = ['/login', '/article/list', '/login/github', '/article/detail'] // no redirect whitelist
12+
13+
function checkPermission(roles, to) {
14+
console.info("check permission")
15+
const permittedRoles = to.meta.roles
16+
let accessRoles = roles.filter(function (v) {
17+
permittedRoles.indexOf(v) > -1
18+
})
19+
return accessRoles.length > 0
20+
}
1221

1322
router.beforeEach(async (to, from, next) => {
1423
// start progress bar
1524
NProgress.start()
16-
// console.log("before route change")
17-
// console.log(to.path)
25+
console.log("before route change")
26+
console.log(to.path)
1827
// set page title
1928
document.title = getPageTitle(to.meta.title)
2029
// console.info(to)
@@ -28,83 +37,74 @@ router.beforeEach(async (to, from, next) => {
2837
delete to.query.code
2938
delete to.query.thirdPart
3039
// console.info("begin to social auth login")
31-
await store.dispatch('user/socialLogin',{'thirdPart': thirdPart, 'oauthCode': oauthCode})
40+
await store.dispatch('user/socialLogin', {'thirdPart': thirdPart, 'oauthCode': oauthCode})
3241
// console.info("social login done")
3342
} catch (error) {
3443
// remove token and go to login page to re-login
3544
await store.dispatch('user/resetToken')
36-
ElMessage.error('Error in login by ',thirdPart)
45+
ElMessage.error('Error in login by ', thirdPart)
3746
next({...to, replace: true})
3847
NProgress.done()
3948
}
4049

4150
}
4251

4352
// determine whether the user has logged in
53+
console.info("check if hastoken")
54+
let hasPagePermission = false
4455
const hasAccessToken = getAccessToken()
4556
if (hasAccessToken) {
46-
console.log("has access token:",hasAccessToken)
47-
// TODO 添加对accessToken的过期检查和refresh
48-
// fixme change to check permissions
57+
console.info("hastoken")
4958

50-
if (to.path === '/login') {
51-
// if is logged in, redirect to the home page
52-
const next_path = to.query && to.query.redirect ? to.query.redirect : '/'
53-
next({path: next_path})
54-
NProgress.done() // hack: https://github.com/PanJiaChen/vue-element-admin/pull/2939
55-
} else {
56-
// determine whether the user has obtained his permission roles through getInfo
57-
console.log("check is has roles")
58-
// console.log(store.getters.roles)
59-
// note Ctrl + F5强制刷新界面,对store有啥影响吗,为啥会导致没有roles,重新获取Info??
60-
// note: 因为store是存在内存中的,所以每次刷新就会判断为空,需要重新获取数据,而cookie保存在本地,所以刷新不会丢失
61-
const hasRoles = store.getters.roles && store.getters.roles.length > 0
62-
if (hasRoles) {
63-
console.log("yes has roles")
64-
next()
65-
console.log('next done')
66-
} else {
67-
console.log('can not get roles from store')
68-
try {
69-
// get user info
70-
// question: roles must be a object array! such as: ['admin'] or ,['developer','editor']
71-
// question 这里的roles是user/GetInfo如何反馈的,如何只反馈roles的
72-
// note: await 's result and 对象解构赋值
73-
const { roles } = await store.dispatch('user/getInfo')
74-
console.log(roles)
75-
// // generate accessible routes map based on roles
76-
// const accessRoutes = await store.dispatch('permission/generateRoutes', roles)
77-
//
78-
// // dynamically add accessible routes
79-
// router.addRoutes(accessRoutes)
59+
let roles = store.getters.roles && store.getters.roles.length > 0
60+
if (roles) {
61+
console.info("has roles")
8062

81-
// hack method to ensure that addRoutes is complete
82-
// set the replace: true, so the navigation will not leave a history record
83-
next({...to, replace: true})
84-
} catch (error) {
85-
// remove token and go to login page to re-login
86-
await store.dispatch('user/resetToken')
87-
ElMessage.error(error || 'Has Error')
88-
next(`/login?redirect=${to.path}`)
89-
NProgress.done()
90-
}
63+
hasPagePermission = checkPermission(roles, to)
64+
} else {
65+
// 对页面进行刷新后重新获取下info
66+
try {
67+
// get user info
68+
// question 这里的roles是user/GetInfo如何反馈的,如何只反馈roles的
69+
// note: await 's result and 对象解构赋值
70+
let {roles} = await store.dispatch('user/getInfo')
71+
hasPagePermission = checkPermission(roles, to)
72+
next({...to, replace: true})
73+
} catch (error) {
74+
// remove token and go to login page to re-login
75+
await store.dispatch('user/resetToken')
76+
ElMessage.error(error || 'Has Error')
77+
next({...from, replace: true})
78+
// NProgress.done()
9179
}
9280
}
81+
9382
} else {
9483
/* has no token*/
84+
let roles = ['anonymous']
85+
hasPagePermission = checkPermission(roles, to)
86+
87+
}
88+
console.info("check permission done")
89+
console.log(hasPagePermission)
90+
if (hasPagePermission) {
91+
console.info("has permission done")
92+
93+
next({...to, replace: true})
94+
// NProgress.done()
95+
96+
} else {
97+
ElMessage.error("Access denied")
98+
console.info("access denied permission done")
99+
100+
next({...from, replace: true})
101+
// NProgress.done()
95102

96-
if (whiteList.indexOf(to.path) !== -1) {
97-
// in the free login whitelist, go directly
98-
next()
99-
} else {
100-
// other pages that do not have permission to access are redirected to the login page.
101-
next(`/login?redirect=${to.path}`)
102-
NProgress.done()
103-
}
104103
}
105104
})
106105

107106
router.afterEach(() => {
108107
// finish progress bar
109108
NProgress.done()
110109
})
110+

src/router/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,29 @@ const routes = [
2626
path: 'list',
2727
component: ArticleList,
2828
name: 'articleListPage',
29-
meta: { title: 'ArticleList'}
29+
meta: {
30+
title: 'ArticleList', roles: ['anonymous','guest']
31+
}
3032

3133
},
3234
{
3335
path: 'detail/:id(\\d+)/',
3436
component: ArticleDetail,
3537
name: 'articleDetailPage',
36-
meta: { title: 'ArticleDetail'}
38+
meta: {title: 'ArticleDetail', roles: ['anonymous','guest']}
3739

3840
},
3941
{
4042
path: 'edit/:id(\\d+)/',
4143
component: ArticleEdit,
4244
name: 'articleEditPage',
43-
meta: { title: 'ArticleEdit'}
44-
45-
45+
meta: {title: 'ArticleEdit', roles: ['admin']}
4646
},
4747
{
4848
path: 'create',
4949
component: ArticleCreate,
5050
name: 'articleCreatePage',
51-
meta: { title: 'ArticleCreate'}
51+
meta: {title: 'ArticleCreate', roles: ['admin']}
5252
}
5353
]
5454
}

src/utils/request.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ axios.defaults.withCredentials = true
77
const service = axios.create({
88
baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
99
withCredentials: true, // send cookies when cross-domain requests
10-
timeout: 15000 // request timeout
10+
timeout: 60 < 941A span class="pl-c1 x">* 1000 // request timeout, 60s
1111
})
1212
// request interceptor
1313
service.interceptors.request.use(

src/views/login/components/SocialSignin.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
// const url = 'https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=' + client_id + '&redirect_uri=' + redirect_uri
3737
const thirdPart = window.location.search === '' ? "?thirdPart=github" : "&thirdPart=github"
3838
const redirect_uri = window.location.href + thirdPart
39-
console.info(redirect_uri)
4039
return this.githubAuthBaseUri + GITHUBCLIENTID + "&redirect_uri=" + encodeURIComponent(redirect_uri)
4140
}
4241
}

0 commit comments

Comments
 (0)
0