8000 only admin user can create article · pycoder404/blog-vue@aa1898f · GitHub
[go: up one dir, main page]

Skip to content

Commit aa1898f

Browse files
committed
only admin user can create article
1 parent f662c25 commit aa1898f

File tree

3 files changed

+39
-28
lines changed

3 files changed

+39
-28
lines changed

src/layout/components/MainHeader.vue

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,40 @@
44
<router-link to="/" class="left-menu-item">HOME</router-link>
55
<router-link to="/article/list" class="left-menu-item">LIST</router-link>
66
<router-link to="/article/list" class="left-menu-item">ARCHIVE</router-link>
7-
<router-link to="/article/create" class="left-menu-item">NEW</router-link>
7+
<router-link v-if=isAdmin to="/article/create" class="left-menu-item">NEW</router-link>
88

99
</div>
1010

1111
<div class="right-menu">
12-
<el-button class="right-menu-item"> <el-icon><SearchIcon /></el-icon></el-button>
12+
<el-button class="right-menu-item">
13+
<el-icon>
14+
<SearchIcon/>
15+
</el-icon>
16+
</el-button>
1317
<el-dropdown class="avatar-container right-menu-item hover-effect" trigger="click">
1418
<div class="avatar-wrapper">
1519
<el-avatar shape="square" :size="30" fit="fill" :src="avatar"></el-avatar>
16-
<el-icon><CaretBottom /></el-icon>
20+
<el-icon>
21+
<CaretBottom/>
22+
</el-icon>
1723
</div>
1824

1925
<template #dropdown>
20-
<el-dropdown-menu>
21-
<router-link to="/">
22-
<el-dropdown-item>Home</el-dropdown-item>
23-
</router-link>
24-
<router-link to="/article/create">
25-
<el-dropdown-item>New</el-dropdown-item>
26-
</router-link>
27-
28-
<a target="_blank" href="https://github.com/pycoder404/blog-vue">
29-
<el-dropdown-item>Github</el-dropdown-item>
30-
</a>
31-
<el-dropdown-item divided @click="logout">
32-
<span style="display:block;">Log Out</span>
33-
</el-dropdown-item>
34-
</el-dropdown-menu>
26+
<el-dropdown-menu>
27+
<router-link to="/">
28+
<el-dropdown-item>Home</el-dropdown-item>
29+
</router-link>
30+
<router-link to="/article/create">
31+
<el-dropdown-item>New</el-dropdown-item>
32+
</router-link>
33+
34+
<a target="_blank" href="https://github.com/pycoder404/blog-vue">
35+
<el-dropdown-item>Github</el-dropdown-item>
36+
</a>
37+
<el-dropdown-item divided @click="logout">
38+
<span style="display:block;">Log Out</span>
39+
</el-dropdown-item>
40+
</el-dropdown-menu>
3541
</template>
3642
</el-dropdown>
3743
</div>
@@ -45,28 +51,31 @@
4551
import {mapGetters} from 'vuex'
4652
// import Breadcrumb from '@/components/Breadcrumb'
4753
// import Hamburger from '@/components/Hamburger'
48-
import {ref} from 'vue'
54+
// import {ref} from 'vue'
4955
import SvgIcon from '@/components/SvgIcon/index'
5056
5157
export default {
5258
components: {
5359
SvgIcon
5460
},
5561
data() {
56-
return {
57-
}
62+
return {}
5863
5964
},
6065
6166
computed: {
6267
...mapGetters([
63-
'avatar'
68+
'avatar',
69+
'isAdmin',
6470
])
6571
},
6672
6773
methods: {
6874
async logout() {
6975
await this.$store.dispatch('user/logout')
76+
// fixme if push to this.$route.fullPath, because the route path is not change,
77+
// fixme so the page is not reloaded
78+
// this.$router.push(this.$route.fullPath)
7079
this.$router.push(`/login?redirect=${this.$route.fullPath}`)
7180
}
7281
}
@@ -82,11 +91,12 @@
8291
/*background:#304156;*/
8392
box-shadow: 0 1px 4px rgba(0, 21, 41, .08);
8493
padding: 4px 32px;
94+
8595
.left-menu {
8696
float: left;
8797
height: 100%;
8898
/*line-height: 30px;*/
89-
padding:10px 1px;
99+
padding: 10px 1px;
90100
91101
&:focus {
92102
outline: none;
@@ -209,6 +219,4 @@
209219
}
210220
211221
212-
213-
214222
</style>

src/store/getters.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ const getters = {
77
username: state => state.user.username,
88
introduction: state => state.user.introduction,
99
roles: state => state.user.roles,
10+
isLogin: state => state.user.roles.length > 0,
11+
isAdmin: state => state.user.roles.indexOf('admin') > -1,
1012
}
1113
export default getters

src/views/article/list.vue

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
<div class="article-desc">
1818
<span>
1919
@{{article.created_time }} {{ article.author }}, views:{{article.views_count}} likes:{{article.likes_count}} comments:{{article.comments_count}}
20-
<el-button v-if=false icon="DeleteFilled" text/>
20+
<el-button v-if=isAdmin icon="DeleteFilled" text/>
2121
<router-link
22-
v-if="false"
22+
v-if="isAdmin"
2323
:to="'/article/edit/'+ article.id"
2424
class="article-title"
2525
><el-button
@@ -83,7 +83,8 @@
8383
},
8484
computed: {
8585
...mapGetters([
86-
'roles'
86+
'isLogin',
87+
'isAdmin',
8788
])
8889
},
8990
methods: {

0 commit comments

Comments
 (0)
0