end: 支持vuex管理全局按钮显示权限

This commit is contained in:
wangiegie@gmail.com 2017-11-11 17:50:49 +08:00
parent 21820464a0
commit 3c7d16b0ff
9 changed files with 164 additions and 5 deletions

View File

@ -56,7 +56,7 @@ public class MenuController extends BaseController {
*/
@GetMapping(value = "/userTree")
public List<Integer> userTree(HttpServletRequest request) {
Set<MenuVo> menus = menuService.findMenuByRole(getRole());
Set<MenuVo> menus = menuService.findMenuByRole(getRole().get(0));
List<Integer> menuList = new ArrayList<>();
for (MenuVo menuVo : menus) {
menuList.add(menuVo.getMenuId());

View File

@ -3,8 +3,10 @@ package com.github.pig.admin.controller;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.admin.dto.UserDto;
import com.github.pig.admin.dto.UserInfo;
import com.github.pig.admin.entity.SysUser;
import com.github.pig.admin.entity.SysUserRole;
import com.github.pig.admin.service.SysMenuService;
import com.github.pig.admin.service.SysUserRoleService;
import com.github.pig.admin.service.UserService;
import com.github.pig.common.constant.CommonConstant;
@ -27,6 +29,8 @@ public class UserController extends BaseController {
private UserService userService;
@Autowired
private SysUserRoleService sysUserRoleService;
@Autowired
private SysMenuService menuService;
/**
* 获取当前用户的用户名
@ -34,8 +38,20 @@ public class UserController extends BaseController {
* @return 用户名
*/
@GetMapping("/info")
public String user() {
return getUser();
public UserInfo user() {
SysUser condition = new SysUser();
condition.setUsername(getUser());
SysUser sysUser = userService.selectOne(new EntityWrapper<>(condition));
UserInfo userInfo = new UserInfo();
userInfo.setSysUser(sysUser);
//设置角色列表
String[] roles = getRole().toArray(new String[getRole().size()]);
userInfo.setRoles(roles);
//设置权限列表menu.permission
String[] permissions = menuService.findPermission(roles);
userInfo.setPermissions(permissions);
return userInfo;
}
/**

View File

@ -0,0 +1,55 @@
package com.github.pig.admin.dto;
import com.github.pig.admin.entity.SysUser;
import java.io.Serializable;
/**
* @author lengleng
* @date 2017/11/11
* <p>
* commit('SET_ROLES', data)
* commit('SET_NAME', data)
* commit('SET_AVATAR', data)
* commit('SET_INTRODUCTION', data)
* commit('SET_PERMISSIONS', data)
*/
public class UserInfo implements Serializable {
/**
* 用户基本信息
*/
private SysUser sysUser;
/**
* 权限标识集合
*/
private String[] permissions;
/**
* 角色集合
*/
private String[] roles;
public String[] getPermissions() {
return permissions;
}
public void setPermissions(String[] permissions) {
this.permissions = permissions;
}
public String[] getRoles() {
return roles;
}
public void setRoles(String[] roles) {
this.roles = roles;
}
public SysUser getSysUser() {
return sysUser;
}
public void setSysUser(SysUser sysUser) {
this.sysUser = sysUser;
}
}

View File

@ -54,6 +54,16 @@ public class SysUser extends Model<SysUser> {
@TableField("del_flag")
private String delFlag;
/**
* 简介
*/
private String introduction;
/**
* 头像
*/
private String avatar;
public Integer getUserId() {
return userId;
@ -116,6 +126,22 @@ public class SysUser extends Model<SysUser> {
return this.userId;
}
public String getIntroduction() {
return introduction;
}
public void setIntroduction(String introduction) {
this.introduction = introduction;
}
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
@Override
public String toString() {
return "SysUser{" +

View File

@ -23,4 +23,12 @@ public interface SysMenuService extends IService<SysMenu> {
* @return 菜单列表
*/
Set<MenuVo> findMenuByRole(String role);
/**
* 通过角色获取菜单权限列表
*
* @param roles 角色
* @return 权限列表
*/
String[] findPermission(String[] roles);
}

View File

@ -5,10 +5,12 @@ import com.github.pig.admin.entity.SysMenu;
import com.github.pig.admin.mapper.SysMenuMapper;
import com.github.pig.admin.service.SysMenuService;
import com.github.pig.common.vo.MenuVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.HashSet;
import java.util.Set;
/**
@ -29,4 +31,23 @@ public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> impl
public Set<MenuVo> findMenuByRole(String role) {
return sysMenuMapper.findMenuByRoleName(role);
}
@Override
public String[] findPermission(String[] roles) {
Set<MenuVo> menuVoSet = new HashSet<>();
for (String role : roles) {
Set<MenuVo> menuVos = findMenuByRole(role);
menuVoSet.addAll(menuVos);
}
Set<String> permissions = new HashSet<>();
for (MenuVo menuVo : menuVoSet) {
if (StringUtils.isNotEmpty(menuVo.getPermission())) {
String permission = menuVo.getPermission();
permissions.add(permission);
}
}
return permissions.toArray(new String[permissions.size()]);
}
}

View File

@ -6,6 +6,8 @@
<id column="user_id" property="userId"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
<result column="introduction" property="introduction" />
<result column="avatar" property="avatar" />
<result column="salt" property="salt"/>
<result column="create_time" property="createTime"/>
<result column="update_time" property="updateTime"/>
@ -18,6 +20,8 @@
<result column="username" property="username"/>
<result column="password" property="password"/>
<result column="salt" property="salt"/>
<result column="introduction" property="introduction" />
<result column="avatar" property="avatar" />
<result column="ucreate_time" property="createTime"/>
<result column="uupdate_time" property="updateTime"/>
<result column="udel_flag" property="delFlag"/>
@ -36,6 +40,8 @@
`user`.username,
`user`.`password`,
`user`.salt,
`user`.introduction,
`user`.avatar,
`user`.create_time AS ucreate_time,
`user`.update_time AS uupdate_time,
`user`.del_flag AS udel_flag,
@ -60,6 +66,8 @@
`user`.username,
`user`.`password`,
`user`.salt,
`user`.introduction,
`user`.avatar,
`user`.create_time AS ucreate_time,
`user`.update_time AS uupdate_time,
`user`.del_flag AS udel_flag,

View File

@ -40,6 +40,15 @@ public class UserVo implements Serializable {
* 0-正常1-删除
*/
private String delFlag;
/**
* 简介
*/
private String introduction;
/**
* 头像
*/
private String avatar;
/**
* 角色列表
*/
@ -108,4 +117,20 @@ public class UserVo implements Serializable {
public void setRoleList(List<SysRole> roleList) {
this.roleList = roleList;
}
public String getIntroduction() {
return introduction;
}
public void setIntroduction(String introduction) {
this.introduction = introduction;
}
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
}

View File

@ -36,12 +36,12 @@ public class BaseController {
*
* @return 角色名
*/
public String getRole() {
public List<String> getRole() {
String authorization = request.getHeader(CommonConstant.REQ_HEADER);
String token = StringUtils.substringAfter(authorization, CommonConstant.TOKEN_SPLIT);
String key = Base64.getEncoder().encodeToString(CommonConstant.SIGN_KEY.getBytes());
Claims claims = Jwts.parser().setSigningKey(key).parseClaimsJws(token).getBody();
List<String> roleNames = (List<String>) claims.get("authorities");
return roleNames.get(0);
return roleNames;
}
}