Skip to content

Commit

Permalink
Merge pull request #72 from Antabot/dev
Browse files Browse the repository at this point in the history
feat: permissions assignment function
  • Loading branch information
Antabot authored Dec 14, 2019
2 parents e5efd45 + 4b368cf commit e71e24f
Show file tree
Hide file tree
Showing 14 changed files with 174 additions and 59 deletions.
2 changes: 1 addition & 1 deletion wj-vue/src/components/admin/content/BookManagement.vue
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
type: 'warning'
}).then(() => {
this.$axios
.post('/delete', {id: id}).then(resp => {
.post('/admin/content/books/delete', {id: id}).then(resp => {
if (resp && resp.status === 200) {
this.loadBooks()
}
Expand Down
2 changes: 1 addition & 1 deletion wj-vue/src/components/admin/content/EditForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
},
onSubmit () {
this.$axios
.post('/books', {
.post('/admin/content/books', {
id: this.form.id,
cover: this.form.cover,
title: this.form.title,
Expand Down
2 changes: 1 addition & 1 deletion wj-vue/src/components/admin/content/ImgUpload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<el-upload
class="img-upload"
ref="upload"
action="http://localhost:8443/api/covers"
action="http://localhost:8443/api/admin/content/books/covers"
:on-preview="handlePreview"
:on-remove="handleRemove"
:before-remove="beforeRemove"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
<template>
<div>
<el-dialog
title="修改角色信息"
:visible.sync="dialogFormVisible">
<el-form v-model="selectedRole" style="text-align: left" ref="dataForm">
<el-form-item label="角色名" label-width="120px" prop="username">
<el-input v-model="selectedRole.name" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="角色描述" label-width="120px" prop="name">
<el-input v-model="selectedRole.nameZh" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="功能配置" label-width="120px" prop="perms">
<el-checkbox-group v-model="selectedPerms">
<el-checkbox v-for="(perm,i) in perms" :key="i" :label="perm.id">{{perm.desc_}}</el-checkbox>
</el-checkbox-group>
</el-form-item>
<el-form-item label="菜单配置" label-width="120px" prop="menus">
<el-checkbox-group v-model="selectedMenus">
<el-checkbox v-for="(menu,i) in menus" :key="i" :label="perm.id">{{menu.nameZh}}</el-checkbox>
</el-checkbox-group>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogFormVisible = false">取 消</el-button>
<el-button type="primary" @click="onSubmit(selectedRole)">确 定</el-button>
</div>
</el-dialog>
<el-row style="margin: 18px 0px 0px 18px ">
<el-breadcrumb separator-class="el-icon-arrow-right">
<el-breadcrumb-item :to="{ path: '/admin/dashboard' }">管理中心</el-breadcrumb-item>
Expand Down Expand Up @@ -41,7 +67,7 @@
active-color="#13ce66"
inactive-color="#ff4949"
@click.native="beforeUpdate"
@change="(value) => commitChange(value, scope.row)">
@change="(value) => commitStatusChange(value, scope.row)">
</el-switch>
</template>
</el-table-column>
Expand All @@ -50,13 +76,12 @@
width="120">
<template slot-scope="scope">
<el-button
@click.native.prevent="editBook(scope.row)"
type="text"
size="small">
size="small"
@click="editRole(scope.row)">
编辑
</el-button>
<el-button
@click.native.prevent="deleteBook(scope.row.id)"
type="text"
size="small">
移除
Expand All @@ -77,11 +102,18 @@
name: 'UserRole',
data () {
return {
roles: []
dialogFormVisible: false,
roles: [],
perms: [],
menus: [],
selectedRole: [],
selectedPerms: [],
selectedMenus: []
}
},
mounted () {
this.listRoles()
this.listPerms()
},
computed: {
tableHeight () {
Expand All @@ -97,16 +129,22 @@
}
})
},
beforeUpdate () {
listPerms () {
var _this = this
this.$axios.get('/admin/perm').then(resp => {
if (resp && resp.status === 200) {
_this.perms = resp.data
}
})
},
commitChange (value, role) {
commitStatusChange (value, role) {
if (role.id !== 1) {
this.$confirm('是否更改角色状态?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
this.$axios.put('/admin/role', {
this.$axios.put('/admin/role/status', {
enabled: value,
id: role.id
}).then(resp => {
Expand All @@ -129,6 +167,40 @@
role.enabled = true
this.$alert('无法禁用系统管理员!')
}
},
onSubmit (role) {
let _this = this
// 根据视图绑定的角色 id 向后端传送角色信息
let perms = []
for (let i = 0; i < _this.selectedPerms.length; i++) {
for (let j = 0; j < _this.perms.length; j++) {
if (_this.selectedPerms[i] === _this.perms[j].id) {
perms.push(_this.perms[j])
}
}
}
this.$axios.put('/admin/role', {
id: role.id,
name: role.name,
nameZh: role.nameZh,
enabled: role.enabled,
perms: perms
}).then(resp => {
if (resp && resp.status === 200) {
this.$alert(resp.data.data)
this.dialogFormVisible = false
this.listRoles()
}
})
},
editRole (role) {
this.dialogFormVisible = true
this.selectedRole = role
let permIds = []
for (let i = 0; i < role.perms.length; i++) {
permIds.push(role.perms[i].id)
}
this.selectedPerms = permIds
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions wj-vue/src/components/admin/user/UserProfile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@
编辑
</el-button>
<el-button
@click.native.prevent="deleteBook(scope.row.id)"
type="text"
size="small">
移除
Expand Down Expand Up @@ -158,7 +157,7 @@
},
commitStatusChange (value, user) {
if (user.username !== 'admin') {
this.$axios.put('/admin/user-status', {
this.$axios.put('/admin/user/status', {
enabled: value,
username: user.username
}).then(resp => {
Expand Down Expand Up @@ -211,7 +210,7 @@
this.selectedRoles = roleIds
},
resetPassword (username) {
this.$axios.put('/admin/password', {
this.$axios.put('/admin/user/password', {
username: username
}).then(resp => {
if (resp && resp.status === 200) {
Expand Down
19 changes: 9 additions & 10 deletions wj/src/main/java/com/gm/wj/controller/LibraryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,23 @@ public class LibraryController {
BookService bookService;

@GetMapping("/api/books")
public List<Book> listBooks() throws Exception {
public List<Book> listBooks() {
return bookService.list();
}

@PostMapping("/api/books")
public Book addOrUpdateBooks(@RequestBody Book book) throws Exception {
System.out.println(book.getCategory());
@PostMapping("/api/admin/content/books")
public Book addOrUpdateBooks(@RequestBody Book book) {
bookService.addOrUpdate(book);
return book;
}

@PostMapping("/api/delete")
public void deleteBook(@RequestBody Book book) throws Exception {
@PostMapping("/api/admin/content/books/delete")
public void deleteBook(@RequestBody Book book) {
bookService.deleteById(book.getId());
}

@PostMapping("/api/search")
public List<Book> searchResult(@RequestBody Search s) throws Exception {
public List<Book> searchResult(@RequestBody Search s) {
if ("".equals(s.getKeywords())) {
return bookService.list();
} else {
Expand All @@ -45,16 +44,16 @@ public List<Book> searchResult(@RequestBody Search s) throws Exception {
}

@GetMapping("/api/categories/{cid}/books")
public List<Book> listByCategory(@PathVariable("cid") int cid) throws Exception {
public List<Book> listByCategory(@PathVariable("cid") int cid) {
if (0 != cid) {
return bookService.listByCategory(cid);
} else {
return listBooks();
}
}

@PostMapping("/api/covers")
public String coversUpload(MultipartFile file) throws Exception {
@PostMapping("/api/admin/content/books/covers")
public String coversUpload(MultipartFile file) {
String folder = "D:/workspace/img";
File imageFolder = new File(folder);
File f = new File(imageFolder, getRandomString(6) + file.getOriginalFilename()
Expand Down
41 changes: 26 additions & 15 deletions wj/src/main/java/com/gm/wj/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.gm.wj.controller;

import com.gm.wj.pojo.AdminPermission;
import com.gm.wj.pojo.AdminRole;
import com.gm.wj.pojo.User;
import com.gm.wj.result.Result;
import com.gm.wj.result.ResultFactory;
import com.gm.wj.service.AdminRoleService;
import com.gm.wj.service.AdminUserRoleService;
import com.gm.wj.service.UserService;
import com.gm.wj.service.*;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -25,13 +24,17 @@ public class UserController {
AdminRoleService adminRoleService;
@Autowired
AdminUserRoleService adminUserRoleService;
@Autowired
AdminPermissionService adminPermissionService;
@Autowired
AdminRolePermissionService adminRolePermissionService;

@GetMapping("/api/admin/user")
public List<User> listUsers() throws Exception {
return userService.list();
}

@PutMapping("/api/admin/user-status")
@PutMapping("/api/admin/user/status")
public Result updateUserStatus(@RequestBody User requestUser) {
User user = userService.findByUserName(requestUser.getUsername());
user.setEnabled(requestUser.isEnabled());
Expand All @@ -40,21 +43,16 @@ public Result updateUserStatus(@RequestBody User requestUser) {
return ResultFactory.buildSuccessResult(message);
}

@PutMapping("/api/admin/password")
@PutMapping("/api/admin/user/password")
public Result resetPassword(@RequestBody User requestUser) {
User user = userService.findByUserName(requestUser.getUsername());
String salt = new SecureRandomNumberGenerator().nextBytes().toString();
int times = 2;
user.setSalt(salt);
if (requestUser.getPassword() == null) {
String encodedPassword = new SimpleHash("md5", "123", salt, times).toString();
user.setPassword(encodedPassword);
} else {
String encodedPassword = new SimpleHash("md5", requestUser.getPassword(), salt, times).toString();
user.setPassword(encodedPassword);
}
String encodedPassword = new SimpleHash("md5", "123", salt, times).toString();
user.setPassword(encodedPassword);
userService.addOrUpdate(user);
String message = "修改密码成功";
String message = "重置密码成功";
return ResultFactory.buildSuccessResult(message);
}

Expand All @@ -71,16 +69,29 @@ public Result editUser(@RequestBody User requestUser) {
}

@GetMapping("/api/admin/role")
public List<AdminRole> listRoles() throws Exception {
public List<AdminRole> listRoles(){
return adminRoleService.list();
}

@PutMapping("/api/admin/role")
@PutMapping("/api/admin/role/status")
public Result updateRoleStatus(@RequestBody AdminRole requestRole) {
AdminRole adminRole = adminRoleService.findById(requestRole.getId());
adminRole.setEnabled(requestRole.isEnabled());
adminRoleService.addOrUpdate(adminRole);
String message = "用户" + adminRole.getNameZh() + "状态更新成功";
return ResultFactory.buildSuccessResult(message);
}

@PutMapping("/api/admin/role")
public Result editRole(@RequestBody AdminRole requestRole) {
adminRoleService.addOrUpdate(requestRole);
adminRolePermissionService.savePermChanges(requestRole.getId(), requestRole.getPerms());
String message = "修改角色信息成功";
return ResultFactory.buildSuccessResult(message);
}

@GetMapping("/api/admin/perm")
public List<AdminPermission> listPerms() {
return adminPermissionService.list();
}
}
1 change: 1 addition & 0 deletions wj/src/main/java/com/gm/wj/dao/AdminRolePermissionDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@

public interface AdminRolePermissionDAO extends JpaRepository<AdminRolePermission, Integer> {
List<AdminRolePermission> findAllByRid(int rid);
void deleteAllByRid(int rid);
}
3 changes: 2 additions & 1 deletion wj/src/main/java/com/gm/wj/filter/URLPathMatchingFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ protected boolean onPreHandle(ServletRequest request, ServletResponse response,
String username = subject.getPrincipal().toString();
Set<String> permissionAPIs = adminPermissionService.listPermissionURLsByUser(username);
for (String api : permissionAPIs) {
if (api.equals(requestAPI)) {
// 匹配前缀
if (requestAPI.startsWith(api)) {
hasPermission = true;
break;
}
Expand Down
13 changes: 11 additions & 2 deletions wj/src/main/java/com/gm/wj/pojo/AdminRole.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import javax.persistence.*;
import java.util.List;

@Entity
@Table(name = "admin_role")
Expand All @@ -12,12 +13,12 @@ public class AdminRole {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
int id;

String name;
@Column(name = "name_zh")
String nameZh;

boolean enabled;
@Transient
List<AdminPermission> perms;

public int getId() {
return id;
Expand Down Expand Up @@ -50,4 +51,12 @@ public boolean isEnabled() {
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public List<AdminPermission> getPerms() {
return perms;
}

public void setPerms(List<AdminPermission> perms) {
this.perms = perms;
}
}
Loading

0 comments on commit e71e24f

Please sign in to comment.