完成角色的授权的功能
This commit is contained in:
parent
ad1d1978d2
commit
2651f9bce3
|
@ -111,18 +111,6 @@ public class SysMenuServiceImpl implements ISysMenuService {
|
|||
return menuMapper.selectMenuListByRoleId(roleId, role.isMenuCheckStrictly());
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建前端所需要下拉树结构
|
||||
*
|
||||
* @param menus 菜单列表
|
||||
* @return 下拉树结构列表
|
||||
*/
|
||||
@Override
|
||||
public List<TreeSelect> buildMenuTreeSelect(List<SysMenu> menus) {
|
||||
List<SysMenu> menuTrees = buildMenuTree(menus);
|
||||
return menuTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询菜单使用数量
|
||||
*
|
||||
|
|
|
@ -1,60 +1,52 @@
|
|||
import request from '@/utils/request'
|
||||
|
||||
// 查询菜单列表
|
||||
export function listMenu(query) {
|
||||
return request({
|
||||
url: '/system/menu/list',
|
||||
method: 'get',
|
||||
params: query
|
||||
})
|
||||
}
|
||||
|
||||
// 查询菜单详细
|
||||
export function getMenu(id) {
|
||||
return request({
|
||||
url: '/system/menu/get?id=' + id,
|
||||
method: 'get'
|
||||
})
|
||||
}
|
||||
|
||||
// 查询菜单下拉树结构
|
||||
export function treeselect() {
|
||||
return request({
|
||||
url: '/system/menu/treeselect',
|
||||
method: 'get'
|
||||
})
|
||||
}
|
||||
|
||||
// 根据角色ID查询菜单下拉树结构
|
||||
export function roleMenuTreeselect(roleId) {
|
||||
return request({
|
||||
url: '/system/menu/roleMenuTreeselect/' + roleId,
|
||||
method: 'get'
|
||||
})
|
||||
}
|
||||
|
||||
// 新增菜单
|
||||
export function addMenu(data) {
|
||||
return request({
|
||||
url: '/system/menu/create',
|
||||
method: 'post',
|
||||
data: data
|
||||
})
|
||||
}
|
||||
|
||||
// 修改菜单
|
||||
export function updateMenu(data) {
|
||||
return request({
|
||||
url: '/system/menu/update',
|
||||
method: 'post',
|
||||
data: data
|
||||
})
|
||||
}
|
||||
|
||||
// 删除菜单
|
||||
export function delMenu(id) {
|
||||
return request({
|
||||
url: '/system/menu/delete?id=' + id,
|
||||
method: 'post'
|
||||
})
|
||||
}
|
||||
import request from '@/utils/request'
|
||||
|
||||
// 查询菜单列表
|
||||
export function listMenu(query) {
|
||||
return request({
|
||||
url: '/system/menu/list',
|
||||
method: 'get',
|
||||
params: query
|
||||
})
|
||||
}
|
||||
|
||||
// 查询菜单(精简)列表
|
||||
export function listSimpleMenus() {
|
||||
return request({
|
||||
url: '/system/menu/list-all-simple',
|
||||
method: 'get'
|
||||
})
|
||||
}
|
||||
|
||||
// 查询菜单详细
|
||||
export function getMenu(id) {
|
||||
return request({
|
||||
url: '/system/menu/get?id=' + id,
|
||||
method: 'get'
|
||||
})
|
||||
}
|
||||
|
||||
// 新增菜单
|
||||
export function addMenu(data) {
|
||||
return request({
|
||||
url: '/system/menu/create',
|
||||
method: 'post',
|
||||
data: data
|
||||
})
|
||||
}
|
||||
|
||||
// 修改菜单
|
||||
export function updateMenu(data) {
|
||||
return request({
|
||||
url: '/system/menu/update',
|
||||
method: 'post',
|
||||
data: data
|
||||
})
|
||||
}
|
||||
|
||||
// 删除菜单
|
||||
export function delMenu(id) {
|
||||
return request({
|
||||
url: '/system/menu/delete?id=' + id,
|
||||
method: 'post'
|
||||
})
|
||||
}
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
import request from '@/utils/request'
|
||||
|
||||
// 查询角色拥有的菜单数组
|
||||
export function listRoleMenus(roleId) {
|
||||
return request({
|
||||
url: '/system/permission/list-role-resources?roleId=' + roleId,
|
||||
method: 'get'
|
||||
})
|
||||
}
|
||||
|
||||
// 赋予角色菜单
|
||||
export function assignRoleMenu(data) {
|
||||
return request({
|
||||
url: '/system/permission/assign-role-menu',
|
||||
method: 'post',
|
||||
data: data
|
||||
})
|
||||
}
|
|
@ -148,5 +148,5 @@ export function handleTree(data, id, parentId, children, rootId) {
|
|||
//返回第一层
|
||||
return father[parentId] === rootId;
|
||||
});
|
||||
return treeData != '' ? treeData : data;
|
||||
return treeData !== '' ? treeData : data;
|
||||
}
|
||||
|
|
|
@ -182,9 +182,9 @@
|
|||
</el-select>
|
||||
</el-form-item>
|
||||
<el-form-item label="数据权限" v-show="form.dataScope == 2">
|
||||
<el-checkbox v-model="form.deptCheckStrictly" @change="handleCheckedTreeConnect($event, 'dept')">父子联动</el-checkbox>
|
||||
<el-checkbox v-model="deptExpand" @change="handleCheckedTreeExpand($event, 'dept')">展开/折叠</el-checkbox>
|
||||
<el-checkbox v-model="deptNodeAll" @change="handleCheckedTreeNodeAll($event, 'dept')">全选/全不选</el-checkbox>
|
||||
<el-checkbox v-model="form.deptCheckStrictly" @change="handleCheckedTreeConnect($event, 'dept')">父子联动</el-checkbox>
|
||||
<el-tree
|
||||
class="tree-border"
|
||||
:data="deptOptions"
|
||||
|
@ -192,7 +192,7 @@
|
|||
default-expand-all
|
||||
ref="dept"
|
||||
node-key="id"
|
||||
:check-strictly="!form.deptCheckStrictly"
|
||||
:check-strictly="form.deptCheckStrictly"
|
||||
empty-text="加载中,请稍后"
|
||||
:props="defaultProps"
|
||||
></el-tree>
|
||||
|
@ -214,16 +214,16 @@
|
|||
<el-input v-model="form.code" :disabled="true" />
|
||||
</el-form-item>
|
||||
<el-form-item label="菜单权限">
|
||||
<el-checkbox v-model="!form.menuCheckStrictly" @change="handleCheckedTreeConnect($event, 'menu')">父子联动(选中父节点,自动选择子节点)</el-checkbox>
|
||||
<el-checkbox v-model="menuExpand" @change="handleCheckedTreeExpand($event, 'menu')">展开/折叠</el-checkbox>
|
||||
<el-checkbox v-model="menuNodeAll" @change="handleCheckedTreeNodeAll($event, 'menu')">全选/全不选</el-checkbox>
|
||||
<el-checkbox v-model="form.menuCheckStrictly" @change="handleCheckedTreeConnect($event, 'menu')">父子联动</el-checkbox>
|
||||
<el-tree
|
||||
class="tree-border"
|
||||
:data="menuOptions"
|
||||
show-checkbox
|
||||
ref="menu"
|
||||
node-key="id"
|
||||
:check-strictly="!form.menuCheckStrictly"
|
||||
:check-strictly="form.menuCheckStrictly"
|
||||
empty-text="加载中,请稍后"
|
||||
:props="defaultProps"
|
||||
></el-tree>
|
||||
|
@ -239,7 +239,8 @@
|
|||
|
||||
<script>
|
||||
import { listRole, getRole, delRole, addRole, updateRole, exportRole, dataScope, changeRoleStatus } from "@/api/system/role";
|
||||
import { treeselect as menuTreeselect, roleMenuTreeselect } from "@/api/system/menu";
|
||||
import { listSimpleMenus } from "@/api/system/menu";
|
||||
import { listRoleMenus, assignRoleMenu } from "@/api/system/permission";
|
||||
import { treeselect as deptTreeselect, roleDeptTreeselect } from "@/api/system/dept";
|
||||
import {SysCommonStatusEnum, SysMenuTypeEnum} from "@/utils/constants";
|
||||
import {DICT_TYPE, getDictDataLabel, getDictDatas} from "@/utils/dict";
|
||||
|
@ -311,8 +312,8 @@ export default {
|
|||
// 表单参数
|
||||
form: {},
|
||||
defaultProps: {
|
||||
children: "children",
|
||||
label: "label"
|
||||
label: "name",
|
||||
children: "children"
|
||||
},
|
||||
// 表单校验
|
||||
rules: {
|
||||
|
@ -349,12 +350,6 @@ export default {
|
|||
}
|
||||
);
|
||||
},
|
||||
/** 查询菜单树结构 */
|
||||
getMenuTreeselect() {
|
||||
menuTreeselect().then(response => {
|
||||
this.menuOptions = response.data;
|
||||
});
|
||||
},
|
||||
/** 查询部门树结构 */
|
||||
getDeptTreeselect() {
|
||||
deptTreeselect().then(response => {
|
||||
|
@ -379,13 +374,6 @@ export default {
|
|||
checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
|
||||
return checkedKeys;
|
||||
},
|
||||
/** 根据角色ID查询菜单树结构 */
|
||||
getRoleMenuTreeselect(id) {
|
||||
return roleMenuTreeselect(id).then(response => {
|
||||
this.menuOptions = response.menus;
|
||||
return response;
|
||||
});
|
||||
},
|
||||
/** 根据角色ID查询部门树结构 */
|
||||
getRoleDeptTreeselect(id) {
|
||||
return roleDeptTreeselect(id).then(response => {
|
||||
|
@ -432,23 +420,22 @@ export default {
|
|||
},
|
||||
// 表单重置
|
||||
reset() {
|
||||
if (this.$refs.menu != undefined) {
|
||||
if (this.$refs.menu !== undefined) {
|
||||
this.$refs.menu.setCheckedKeys([]);
|
||||
}
|
||||
this.menuExpand = false,
|
||||
this.menuNodeAll = false,
|
||||
this.deptExpand = true,
|
||||
this.deptNodeAll = false,
|
||||
this.menuExpand = false;
|
||||
this.menuNodeAll = false;
|
||||
this.deptExpand = true;
|
||||
this.deptNodeAll = false;
|
||||
this.form = {
|
||||
id: undefined,
|
||||
name: undefined,
|
||||
code: undefined,
|
||||
sort: 0,
|
||||
status: "0",
|
||||
ids: [],
|
||||
deptIds: [],
|
||||
menuCheckStrictly: true,
|
||||
deptCheckStrictly: true,
|
||||
menuIds: [],
|
||||
menuCheckStrictly: false, // 非严格,菜单弹窗的父子级联动
|
||||
deptCheckStrictly: false,
|
||||
remark: undefined
|
||||
};
|
||||
this.resetForm("form");
|
||||
|
@ -466,12 +453,12 @@ export default {
|
|||
},
|
||||
// 树权限(展开/折叠)
|
||||
handleCheckedTreeExpand(value, type) {
|
||||
if (type == 'menu') {
|
||||
if (type === 'menu') {
|
||||
let treeList = this.menuOptions;
|
||||
for (let i = 0; i < treeList.length; i++) {
|
||||
this.$refs.menu.store.nodesMap[treeList[i].id].expanded = value;
|
||||
}
|
||||
} else if (type == 'dept') {
|
||||
} else if (type === 'dept') {
|
||||
let treeList = this.deptOptions;
|
||||
for (let i = 0; i < treeList.length; i++) {
|
||||
this.$refs.dept.store.nodesMap[treeList[i].id].expanded = value;
|
||||
|
@ -480,18 +467,18 @@ export default {
|
|||
},
|
||||
// 树权限(全选/全不选)
|
||||
handleCheckedTreeNodeAll(value, type) {
|
||||
if (type == 'menu') {
|
||||
if (type === 'menu') {
|
||||
this.$refs.menu.setCheckedNodes(value ? this.menuOptions: []);
|
||||
} else if (type == 'dept') {
|
||||
} else if (type === 'dept') {
|
||||
this.$refs.dept.setCheckedNodes(value ? this.deptOptions: []);
|
||||
}
|
||||
},
|
||||
// 树权限(父子联动)
|
||||
handleCheckedTreeConnect(value, type) {
|
||||
if (type == 'menu') {
|
||||
this.form.menuCheckStrictly = value ? true: false;
|
||||
} else if (type == 'dept') {
|
||||
this.form.deptCheckStrictly = value ? true: false;
|
||||
if (type === 'menu') {
|
||||
this.form.menuCheckStrictly = value;
|
||||
} else if (type === 'dept') {
|
||||
this.form.deptCheckStrictly = value;
|
||||
}
|
||||
},
|
||||
/** 新增按钮操作 */
|
||||
|
@ -514,27 +501,27 @@ export default {
|
|||
handleMenu(row) {
|
||||
this.reset();
|
||||
const id = row.id
|
||||
// TODO 优化
|
||||
this.getMenuTreeselect();
|
||||
const roleMenu = this.getRoleMenuTreeselect(id);
|
||||
this.$nextTick(() => {
|
||||
roleMenu.then(res => {
|
||||
this.$refs.menu.setCheckedKeys(res.checkedKeys);
|
||||
});
|
||||
});
|
||||
// 处理了 form 的角色 name 和 code 的展示
|
||||
this.form.id = id;
|
||||
this.form.name = row.name;
|
||||
this.form.code = row.code;
|
||||
// 打开弹窗
|
||||
this.openMenu = true;
|
||||
|
||||
// const roleDeptTreeselect = this.getRoleDeptTreeselect(row.id);
|
||||
// getRole(row.id).then(response => {
|
||||
// this.form = response.data;
|
||||
// this.openDataScope = true;
|
||||
// this.$nextTick(() => {
|
||||
// roleDeptTreeselect.then(res => {
|
||||
// this.$refs.dept.setCheckedKeys(res.checkedKeys);
|
||||
// });
|
||||
// });
|
||||
// this.title = "分配数据权限";
|
||||
// });
|
||||
// 获得菜单列表
|
||||
listSimpleMenus().then(response => {
|
||||
// 处理 menuOptions 参数
|
||||
this.menuOptions = [];
|
||||
this.menuOptions.push(...this.handleTree(response.data, "id"));
|
||||
});
|
||||
// 获得角色拥有的菜单集合
|
||||
listRoleMenus(id).then(response => {
|
||||
// 设置为严格,避免设置父节点自动选中子节点,解决半选中问题
|
||||
// this.form.menuCheckStrictly = true
|
||||
// 设置选中
|
||||
this.$refs.menu.setCheckedKeys(response.data, false);
|
||||
// 设置为非严格,继续使用半选中
|
||||
// this.form.menuCheckStrictly = false
|
||||
})
|
||||
},
|
||||
/** 分配数据权限操作 */
|
||||
handleDataScope(row) {
|
||||
|
@ -556,16 +543,12 @@ export default {
|
|||
this.$refs["form"].validate(valid => {
|
||||
if (valid) {
|
||||
if (this.form.id !== undefined) {
|
||||
// TODO 芋艿:需要迁移
|
||||
// this.form.ids = this.getMenuAllCheckedKeys();
|
||||
updateRole(this.form).then(response => {
|
||||
this.msgSuccess("修改成功");
|
||||
this.open = false;
|
||||
this.getList();
|
||||
});
|
||||
} else {
|
||||
// TODO 芋艿:需要迁移
|
||||
// this.form.ids = this.getMenuAllCheckedKeys();
|
||||
addRole(this.form).then(response => {
|
||||
this.msgSuccess("新增成功");
|
||||
this.open = false;
|
||||
|
@ -577,7 +560,7 @@ export default {
|
|||
},
|
||||
/** 提交按钮(数据权限) */
|
||||
submitDataScope: function() {
|
||||
if (this.form.id != undefined) {
|
||||
if (this.form.id !== undefined) {
|
||||
this.form.deptIds = this.getDeptAllCheckedKeys();
|
||||
dataScope(this.form).then(response => {
|
||||
this.msgSuccess("修改成功");
|
||||
|
@ -589,11 +572,13 @@ export default {
|
|||
/** 提交按钮(菜单权限) */
|
||||
submitMenu: function() {
|
||||
// TODO 需要改动下
|
||||
if (this.form.id != undefined) {
|
||||
this.form.deptIds = this.getDeptAllCheckedKeys();
|
||||
dataScope(this.form).then(response => {
|
||||
if (this.form.id !== undefined) {
|
||||
assignRoleMenu({
|
||||
roleId: this.form.id,
|
||||
menuIds: this.getMenuAllCheckedKeys()
|
||||
}).then(response => {
|
||||
this.msgSuccess("修改成功");
|
||||
this.openDataScope = false;
|
||||
this.openMenu = false;
|
||||
this.getList();
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,10 +1,8 @@
|
|||
package cn.iocoder.dashboard.modules.system.controller.permission;
|
||||
|
||||
import cn.iocoder.dashboard.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.dashboard.common.pojo.CommonResult;
|
||||
import cn.iocoder.dashboard.modules.system.controller.permission.vo.menu.SysMenuCreateReqVO;
|
||||
import cn.iocoder.dashboard.modules.system.controller.permission.vo.menu.SysMenuListReqVO;
|
||||
import cn.iocoder.dashboard.modules.system.controller.permission.vo.menu.SysMenuRespVO;
|
||||
import cn.iocoder.dashboard.modules.system.controller.permission.vo.menu.SysMenuUpdateReqVO;
|
||||
import cn.iocoder.dashboard.modules.system.controller.permission.vo.menu.*;
|
||||
import cn.iocoder.dashboard.modules.system.convert.permission.SysMenuConvert;
|
||||
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysMenuDO;
|
||||
import cn.iocoder.dashboard.modules.system.service.permission.SysMenuService;
|
||||
|
@ -15,6 +13,7 @@ import org.springframework.validation.annotation.Validated;
|
|||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import static cn.iocoder.dashboard.common.pojo.CommonResult.success;
|
||||
|
@ -30,8 +29,22 @@ public class SysMenuController {
|
|||
@ApiOperation("获取菜单列表")
|
||||
// @PreAuthorize("@ss.hasPermi('system:menu:list')")
|
||||
@GetMapping("/list")
|
||||
public CommonResult<List<SysMenuRespVO>> list(SysMenuListReqVO reqVO) {
|
||||
return success(menuService.listMenus(reqVO));
|
||||
public CommonResult<List<SysMenuRespVO>> listMenus(SysMenuListReqVO reqVO) {
|
||||
List<SysMenuDO> list = menuService.listMenus(reqVO);
|
||||
list.sort(Comparator.comparing(SysMenuDO::getSort));
|
||||
return success(SysMenuConvert.INSTANCE.convertList(list));
|
||||
}
|
||||
|
||||
@ApiOperation(value = "获取菜单精简信息列表", notes = "只包含被开启的菜单,主要用于前端的下拉选项")
|
||||
@GetMapping("/list-all-simple")
|
||||
public CommonResult<List<SysMenuSimpleRespVO>> listSimpleMenus() {
|
||||
// 获得菜单列表,只要开启状态的
|
||||
SysMenuListReqVO reqVO = new SysMenuListReqVO();
|
||||
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
List<SysMenuDO> list = menuService.listMenus(reqVO);
|
||||
// 排序后,返回个诶前端
|
||||
list.sort(Comparator.comparing(SysMenuDO::getSort));
|
||||
return success(SysMenuConvert.INSTANCE.convertList02(list));
|
||||
}
|
||||
|
||||
@ApiOperation("获取菜单信息")
|
||||
|
@ -42,17 +55,6 @@ public class SysMenuController {
|
|||
return success(SysMenuConvert.INSTANCE.convert(menu));
|
||||
}
|
||||
|
||||
// /**
|
||||
// * 获取菜单下拉树列表
|
||||
// */
|
||||
// @GetMapping("/treeselect")
|
||||
// public AjaxResult treeselect(SysMenu menu) {
|
||||
// LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
|
||||
// Long userId = loginUser.getUser().getUserId();
|
||||
// List<SysMenu> menus = menuService.selectMenuList(menu, userId);
|
||||
// return AjaxResult.success(menuService.buildMenuTreeSelect(menus));
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * 加载对应角色菜单列表树
|
||||
// */
|
||||
|
|
|
@ -0,0 +1,62 @@
|
|||
package cn.iocoder.dashboard.modules.system.controller.permission;
|
||||
|
||||
import cn.iocoder.dashboard.common.pojo.CommonResult;
|
||||
import cn.iocoder.dashboard.modules.system.controller.permission.vo.permission.SysPermissionAssignRoleMenuReqVO;
|
||||
import cn.iocoder.dashboard.modules.system.service.permission.SysPermissionService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiImplicitParam;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Set;
|
||||
|
||||
import static cn.iocoder.dashboard.common.pojo.CommonResult.success;
|
||||
|
||||
/**
|
||||
* 权限 Controller,提供赋予用户、角色的权限的 API 接口
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Api(tags = "权限 API")
|
||||
@RestController
|
||||
@RequestMapping("/system/permission")
|
||||
public class SysPermissionController {
|
||||
|
||||
@Resource
|
||||
private SysPermissionService permissionService;
|
||||
|
||||
@ApiOperation("获得角色拥有的菜单编号")
|
||||
@GetMapping("/list-role-resources")
|
||||
@ApiImplicitParam(name = "roleId", value = "角色编号", required = true)
|
||||
// @RequiresPermissions("system:permission:assign-role-menu")
|
||||
public CommonResult<Set<Long>> listRoleMenus(Long roleId) {
|
||||
return success(permissionService.listRoleMenuIds(roleId));
|
||||
}
|
||||
|
||||
@PostMapping("/assign-role-menu")
|
||||
@ApiOperation("赋予角色菜单")
|
||||
// @RequiresPermissions("system:permission:assign-role-resource")
|
||||
public CommonResult<Boolean> assignRoleMenu(@Validated @RequestBody SysPermissionAssignRoleMenuReqVO reqVO) {
|
||||
permissionService.assignRoleMenu(reqVO.getRoleId(), reqVO.getMenuIds());
|
||||
return success(true);
|
||||
}
|
||||
|
||||
// @GetMapping("/list-admin-roles")
|
||||
// @ApiOperation("获得管理员拥有的角色编号列表")
|
||||
//// @RequiresPermissions("system:permission:assign-admin-role")
|
||||
// @ApiImplicitParam(name = "adminId", value = "管理员编号", required = true)
|
||||
// public CommonResult<Set<Integer>> listAdminRoles(Integer adminId) {
|
||||
// return success(permissionManager.listAdminRoles(adminId));
|
||||
// }
|
||||
|
||||
// @PostMapping("/assign-admin-role")
|
||||
// @ApiOperation("赋予用户角色")
|
||||
//// @RequiresPermissions("system:permission:assign-admin-role")
|
||||
// public CommonResult<Boolean> assignAdminRole(PermissionAssignAdminRoleDTO assignAdminRoleDTO) {
|
||||
// permissionManager.assignAdminRole(assignAdminRoleDTO);
|
||||
// return success(true);
|
||||
// }
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package cn.iocoder.dashboard.modules.system.controller.permission.vo.menu;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@ApiModel("菜单精简信息 Response VO")
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class SysMenuSimpleRespVO {
|
||||
|
||||
@ApiModelProperty(value = "菜单编号", required = true, example = "1024")
|
||||
private Integer id;
|
||||
|
||||
@ApiModelProperty(value = "菜单名称", required = true, example = "芋道")
|
||||
private String name;
|
||||
|
||||
@ApiModelProperty(value = "父菜单 ID", required = true, example = "1024")
|
||||
private Long parentId;
|
||||
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package cn.iocoder.dashboard.modules.system.controller.permission.vo.permission;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
@ApiModel("赋予角色菜单 Request VO")
|
||||
@Data
|
||||
public class SysPermissionAssignRoleMenuReqVO {
|
||||
|
||||
@ApiModelProperty(value = "角色名", required = true, example = "1")
|
||||
@NotNull(message = "角色编号不能为空")
|
||||
private Long roleId;
|
||||
|
||||
@ApiModelProperty(value = "菜单编号列表", example = "1,3,5")
|
||||
private Set<Long> menuIds = Collections.emptySet(); // 兜底
|
||||
|
||||
}
|
|
@ -2,6 +2,7 @@ package cn.iocoder.dashboard.modules.system.convert.permission;
|
|||
|
||||
import cn.iocoder.dashboard.modules.system.controller.permission.vo.menu.SysMenuCreateReqVO;
|
||||
import cn.iocoder.dashboard.modules.system.controller.permission.vo.menu.SysMenuRespVO;
|
||||
import cn.iocoder.dashboard.modules.system.controller.permission.vo.menu.SysMenuSimpleRespVO;
|
||||
import cn.iocoder.dashboard.modules.system.controller.permission.vo.menu.SysMenuUpdateReqVO;
|
||||
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysMenuDO;
|
||||
import org.mapstruct.Mapper;
|
||||
|
@ -22,4 +23,6 @@ public interface SysMenuConvert {
|
|||
|
||||
SysMenuRespVO convert(SysMenuDO bean);
|
||||
|
||||
List<SysMenuSimpleRespVO> convertList02(List<SysMenuDO> list);
|
||||
|
||||
}
|
||||
|
|
|
@ -1,9 +1,35 @@
|
|||
package cn.iocoder.dashboard.modules.system.dal.mysql.dao.permission;
|
||||
|
||||
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysRoleMenuDO;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Mapper
|
||||
public interface SysRoleMenuMapper extends BaseMapper<SysRoleMenuDO> {
|
||||
|
||||
default List<SysRoleMenuDO> selectListByRoleId(Long roleId) {
|
||||
return selectList(new QueryWrapper<SysRoleMenuDO>().eq("role_id", roleId));
|
||||
}
|
||||
|
||||
default void insertList(Long roleId, Collection<Long> menuIds) {
|
||||
List<SysRoleMenuDO> list = menuIds.stream().map(menuId -> {
|
||||
SysRoleMenuDO entity = new SysRoleMenuDO();
|
||||
entity.setRoleId(roleId);
|
||||
entity.setMenuId(menuId);
|
||||
return entity;
|
||||
}).collect(Collectors.toList());
|
||||
// TODO 芋艿,mybatis plus 增加批量插入的功能
|
||||
list.forEach(this::insert);
|
||||
}
|
||||
|
||||
default void deleteListByRoleIdAndMenuIds(Long roleId, Collection<Long> menuIds) {
|
||||
delete(new QueryWrapper<SysRoleMenuDO>().eq("role_id", roleId)
|
||||
.in("menu_id", menuIds));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package cn.iocoder.dashboard.modules.system.service.permission;
|
|||
|
||||
import cn.iocoder.dashboard.modules.system.controller.permission.vo.menu.SysMenuCreateReqVO;
|
||||
import cn.iocoder.dashboard.modules.system.controller.permission.vo.menu.SysMenuListReqVO;
|
||||
import cn.iocoder.dashboard.modules.system.controller.permission.vo.menu.SysMenuRespVO;
|
||||
import cn.iocoder.dashboard.modules.system.controller.permission.vo.menu.SysMenuUpdateReqVO;
|
||||
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysMenuDO;
|
||||
|
||||
|
@ -21,13 +20,20 @@ public interface SysMenuService {
|
|||
*/
|
||||
void init();
|
||||
|
||||
/**
|
||||
* 获得所有菜单列表
|
||||
*
|
||||
* @return 菜单列表
|
||||
*/
|
||||
List<SysMenuDO> listMenus();
|
||||
|
||||
/**
|
||||
* 筛选菜单列表
|
||||
*
|
||||
* @param reqVO 筛选条件请求 VO
|
||||
* @return 菜单列表
|
||||
*/
|
||||
List<SysMenuRespVO> listMenus(SysMenuListReqVO reqVO);
|
||||
List<SysMenuDO> listMenus(SysMenuListReqVO reqVO);
|
||||
|
||||
/**
|
||||
* 获得所有菜单,从缓存中
|
||||
|
|
|
@ -43,6 +43,22 @@ public interface SysPermissionService {
|
|||
*/
|
||||
Set<Long> listUserRoleIds(Long userId, @Nullable Collection<Integer> roleStatuses);
|
||||
|
||||
/**
|
||||
* 获得角色拥有的菜单编号集合
|
||||
*
|
||||
* @param roleId 角色编号
|
||||
* @return 菜单编号集合
|
||||
*/
|
||||
Set<Long> listRoleMenuIds(Long roleId);
|
||||
|
||||
/**
|
||||
* 赋予赋予角色菜单
|
||||
*
|
||||
* @param roleId 角色编号
|
||||
* @param menuIds 菜单编号集合
|
||||
*/
|
||||
void assignRoleMenu(Long roleId, Set<Long> menuIds);
|
||||
|
||||
/**
|
||||
* 处理角色删除时,删除关联授权角色
|
||||
*
|
||||
|
|
|
@ -3,7 +3,6 @@ package cn.iocoder.dashboard.modules.system.service.permission.impl;
|
|||
import cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil;
|
||||
import cn.iocoder.dashboard.modules.system.controller.permission.vo.menu.SysMenuCreateReqVO;
|
||||
import cn.iocoder.dashboard.modules.system.controller.permission.vo.menu.SysMenuListReqVO;
|
||||
import cn.iocoder.dashboard.modules.system.controller.permission.vo.menu.SysMenuRespVO;
|
||||
import cn.iocoder.dashboard.modules.system.controller.permission.vo.menu.SysMenuUpdateReqVO;
|
||||
import cn.iocoder.dashboard.modules.system.convert.permission.SysMenuConvert;
|
||||
import cn.iocoder.dashboard.modules.system.dal.mysql.dao.permission.SysMenuMapper;
|
||||
|
@ -78,10 +77,13 @@ public class SysMenuServiceImpl implements SysMenuService {
|
|||
}
|
||||
|
||||
@Override
|
||||
public List<SysMenuRespVO> listMenus(SysMenuListReqVO reqVO) {
|
||||
List<SysMenuDO> list = menuMapper.selectList(reqVO);
|
||||
// TODO 排序
|
||||
return SysMenuConvert.INSTANCE.convertList(list);
|
||||
public List<SysMenuDO> listMenus() {
|
||||
return menuMapper.selectList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SysMenuDO> listMenus(SysMenuListReqVO reqVO) {
|
||||
return menuMapper.selectList(reqVO);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package cn.iocoder.dashboard.modules.system.service.permission.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.iocoder.dashboard.modules.system.dal.mysql.dao.permission.SysRoleMenuMapper;
|
||||
import cn.iocoder.dashboard.modules.system.dal.mysql.dao.permission.SysUserRoleMapper;
|
||||
|
@ -16,6 +17,7 @@ import com.google.common.collect.ImmutableMultimap;
|
|||
import com.google.common.collect.Multimap;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
|
@ -110,6 +112,36 @@ public class SysPermissionServiceImpl implements SysPermissionService {
|
|||
return CollectionUtils.convertSet(userRoleList, SysUserRoleDO::getRoleId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Long> listRoleMenuIds(Long roleId) {
|
||||
// 如果是管理员的情况下,获取全部菜单编号
|
||||
SysRoleDO role = roleService.getRole(roleId);
|
||||
if (roleService.hasAnyAdmin(Collections.singletonList(role))) {
|
||||
return CollectionUtils.convertSet(menuService.listMenus(), SysMenuDO::getId);
|
||||
}
|
||||
// 如果是非管理员的情况下,获得拥有的菜单编号
|
||||
return CollectionUtils.convertSet(roleMenuMapper.selectListByRoleId(roleId),
|
||||
SysRoleMenuDO::getMenuId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void assignRoleMenu(Long roleId, Set<Long> menuIds) {
|
||||
// 获得角色拥有菜单编号
|
||||
Set<Long> dbMenuIds = CollectionUtils.convertSet(roleMenuMapper.selectListByRoleId(roleId),
|
||||
SysRoleMenuDO::getMenuId);
|
||||
// 计算新增和删除的菜单编号
|
||||
Collection<Long> createMenuIds = CollUtil.subtract(menuIds, dbMenuIds);
|
||||
Collection<Long> deleteMenuIds = CollUtil.subtract(dbMenuIds, menuIds);
|
||||
// 执行新增和删除。对于已经授权的菜单,不用做任何处理
|
||||
if (!CollectionUtil.isEmpty(createMenuIds)) {
|
||||
roleMenuMapper.insertList(roleId, createMenuIds);
|
||||
}
|
||||
if (!CollectionUtil.isEmpty(deleteMenuIds)) {
|
||||
roleMenuMapper.deleteListByRoleIdAndMenuIds(roleId, deleteMenuIds);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processRoleDeleted(Long roleId) {
|
||||
// TODO 实现我
|
||||
|
|
|
@ -18,6 +18,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
|
|||
import com.google.common.collect.ImmutableMap;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
@ -115,6 +116,7 @@ public class SysRoleServiceImpl implements SysRoleService {
|
|||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void deleteRole(Long id) {
|
||||
// 校验是否可以更新
|
||||
this.checkUpdateRole(id);
|
||||
|
@ -129,7 +131,6 @@ public class SysRoleServiceImpl implements SysRoleService {
|
|||
return roleMapper.selectById(id);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public PageResult<SysRoleDO> pageRole(SysRolePageReqVO reqVO) {
|
||||
IPage<SysRoleDO> roleDOPage = roleMapper.selectPage(reqVO);
|
||||
|
|
Loading…
Reference in New Issue