diff --git a/ruoyi-generator/src/main/java/com/ruoyi/generator/util/VelocityUtils.java b/ruoyi-generator/src/main/java/com/ruoyi/generator/util/VelocityUtils.java index 0be5fbb6e..a87866d8f 100644 --- a/ruoyi-generator/src/main/java/com/ruoyi/generator/util/VelocityUtils.java +++ b/ruoyi-generator/src/main/java/com/ruoyi/generator/util/VelocityUtils.java @@ -3,6 +3,7 @@ package com.ruoyi.generator.util; import java.util.ArrayList; import java.util.HashSet; import java.util.List; + import org.apache.velocity.VelocityContext; import com.alibaba.fastjson.JSONObject; import com.ruoyi.common.constant.GenConstants; @@ -13,18 +14,23 @@ import com.ruoyi.generator.domain.GenTableColumn; /** * 模板处理工具类 - * + * * @author ruoyi */ -public class VelocityUtils -{ - /** 项目空间路径 */ +public class VelocityUtils { + /** + * 项目空间路径 + */ private static final String PROJECT_PATH = "main/java"; - /** mybatis空间路径 */ + /** + * mybatis空间路径 + */ private static final String MYBATIS_PATH = "main/resources/mapper"; - /** 默认上级菜单,系统工具 */ + /** + * 默认上级菜单,系统工具 + */ private static final String DEFAULT_PARENT_MENU_ID = "3"; /** @@ -32,8 +38,7 @@ public class VelocityUtils * * @return 模板列表 */ - public static VelocityContext prepareContext(GenTable genTable) - { + public static VelocityContext prepareContext(GenTable genTable) { String moduleName = genTable.getModuleName(); String businessName = genTable.getBusinessName(); String packageName = genTable.getPackageName(); @@ -59,23 +64,20 @@ public class VelocityUtils velocityContext.put("columns", genTable.getColumns()); velocityContext.put("table", genTable); setMenuVelocityContext(velocityContext, genTable); - if (GenConstants.TPL_TREE.equals(tplCategory)) - { + if (GenConstants.TPL_TREE.equals(tplCategory)) { setTreeVelocityContext(velocityContext, genTable); } return velocityContext; } - public static void setMenuVelocityContext(VelocityContext context, GenTable genTable) - { + public static void setMenuVelocityContext(VelocityContext context, GenTable genTable) { String options = genTable.getOptions(); JSONObject paramsObj = JSONObject.parseObject(options); String parentMenuId = getParentMenuId(paramsObj); context.put("parentMenuId", parentMenuId); } - public static void setTreeVelocityContext(VelocityContext context, GenTable genTable) - { + public static void setTreeVelocityContext(VelocityContext context, GenTable genTable) { String options = genTable.getOptions(); JSONObject paramsObj = JSONObject.parseObject(options); String treeCode = getTreecode(paramsObj); @@ -86,12 +88,10 @@ public class VelocityUtils context.put("treeParentCode", treeParentCode); context.put("treeName", treeName); context.put("expandColumn", getExpandColumn(genTable)); - if (paramsObj.containsKey(GenConstants.TREE_PARENT_CODE)) - { + if (paramsObj.containsKey(GenConstants.TREE_PARENT_CODE)) { context.put("tree_parent_code", paramsObj.getString(GenConstants.TREE_PARENT_CODE)); } - if (paramsObj.containsKey(GenConstants.TREE_NAME)) - { + if (paramsObj.containsKey(GenConstants.TREE_NAME)) { context.put("tree_name", paramsObj.getString(GenConstants.TREE_NAME)); } } @@ -101,8 +101,7 @@ public class VelocityUtils * * @return 模板列表 */ - public static List getTemplateList(String tplCategory) - { + public static List getTemplateList(String tplCategory) { List templates = new ArrayList(); templates.add("vm/java/domain.java.vm"); templates.add("vm/java/mapper.java.vm"); @@ -112,12 +111,9 @@ public class VelocityUtils templates.add("vm/xml/mapper.xml.vm"); templates.add("vm/sql/sql.vm"); templates.add("vm/js/api.js.vm"); - if (GenConstants.TPL_CRUD.equals(tplCategory)) - { + if (GenConstants.TPL_CRUD.equals(tplCategory)) { templates.add("vm/vue/index.vue.vm"); - } - else if (GenConstants.TPL_TREE.equals(tplCategory)) - { + } else if (GenConstants.TPL_TREE.equals(tplCategory)) { templates.add("vm/vue/index-tree.vue.vm"); } return templates; @@ -126,8 +122,7 @@ public class VelocityUtils /** * 获取文件名 */ - public static String getFileName(String template, GenTable genTable) - { + public static String getFileName(String template, GenTable genTable) { // 文件名称 String fileName = ""; // 包路径 @@ -143,44 +138,25 @@ public class VelocityUtils String mybatisPath = MYBATIS_PATH + "/" + moduleName; String vuePath = "vue"; - if (template.contains("domain.java.vm")) - { + if (template.contains("domain.java.vm")) { fileName = StringUtils.format("{}/domain/{}.java", javaPath, className); - } - else if (template.contains("mapper.java.vm")) - { + } else if (template.contains("mapper.java.vm")) { fileName = StringUtils.format("{}/mapper/{}Mapper.java", javaPath, className); - } - else if (template.contains("service.java.vm")) - { + } else if (template.contains("service.java.vm")) { fileName = StringUtils.format("{}/service/I{}Service.java", javaPath, className); - } - else if (template.contains("serviceImpl.java.vm")) - { + } else if (template.contains("serviceImpl.java.vm")) { fileName = StringUtils.format("{}/service/impl/{}ServiceImpl.java", javaPath, className); - } - else if (template.contains("controller.java.vm")) - { + } else if (template.contains("controller.java.vm")) { fileName = StringUtils.format("{}/controller/{}Controller.java", javaPath, className); - } - else if (template.contains("mapper.xml.vm")) - { + } else if (template.contains("mapper.xml.vm")) { fileName = StringUtils.format("{}/{}Mapper.xml", mybatisPath, className); - } - else if (template.contains("sql.vm")) - { + } else if (template.contains("sql.vm")) { fileName = businessName + "Menu.sql"; - } - else if (template.contains("api.js.vm")) - { + } else if (template.contains("api.js.vm")) { fileName = StringUtils.format("{}/api/{}/{}.js", vuePath, moduleName, businessName); - } - else if (template.contains("index.vue.vm")) - { + } else if (template.contains("index.vue.vm")) { fileName = StringUtils.format("{}/views/{}/{}/index.vue", vuePath, moduleName, businessName); - } - else if (template.contains("index-tree.vue.vm")) - { + } else if (template.contains("index-tree.vue.vm")) { fileName = StringUtils.format("{}/views/{}/{}/index.vue", vuePath, moduleName, businessName); } return fileName; @@ -192,8 +168,7 @@ public class VelocityUtils * @param packageName 包名称 * @return 包前缀名称 */ - public static String getPackagePrefix(String packageName) - { + public static String getPackagePrefix(String packageName) { int lastIndex = packageName.lastIndexOf("."); String basePackage = StringUtils.substring(packageName, 0, lastIndex); return basePackage; @@ -205,18 +180,13 @@ public class VelocityUtils * @param columns 列集合 * @return 返回需要导入的包列表 */ - public static HashSet getImportList(List columns) - { + public static HashSet getImportList(List columns) { HashSet importList = new HashSet(); - for (GenTableColumn column : columns) - { - if (!column.isSuperColumn() && GenConstants.TYPE_DATE.equals(column.getJavaType())) - { + for (GenTableColumn column : columns) { + if (!column.isSuperColumn() && GenConstants.TYPE_DATE.equals(column.getJavaType())) { importList.add("java.util.Date"); importList.add("com.fasterxml.jackson.annotation.JsonFormat"); - } - else if (!column.isSuperColumn() && GenConstants.TYPE_BIGDECIMAL.equals(column.getJavaType())) - { + } else if (!column.isSuperColumn() && GenConstants.TYPE_BIGDECIMAL.equals(column.getJavaType())) { importList.add("java.math.BigDecimal"); } } @@ -226,12 +196,11 @@ public class VelocityUtils /** * 获取权限前缀 * - * @param moduleName 模块名称 + * @param moduleName 模块名称 * @param businessName 业务名称 * @return 返回权限前缀 */ - public static String getPermissionPrefix(String moduleName, String businessName) - { + public static String getPermissionPrefix(String moduleName, String businessName) { return StringUtils.format("{}:{}", moduleName, businessName); } @@ -241,10 +210,8 @@ public class VelocityUtils * @param paramsObj 生成其他选项 * @return 上级菜单ID字段 */ - public static String getParentMenuId(JSONObject paramsObj) - { - if (StringUtils.isNotEmpty(paramsObj) && paramsObj.containsKey(GenConstants.PARENT_MENU_ID)) - { + public static String getParentMenuId(JSONObject paramsObj) { + if (StringUtils.isNotEmpty(paramsObj) && paramsObj.containsKey(GenConstants.PARENT_MENU_ID)) { return paramsObj.getString(GenConstants.PARENT_MENU_ID); } return DEFAULT_PARENT_MENU_ID; @@ -256,10 +223,8 @@ public class VelocityUtils * @param paramsObj 生成其他选项 * @return 树编码 */ - public static String getTreecode(JSONObject paramsObj) - { - if (paramsObj.containsKey(GenConstants.TREE_CODE)) - { + public static String getTreecode(JSONObject paramsObj) { + if (paramsObj.containsKey(GenConstants.TREE_CODE)) { return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_CODE)); } return StringUtils.EMPTY; @@ -271,10 +236,8 @@ public class VelocityUtils * @param paramsObj 生成其他选项 * @return 树父编码 */ - public static String getTreeParentCode(JSONObject paramsObj) - { - if (paramsObj.containsKey(GenConstants.TREE_PARENT_CODE)) - { + public static String getTreeParentCode(JSONObject paramsObj) { + if (paramsObj.containsKey(GenConstants.TREE_PARENT_CODE)) { return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_PARENT_CODE)); } return StringUtils.EMPTY; @@ -286,10 +249,8 @@ public class VelocityUtils * @param paramsObj 生成其他选项 * @return 树名称 */ - public static String getTreeName(JSONObject paramsObj) - { - if (paramsObj.containsKey(GenConstants.TREE_NAME)) - { + public static String getTreeName(JSONObject paramsObj) { + if (paramsObj.containsKey(GenConstants.TREE_NAME)) { return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_NAME)); } return StringUtils.EMPTY; @@ -301,20 +262,16 @@ public class VelocityUtils * @param genTable 业务表对象 * @return 展开按钮列序号 */ - public static int getExpandColumn(GenTable genTable) - { + public static int getExpandColumn(GenTable genTable) { String options = genTable.getOptions(); JSONObject paramsObj = JSONObject.parseObject(options); String treeName = paramsObj.getString(GenConstants.TREE_NAME); int num = 0; - for (GenTableColumn column : genTable.getColumns()) - { - if (column.isList()) - { + for (GenTableColumn column : genTable.getColumns()) { + if (column.isList()) { num++; String columnName = column.getColumnName(); - if (columnName.equals(treeName)) - { + if (columnName.equals(treeName)) { break; } } diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysRoleServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysRoleServiceImpl.java index cecc6a2cb..ad3bebffc 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysRoleServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysRoleServiceImpl.java @@ -24,7 +24,7 @@ import com.ruoyi.system.service.ISysRoleService; /** * 角色 业务层处理 - * + * * @author ruoyi */ @Service @@ -44,7 +44,7 @@ public class SysRoleServiceImpl implements ISysRoleService /** * 根据条件分页查询角色数据 - * + * * @param role 角色信息 * @return 角色数据集合信息 */ @@ -57,7 +57,7 @@ public class SysRoleServiceImpl implements ISysRoleService /** * 根据用户ID查询权限 - * + * * @param userId 用户ID * @return 权限列表 */ @@ -78,7 +78,7 @@ public class SysRoleServiceImpl implements ISysRoleService /** * 查询所有角色 - * + * * @return 角色列表 */ @Override @@ -89,7 +89,7 @@ public class SysRoleServiceImpl implements ISysRoleService /** * 根据用户ID获取角色选择框列表 - * + * * @param userId 用户ID * @return 选中角色ID列表 */ @@ -101,7 +101,7 @@ public class SysRoleServiceImpl implements ISysRoleService /** * 通过角色ID查询角色 - * + * * @param roleId 角色ID * @return 角色对象信息 */ @@ -113,7 +113,7 @@ public class SysRoleServiceImpl implements ISysRoleService /** * 校验角色名称是否唯一 - * + * * @param role 角色信息 * @return 结果 */ @@ -129,97 +129,9 @@ public class SysRoleServiceImpl implements ISysRoleService return UserConstants.UNIQUE; } - /** - * 校验角色权限是否唯一 - * - * @param role 角色信息 - * @return 结果 - */ - @Override - public String checkRoleKeyUnique(SysRole role) - { - Long roleId = StringUtils.isNull(role.getRoleId()) ? -1L : role.getRoleId(); - SysRole info = roleMapper.checkRoleKeyUnique(role.getRoleKey()); - if (StringUtils.isNotNull(info) && info.getRoleId().longValue() != roleId.longValue()) - { - return UserConstants.NOT_UNIQUE; - } - return UserConstants.UNIQUE; - } - - /** - * 校验角色是否允许操作 - * - * @param role 角色信息 - */ - @Override - public void checkRoleAllowed(SysRole role) - { - if (StringUtils.isNotNull(role.getRoleId()) && role.isAdmin()) - { - throw new CustomException("不允许操作超级管理员角色"); - } - } - - /** - * 通过角色ID查询角色使用数量 - * - * @param roleId 角色ID - * @return 结果 - */ - @Override - public int countUserRoleByRoleId(Long roleId) - { - return userRoleMapper.countUserRoleByRoleId(roleId); - } - - /** - * 新增保存角色信息 - * - * @param role 角色信息 - * @return 结果 - */ - @Override - @Transactional - public int insertRole(SysRole role) - { - // 新增角色信息 - roleMapper.insertRole(role); - return insertRoleMenu(role); - } - - /** - * 修改保存角色信息 - * - * @param role 角色信息 - * @return 结果 - */ - @Override - @Transactional - public int updateRole(SysRole role) - { - // 修改角色信息 - roleMapper.updateRole(role); - // 删除角色与菜单关联 - roleMenuMapper.deleteRoleMenuByRoleId(role.getRoleId()); - return insertRoleMenu(role); - } - - /** - * 修改角色状态 - * - * @param role 角色信息 - * @return 结果 - */ - @Override - public int updateRoleStatus(SysRole role) - { - return roleMapper.updateRole(role); - } - /** * 修改数据权限信息 - * + * * @param role 角色信息 * @return 结果 */ @@ -237,7 +149,7 @@ public class SysRoleServiceImpl implements ISysRoleService /** * 新增角色菜单信息 - * + * * @param role 角色对象 */ public int insertRoleMenu(SysRole role) @@ -285,7 +197,7 @@ public class SysRoleServiceImpl implements ISysRoleService /** * 通过角色ID删除角色 - * + * * @param roleId 角色ID * @return 结果 */ @@ -302,7 +214,7 @@ public class SysRoleServiceImpl implements ISysRoleService /** * 批量删除角色信息 - * + * * @param roleIds 需要删除的角色ID * @return 结果 */ diff --git a/src/main/java/cn/iocoder/dashboard/framework/mybatis/config/MybatisConfiguration.java b/src/main/java/cn/iocoder/dashboard/framework/mybatis/config/MybatisConfiguration.java index ed1a51313..b2038cd36 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/mybatis/config/MybatisConfiguration.java +++ b/src/main/java/cn/iocoder/dashboard/framework/mybatis/config/MybatisConfiguration.java @@ -1,7 +1,10 @@ package cn.iocoder.dashboard.framework.mybatis.config; +import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; +import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import org.apache.ibatis.annotations.Mapper; import org.mybatis.spring.annotation.MapperScan; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** @@ -12,4 +15,12 @@ import org.springframework.context.annotation.Configuration; @Configuration @MapperScan(value = "cn.iocoder.dashboard", annotationClass = Mapper.class) public class MybatisConfiguration { + + @Bean + public MybatisPlusInterceptor mybatisPlusInterceptor() { + MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor(); + mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor()); // 分页插件 + return mybatisPlusInterceptor; + } + } diff --git a/src/main/java/cn/iocoder/dashboard/framework/mybatis/core/util/MyBatisUtils.java b/src/main/java/cn/iocoder/dashboard/framework/mybatis/core/util/MyBatisUtils.java new file mode 100644 index 000000000..47d2f5666 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/mybatis/core/util/MyBatisUtils.java @@ -0,0 +1,33 @@ +package cn.iocoder.dashboard.framework.mybatis.core.util; + +import cn.hutool.core.collection.CollectionUtil; +import cn.iocoder.dashboard.common.pojo.PageParam; +import cn.iocoder.dashboard.common.pojo.SortingField; +import com.baomidou.mybatisplus.core.metadata.OrderItem; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; + +import java.util.Collection; +import java.util.stream.Collectors; + +/** + * MyBatis 工具类 + */ +public class MyBatisUtils { + + public static Page buildPage(PageParam pageParam) { + return buildPage(pageParam, null); + } + + public static Page buildPage(PageParam pageParam, Collection sortingFields) { + // 页码 + 数量 + Page page = new Page<>(pageParam.getPageNo(), pageParam.getPageSize()); + // 排序字段 + if (!CollectionUtil.isEmpty(sortingFields)) { + page.addOrder(sortingFields.stream().map(sortingField -> SortingField.ORDER_ASC.equals(sortingField.getOrder()) ? + OrderItem.asc(sortingField.getField()) : OrderItem.desc(sortingField.getField())) + .collect(Collectors.toList())); + } + return page; + } + +} diff --git a/src/main/java/cn/iocoder/dashboard/framework/security/core/enums/DataScopeEnum.java b/src/main/java/cn/iocoder/dashboard/framework/security/core/enums/DataScopeEnum.java index f97692f4a..7af847d8c 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/security/core/enums/DataScopeEnum.java +++ b/src/main/java/cn/iocoder/dashboard/framework/security/core/enums/DataScopeEnum.java @@ -15,10 +15,10 @@ import lombok.Getter; public enum DataScopeEnum { ALL(1), // 全部数据权限 - CUSTOM(2), // 自定数据权限 - DEPT(3), // 部门数据权限 + DEPT_CUSTOM(2), // 指定部门数据权限 + DEPT_ONLY(3), // 部门数据权限 DEPT_AND_CHILD(4), // 部门及以下数据权限 - SELF(5); // 仅本人数据权限 + DEPT_SELF(5); // 仅本人数据权限 /** * 范围 diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/SysMenuController.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/SysMenuController.java index 7a5a471c9..974577e72 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/SysMenuController.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/SysMenuController.java @@ -9,6 +9,7 @@ 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; 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.*; @@ -65,7 +66,7 @@ public class SysMenuController { // return ajax; // } - @ApiOperation("新增菜单") + @ApiOperation("创建菜单") // @PreAuthorize("@ss.hasPermi('system:menu:add')") // @Log(title = "菜单管理", businessType = BusinessType.INSERT) @PostMapping("/create") @@ -85,6 +86,7 @@ public class SysMenuController { @ApiOperation("删除菜单") @PostMapping("/delete") + @ApiImplicitParam(name = "id", value = "角色编号", required= true, example = "1024") // @Log(title = "菜单管理", businessType = BusinessType.DELETE) public CommonResult deleteMenu(@RequestParam("id") Long id) { menuService.deleteMenu(id); diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/SysRoleController.http b/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/SysRoleController.http new file mode 100644 index 000000000..4e150ac30 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/SysRoleController.http @@ -0,0 +1,41 @@ +### /role/create 成功 +POST {{baseUrl}}/system/role/create +Authorization: Bearer {{token}} +Content-Type: application/json + +{ + "name": "测试角色", + "code": "test", + "sort": 0 +} + +### /role/update 成功 +POST {{baseUrl}}/system/role/update +Authorization: Bearer {{token}} +Content-Type: application/json + + +{ + "id": 100, + "name": "测试角色", + "code": "test", + "sort": 10 +} +### /resource/delete 成功 +POST {{baseUrl}}/system/role/delete +Content-Type: application/x-www-form-urlencoded +Authorization: Bearer {{token}} + +roleId=14 + +### /role/get 成功 +GET {{baseUrl}}/system/role/get?id=100 +Content-Type: application/x-www-form-urlencoded +Authorization: Bearer {{token}} + +### /role/page 成功 +GET {{baseUrl}}/system/role/page?pageNo=1&pageSize=10 +Authorization: Bearer {{token}} + +### + diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/SysRoleController.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/SysRoleController.java index e96fec357..748e82ec3 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/SysRoleController.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/SysRoleController.java @@ -1,23 +1,75 @@ package cn.iocoder.dashboard.modules.system.controller.permission; +import cn.iocoder.dashboard.common.pojo.CommonResult; +import cn.iocoder.dashboard.common.pojo.PageResult; +import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRoleCreateReqVO; +import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRolePageReqVO; +import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRoleRespVO; +import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRoleUpdateReqVO; +import cn.iocoder.dashboard.modules.system.convert.permission.SysRoleConvert; +import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysRoleDO; +import cn.iocoder.dashboard.modules.system.service.permission.SysRoleService; import io.swagger.annotations.Api; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiImplicitParams; +import io.swagger.annotations.ApiOperation; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; + +import static cn.iocoder.dashboard.common.pojo.CommonResult.success; @Api(tags = "角色 API") @RestController @RequestMapping("/system/role") public class SysRoleController { + @Resource + private SysRoleService roleService; + + @ApiOperation("获得角色分页") + @GetMapping("/page") // @PreAuthorize("@ss.hasPermi('system:role:list')") -// @GetMapping("/list") -// public TableDataInfo list(SysRole role) -// { -// startPage(); -// List list = roleService.selectRoleList(role); -// return getDataTable(list); -// } -// + public CommonResult> list(SysRolePageReqVO reqVO) { + return success(roleService.pageRole(reqVO)); + } + + @ApiOperation("创建角色") + @PostMapping("/create") +// @PreAuthorize("@ss.hasPermi('system:role:add')") +// @Log(title = "角色管理", businessType = BusinessType.INSERT) + public CommonResult add(@Validated @RequestBody SysRoleCreateReqVO reqVO) { + return success(roleService.createRole(reqVO)); + } + + @ApiOperation("修改角色") +// @PreAuthorize("@ss.hasPermi('system:role:edit')") +// @Log(title = "角色管理", businessType = BusinessType.UPDATE) + @PostMapping("/update") + public CommonResult update(@Validated @RequestBody SysRoleUpdateReqVO reqVO) { + roleService.updateRole(reqVO); + return success(true); + } + + @ApiOperation("删除角色") + @PostMapping("/delete") + @ApiImplicitParam(name = "id", value = "角色编号", required = true, example = "1024") +// @PreAuthorize("@ss.hasPermi('system:role:remove')") +// @Log(title = "角色管理", businessType = BusinessType.DELETE) + public CommonResult remove(@RequestParam("id") Long id) { + roleService.deleteRole(id); + return success(true); + } + + @ApiOperation("获得角色信息") + @GetMapping("/get") +// @PreAuthorize("@ss.hasPermi('system:role:query')") + public CommonResult getRole(@RequestParam("id") Long id) { + SysRoleDO role = roleService.getRole(id); + return success(SysRoleConvert.INSTANCE.convert(role)); + } + // @Log(title = "角色管理", businessType = BusinessType.EXPORT) // @PreAuthorize("@ss.hasPermi('system:role:export')") // @GetMapping("/export") @@ -27,71 +79,20 @@ public class SysRoleController { // ExcelUtil util = new ExcelUtil(SysRole.class); // return util.exportExcel(list, "角色数据"); // } -// -// /** -// * 根据角色编号获取详细信息 -// */ -// @PreAuthorize("@ss.hasPermi('system:role:query')") -// @GetMapping(value = "/{roleId}") -// public AjaxResult getInfo(@PathVariable Long roleId) -// { -// return AjaxResult.success(roleService.selectRoleById(roleId)); -// } -// -// /** -// * 新增角色 -// */ -// @PreAuthorize("@ss.hasPermi('system:role:add')") -// @Log(title = "角色管理", businessType = BusinessType.INSERT) -// @PostMapping -// public AjaxResult add(@Validated @RequestBody SysRole role) -// { -// if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role))) -// { -// return AjaxResult.error("新增角色'" + role.getRoleName() + "'失败,角色名称已存在"); -// } -// else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role))) -// { -// return AjaxResult.error("新增角色'" + role.getRoleName() + "'失败,角色权限已存在"); -// } -// role.setCreateBy(SecurityUtils.getUsername()); -// return toAjax(roleService.insertRole(role)); -// -// } -// -// /** -// * 修改保存角色 -// */ + + @ApiOperation("修改角色状态") + @PostMapping("/update-status") + @ApiImplicitParams(value = { + @ApiImplicitParam(name = "id", value = "角色编号", required = true, example = "1024"), + @ApiImplicitParam(name = "status", value = "状态", required = true, example = "1") + }) // @PreAuthorize("@ss.hasPermi('system:role:edit')") // @Log(title = "角色管理", businessType = BusinessType.UPDATE) -// @PutMapping -// public AjaxResult edit(@Validated @RequestBody SysRole role) -// { -// roleService.checkRoleAllowed(role); -// if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role))) -// { -// return AjaxResult.error("修改角色'" + role.getRoleName() + "'失败,角色名称已存在"); -// } -// else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role))) -// { -// return AjaxResult.error("修改角色'" + role.getRoleName() + "'失败,角色权限已存在"); -// } -// role.setUpdateBy(SecurityUtils.getUsername()); -// -// if (roleService.updateRole(role) > 0) -// { -// // 更新缓存用户权限 -// LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest()); -// if (StringUtils.isNotNull(loginUser.getUser()) && !loginUser.getUser().isAdmin()) -// { -// loginUser.setPermissions(permissionService.getMenuPermission(loginUser.getUser())); -// loginUser.setUser(userService.selectUserByUserName(loginUser.getUser().getUserName())); -// tokenService.setLoginUser(loginUser); -// } -// return AjaxResult.success(); -// } -// return AjaxResult.error("修改角色'" + role.getRoleName() + "'失败,请联系管理员"); -// } + public CommonResult updateRoleStatus(@RequestParam("id") Long id, @RequestParam("status") Integer status) { + roleService.updateRoleStatus(id, status); + return success(true); + } + // // /** // * 修改保存数据权限 @@ -104,30 +105,8 @@ public class SysRoleController { // roleService.checkRoleAllowed(role); // return toAjax(roleService.authDataScope(role)); // } -// -// /** -// * 状态修改 -// */ -// @PreAuthorize("@ss.hasPermi('system:role:edit')") -// @Log(title = "角色管理", businessType = BusinessType.UPDATE) -// @PutMapping("/changeStatus") -// public AjaxResult changeStatus(@RequestBody SysRole role) -// { -// roleService.checkRoleAllowed(role); -// role.setUpdateBy(SecurityUtils.getUsername()); -// return toAjax(roleService.updateRoleStatus(role)); -// } -// -// /** -// * 删除角色 -// */ -// @PreAuthorize("@ss.hasPermi('system:role:remove')") -// @Log(title = "角色管理", businessType = BusinessType.DELETE) -// @DeleteMapping("/{roleIds}") -// public AjaxResult remove(@PathVariable Long[] roleIds) -// { -// return toAjax(roleService.deleteRoleByIds(roleIds)); -// } + + // // /** // * 获取角色选择框列表 diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/vo/role/SysMenuPageReqVO.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/vo/role/SysMenuPageReqVO.java deleted file mode 100644 index 10837c38b..000000000 --- a/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/vo/role/SysMenuPageReqVO.java +++ /dev/null @@ -1,13 +0,0 @@ -package cn.iocoder.dashboard.modules.system.controller.permission.vo.role; - -import cn.iocoder.dashboard.common.pojo.PageParam; -import io.swagger.annotations.ApiModel; -import lombok.Data; -import lombok.EqualsAndHashCode; - -@ApiModel("角色分页 Request VO") -@Data -@EqualsAndHashCode(callSuper = true) -public class SysMenuPageReqVO extends PageParam { - -} diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/vo/role/SysRolePageReqVO.java b/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/vo/role/SysRolePageReqVO.java new file mode 100644 index 000000000..41ac1adb0 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/modules/system/controller/permission/vo/role/SysRolePageReqVO.java @@ -0,0 +1,20 @@ +package cn.iocoder.dashboard.modules.system.controller.permission.vo.role; + +import cn.iocoder.dashboard.common.pojo.PageParam; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@ApiModel("角色分页 Request VO") +@Data +@EqualsAndHashCode(callSuper = true) +public class SysRolePageReqVO extends PageParam { + + @ApiModelProperty(value = "角色名称", example = "芋道", notes = "模糊匹配") + private String name; + + @ApiModelProperty(value = "展示状态", example = "1", notes = "参见 SysCommonStatusEnum 枚举类") + private Integer status; + +} diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/convert/permission/SysRoleConvert.java b/src/main/java/cn/iocoder/dashboard/modules/system/convert/permission/SysRoleConvert.java new file mode 100644 index 000000000..7fc2287cb --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/modules/system/convert/permission/SysRoleConvert.java @@ -0,0 +1,27 @@ +package cn.iocoder.dashboard.modules.system.convert.permission; + +import cn.iocoder.dashboard.common.pojo.PageResult; +import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRoleCreateReqVO; +import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRoleRespVO; +import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRoleUpdateReqVO; +import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysRoleDO; +import com.baomidou.mybatisplus.core.metadata.IPage; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.factory.Mappers; + +@Mapper +public interface SysRoleConvert { + + SysRoleConvert INSTANCE = Mappers.getMapper(SysRoleConvert.class); + + SysRoleDO convert(SysRoleUpdateReqVO bean); + + SysRoleRespVO convert(SysRoleDO bean); + + @Mapping(source = "records", target = "list") + PageResult convertPage(IPage page); + + SysRoleDO convert(SysRoleCreateReqVO bean); + +} diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dao/permission/SysRoleDeptMapper.java b/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dao/permission/SysRoleDeptMapper.java deleted file mode 100644 index dd9052c1b..000000000 --- a/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dao/permission/SysRoleDeptMapper.java +++ /dev/null @@ -1,9 +0,0 @@ -package cn.iocoder.dashboard.modules.system.dal.mysql.dao.permission; - -import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysRoleDeptDO; -import com.baomidou.mybatisplus.core.mapper.BaseMapper; -import org.apache.ibatis.annotations.Mapper; - -@Mapper -public interface SysRoleDeptMapper extends BaseMapper { -} diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dao/permission/SysRoleMapper.java b/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dao/permission/SysRoleMapper.java index e148e2485..188033fb4 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dao/permission/SysRoleMapper.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dao/permission/SysRoleMapper.java @@ -1,9 +1,29 @@ package cn.iocoder.dashboard.modules.system.dal.mysql.dao.permission; +import cn.iocoder.dashboard.framework.mybatis.core.query.QueryWrapperX; +import cn.iocoder.dashboard.framework.mybatis.core.util.MyBatisUtils; +import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRolePageReqVO; import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysRoleDO; import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.baomidou.mybatisplus.core.metadata.IPage; import org.apache.ibatis.annotations.Mapper; @Mapper public interface SysRoleMapper extends BaseMapper { + + default IPage selectPage(SysRolePageReqVO reqVO) { + return selectPage(MyBatisUtils.buildPage(reqVO), + new QueryWrapperX().likeIfPresent("name", reqVO.getName()) + .eqIfPresent("status", reqVO.getStatus())); + } + + default SysRoleDO selectByName(String name) { + return selectOne(new QueryWrapperX().eq("name", name)); + } + + default SysRoleDO selectByCode(String code) { + return selectOne(new QueryWrapperX().eq("code", code)); + } + + } diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dataobject/permission/SysRoleDO.java b/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dataobject/permission/SysRoleDO.java index c65c1d74a..ba893ce0e 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dataobject/permission/SysRoleDO.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dataobject/permission/SysRoleDO.java @@ -5,17 +5,21 @@ import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO; import cn.iocoder.dashboard.framework.security.core.enums.DataScopeEnum; import cn.iocoder.dashboard.modules.system.enums.permission.RoleCodeEnum; import cn.iocoder.dashboard.modules.system.enums.permission.RoleTypeEnum; +import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; +import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler; import lombok.Data; import lombok.EqualsAndHashCode; +import java.util.List; + /** * 角色 DO * * @author ruoyi */ -@TableName("sys_role") +@TableName(value = "sys_role", autoResultMap = true) @Data @EqualsAndHashCode(callSuper = true) public class SysRoleDO extends BaseDO { @@ -39,18 +43,12 @@ public class SysRoleDO extends BaseDO { * 角色排序 */ private Integer sort; - /** - * 数据范围 - * - * 枚举类 {@link DataScopeEnum} - */ - private Integer dataScope; /** * 角色状态 * * 枚举 {@link CommonStatusEnum} */ - private String status; + private Integer status; /** * 角色类型 * @@ -63,13 +61,17 @@ public class SysRoleDO extends BaseDO { private String remark; /** - * 菜单树选择项是否关联显示( 0:父子不互相关联显示 1:父子互相关联显示) + * 数据范围 + * + * 枚举 {@link DataScopeEnum} */ - private boolean menuCheckStrictly; - + private Integer dataScope; /** - * 部门树选择项是否关联显示(0:父子不互相关联显示 1:父子互相关联显示 ) + * 数据范围(指定部门数组) + * + * 适用于 {@link #dataScope} 的值为 {@link DataScopeEnum#DEPT_CUSTOM} 时 */ - private boolean deptCheckStrictly; + @TableField(typeHandler = FastjsonTypeHandler.class) + private List dataScopeDeptIds; } diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dataobject/permission/SysRoleDeptDO.java b/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dataobject/permission/SysRoleDeptDO.java deleted file mode 100644 index c0bfc9ced..000000000 --- a/src/main/java/cn/iocoder/dashboard/modules/system/dal/mysql/dataobject/permission/SysRoleDeptDO.java +++ /dev/null @@ -1,33 +0,0 @@ -package cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission; - -import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO; -import com.baomidou.mybatisplus.annotation.TableId; -import com.baomidou.mybatisplus.annotation.TableName; -import lombok.Data; -import lombok.EqualsAndHashCode; - -/** - * 部门和角色关联 DO - * - * @author ruoyi - */ -@TableName("sys_role_dept") -@Data -@EqualsAndHashCode(callSuper = true) -public class SysRoleDeptDO extends BaseDO { - - /** - * 自增主键 - */ - @TableId - private Long id; - /** - * 部门 ID - */ - private Long deptId; - /** - * 角色 ID - */ - private Long roleId; - -} diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/enums/SysErrorCodeConstants.java b/src/main/java/cn/iocoder/dashboard/modules/system/enums/SysErrorCodeConstants.java index 10c3518b3..7d0b8a788 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/enums/SysErrorCodeConstants.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/enums/SysErrorCodeConstants.java @@ -26,4 +26,11 @@ public interface SysErrorCodeConstants { ErrorCode MENU_EXISTS_CHILDREN = new ErrorCode(1002002004, "存在子菜单,无法删除"); ErrorCode MENU_PARENT_NOT_DIR_OR_MENU = new ErrorCode(1002002005, "父菜单的类型必须是目录或者菜单"); + // ========== 角色模块 1002003000 ========== + ErrorCode ROLE_NOT_EXISTS = new ErrorCode(1002003000, "角色不存在"); + ErrorCode ROLE_NAME_DUPLICATE = new ErrorCode(1002003001, "已经存在名为【{}}】的角色"); + ErrorCode ROLE_CODE_DUPLICATE = new ErrorCode(1002003002, "已经存在编码为【{}}】的角色"); + ErrorCode ROLE_CAN_NOT_UPDATE_SYSTEM_TYPE_ROLE = new ErrorCode(1002003004, "不能修改类型为系统内置的角色"); + ErrorCode ROLE_CAN_NOT_DELETE_SYSTEM_TYPE_ROLE = new ErrorCode(1002003005, "不能删除类型为系统内置的角色"); + } diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/auth/impl/SysAuthServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/auth/impl/SysAuthServiceImpl.java index 98fcba533..e78e7b582 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/auth/impl/SysAuthServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/auth/impl/SysAuthServiceImpl.java @@ -5,22 +5,14 @@ import cn.hutool.core.util.StrUtil; import cn.iocoder.dashboard.common.enums.CommonStatusEnum; import cn.iocoder.dashboard.framework.security.config.SecurityProperties; import cn.iocoder.dashboard.framework.security.core.LoginUser; -import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthMenuRespVO; -import cn.iocoder.dashboard.modules.system.controller.auth.vo.SysAuthPermissionInfoRespVO; import cn.iocoder.dashboard.modules.system.convert.auth.SysAuthConvert; -import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysMenuDO; -import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysRoleDO; import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.user.SysUserDO; import cn.iocoder.dashboard.modules.system.dal.redis.dao.auth.SysLoginUserRedisDAO; -import cn.iocoder.dashboard.modules.system.enums.permission.MenuTypeEnum; import cn.iocoder.dashboard.modules.system.enums.user.UserStatus; import cn.iocoder.dashboard.modules.system.service.auth.SysAuthService; import cn.iocoder.dashboard.modules.system.service.auth.SysTokenService; import cn.iocoder.dashboard.modules.system.service.permission.SysPermissionService; -import cn.iocoder.dashboard.modules.system.service.permission.SysRoleService; import cn.iocoder.dashboard.modules.system.service.user.SysUserService; -import cn.iocoder.dashboard.util.collection.CollectionUtils; -import cn.iocoder.dashboard.util.collection.SetUtils; import cn.iocoder.dashboard.util.date.DateUtils; import io.jsonwebtoken.Claims; import io.jsonwebtoken.JwtException; @@ -37,9 +29,8 @@ import org.springframework.stereotype.Service; import org.springframework.util.Assert; import javax.annotation.Resource; +import java.util.Collections; import java.util.Date; -import java.util.HashSet; -import java.util.List; import java.util.Set; import static cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil.exception; @@ -64,8 +55,6 @@ public class SysAuthServiceImpl implements SysAuthService { @Resource private SysUserService userService; @Resource - private SysRoleService roleService; - @Resource private SysPermissionService permissionService; @Resource @@ -92,7 +81,7 @@ public class SysAuthServiceImpl implements SysAuthService { // 创建 LoginUser 对象 LoginUser loginUser = SysAuthConvert.INSTANCE.convert(user); loginUser.setUpdateTime(new Date()); - loginUser.setRoleIds(this.getUserRoleIds(loginUser.getUserId(), loginUser.getDeptId())); + loginUser.setRoleIds(this.getUserRoleIds(loginUser.getUserId())); return loginUser; } @@ -106,7 +95,7 @@ public class SysAuthServiceImpl implements SysAuthService { // 缓存登陆用户到 Redis 中 String sessionId = IdUtil.fastSimpleUUID(); loginUser.setUpdateTime(new Date()); - loginUser.setRoleIds(this.getUserRoleIds(loginUser.getUserId(), loginUser.getDeptId())); + loginUser.setRoleIds(this.getUserRoleIds(loginUser.getUserId())); loginUserRedisDAO.set(sessionId, loginUser); // 创建 Token @@ -156,15 +145,10 @@ public class SysAuthServiceImpl implements SysAuthService { * 获得 User 拥有的角色编号数组 * * @param userId 用户编号 - * @param deptId 科室编号 * @return 角色编号数组 */ - private Set getUserRoleIds(Long userId, Long deptId) { - // 用户拥有的角色 - Set roleIds = new HashSet<>(permissionService.listUserRoleIds(userId)); - // 部门拥有的角色 - CollectionUtils.addIfNotNull(roleIds, permissionService.getDeptRoleId(deptId)); - return roleIds; + private Set getUserRoleIds(Long userId) { + return permissionService.listUserRoleIds(userId, Collections.singleton(CommonStatusEnum.ENABLE.getStatus())); } @Override @@ -222,7 +206,7 @@ public class SysAuthServiceImpl implements SysAuthService { // 刷新 LoginUser 缓存 loginUser.setDeptId(user.getDeptId()); loginUser.setUpdateTime(new Date()); - loginUser.setRoleIds(this.getUserRoleIds(loginUser.getUserId(), loginUser.getDeptId())); + loginUser.setRoleIds(this.getUserRoleIds(loginUser.getUserId())); loginUserRedisDAO.set(sessionId, loginUser); } diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/SysPermissionService.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/SysPermissionService.java index ce89677c1..fe33f3440 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/SysPermissionService.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/SysPermissionService.java @@ -2,8 +2,10 @@ package cn.iocoder.dashboard.modules.system.service.permission; import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysMenuDO; +import javax.annotation.Nullable; import java.util.Collection; import java.util.List; +import java.util.Set; /** * 权限 Service 接口 @@ -33,26 +35,26 @@ public interface SysPermissionService { Collection menusStatuses); /** - * 获得用户拥有的角色编号数组 + * 获得用户拥有的角色编号集合 * * @param userId 用户编号 - * @return 角色编号数组 + * @param roleStatuses 角色状态集合. 允许为空,为空时不过滤 + * @return 角色编号集合 */ - List listUserRoleIds(Long userId); + Set listUserRoleIds(Long userId, @Nullable Collection roleStatuses); /** - * 获得部门拥有的角色编号 + * 处理角色删除时,删除关联授权角色 * - * @param deptId 部门编号 - * @return 角色编号 + * @param roleId 角色编号 */ - Long getDeptRoleId(Long deptId); + void processRoleDeleted(Long roleId); /** - * 删除授予给角色的菜单们 + * 处理菜单删除时,删除关联授权数据 * * @param menuId 菜单编号 */ - void deleteRolesMenuByMenuId(Long menuId); + void processMenuDeleted(Long menuId); } diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/SysRoleService.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/SysRoleService.java index fadb809a9..8a619a473 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/SysRoleService.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/SysRoleService.java @@ -1,5 +1,9 @@ package cn.iocoder.dashboard.modules.system.service.permission; +import cn.iocoder.dashboard.common.pojo.PageResult; +import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRoleCreateReqVO; +import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRolePageReqVO; +import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRoleUpdateReqVO; import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysRoleDO; import java.util.Collection; @@ -17,13 +21,21 @@ public interface SysRoleService { */ void init(); + /** + * 获得角色,从缓存中 + * + * @param id 角色编号 + * @return 角色 + */ + SysRoleDO getRoleFromCache(Long id); + /** * 获得角色数组,从缓存中 * - * @param roleIds 角色编号数组 + * @param ids 角色编号数组 * @return 角色数组 */ - List listRolesFromCache(Collection roleIds); + List listRolesFromCache(Collection ids); /** * 判断角色数组中,是否有管理员 @@ -33,4 +45,50 @@ public interface SysRoleService { */ boolean hasAnyAdmin(Collection roleList); + /** + * 创建角色 + * + * @param reqVO 创建角色信息 + * @return 角色编号 + */ + Long createRole(SysRoleCreateReqVO reqVO); + + /** + * 更新角色 + * + * @param reqVO 更新角色信息 + */ + void updateRole(SysRoleUpdateReqVO reqVO); + + /** + * 删除角色 + * + * @param id 角色编号 + */ + void deleteRole(Long id); + + /** + * 获得角色 + * + * @param id 角色编号 + * @return 角色 + */ + SysRoleDO getRole(Long id); + + /** + * 获得角色分页 + * + * @param reqVO 角色分页查询 + * @return 角色分页结果 + */ + PageResult pageRole(SysRolePageReqVO reqVO); + + /** + * 更新角色状态 + * + * @param id 角色编号 + * @param status 状态 + */ + void updateRoleStatus(Long id, Integer status); + } diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysMenuServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysMenuServiceImpl.java index 4f05a20d2..ba80e4ec8 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysMenuServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysMenuServiceImpl.java @@ -160,7 +160,7 @@ public class SysMenuServiceImpl implements SysMenuService { // 标记删除 menuMapper.deleteById(menuId); // 删除授予给角色的权限 - permissionService.deleteRolesMenuByMenuId(menuId); + permissionService.processMenuDeleted(menuId); } @Override diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysPermissionServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysPermissionServiceImpl.java index 596159182..ecbee70c3 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysPermissionServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysPermissionServiceImpl.java @@ -1,9 +1,12 @@ package cn.iocoder.dashboard.modules.system.service.permission.impl; -import cn.iocoder.dashboard.modules.system.dal.mysql.dao.permission.SysRoleDeptMapper; +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; -import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.*; +import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysMenuDO; +import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysRoleDO; +import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysRoleMenuDO; +import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysUserRoleDO; import cn.iocoder.dashboard.modules.system.service.permission.SysMenuService; import cn.iocoder.dashboard.modules.system.service.permission.SysPermissionService; import cn.iocoder.dashboard.modules.system.service.permission.SysRoleService; @@ -19,11 +22,12 @@ import javax.annotation.Resource; import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Set; /** * 权限 Service 实现类 * - * @author 初始化 + * @author 芋道源码 */ @Service @Slf4j @@ -50,8 +54,6 @@ public class SysPermissionServiceImpl implements SysPermissionService { private SysRoleMenuMapper roleMenuMapper; @Resource private SysUserRoleMapper userRoleMapper; - @Resource - private SysRoleDeptMapper roleDeptMapper; @Resource private SysRoleService roleService; @@ -96,19 +98,29 @@ public class SysPermissionServiceImpl implements SysPermissionService { } @Override - public List listUserRoleIds(Long userId) { + public Set listUserRoleIds(Long userId, Collection roleStatuses) { List userRoleList = userRoleMapper.selectListByUserId(userId); - return CollectionUtils.convertList(userRoleList, SysUserRoleDO::getRoleId); + // 过滤角色状态 + if (CollectionUtil.isNotEmpty(roleStatuses)) { + userRoleList.removeIf(userRoleDO -> { + SysRoleDO role = roleService.getRoleFromCache(userRoleDO.getRoleId()); + return role == null || !roleStatuses.contains(role.getStatus()); + }); + } + return CollectionUtils.convertSet(userRoleList, SysUserRoleDO::getRoleId); } @Override - public Long getDeptRoleId(Long deptId) { - SysRoleDeptDO roleDept = roleDeptMapper.selectById(deptId); - return roleDept != null ? roleDept.getRoleId() : null; + public void processRoleDeleted(Long roleId) { + // TODO 实现我 +// // 标记删除 RoleResource +// roleResourceMapper.deleteByRoleId(roleId); +// // 标记删除 AdminRole +// adminRoleMapper.deleteByRoleId(roleId); } @Override - public void deleteRolesMenuByMenuId(Long menuId) { + public void processMenuDeleted(Long menuId) { // TODO 实现我 } diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysRoleServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysRoleServiceImpl.java index be4db1059..d1875aafb 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysRoleServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysRoleServiceImpl.java @@ -1,13 +1,24 @@ package cn.iocoder.dashboard.modules.system.service.permission.impl; import cn.hutool.core.collection.CollectionUtil; +import cn.iocoder.dashboard.common.enums.CommonStatusEnum; +import cn.iocoder.dashboard.common.exception.util.ServiceExceptionUtil; +import cn.iocoder.dashboard.common.pojo.PageResult; +import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRoleCreateReqVO; +import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRolePageReqVO; +import cn.iocoder.dashboard.modules.system.controller.permission.vo.role.SysRoleUpdateReqVO; +import cn.iocoder.dashboard.modules.system.convert.permission.SysRoleConvert; import cn.iocoder.dashboard.modules.system.dal.mysql.dao.permission.SysRoleMapper; import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysRoleDO; import cn.iocoder.dashboard.modules.system.enums.permission.RoleCodeEnum; +import cn.iocoder.dashboard.modules.system.enums.permission.RoleTypeEnum; +import cn.iocoder.dashboard.modules.system.service.permission.SysPermissionService; import cn.iocoder.dashboard.modules.system.service.permission.SysRoleService; +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.util.StringUtils; import javax.annotation.PostConstruct; import javax.annotation.Resource; @@ -17,13 +28,20 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; +import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.*; + /** * 角色 Service 实现类 + * + * @author 芋道源码 */ @Service @Slf4j public class SysRoleServiceImpl implements SysRoleService { + @Resource + private SysPermissionService permissionService; + @Resource private SysRoleMapper roleMapper; @@ -51,11 +69,16 @@ public class SysRoleServiceImpl implements SysRoleService { } @Override - public List listRolesFromCache(Collection roleIds) { - if (CollectionUtil.isEmpty(roleIds)) { + public SysRoleDO getRoleFromCache(Long id) { + return roleCache.get(id); + } + + @Override + public List listRolesFromCache(Collection ids) { + if (CollectionUtil.isEmpty(ids)) { return Collections.emptyList(); } - return roleCache.values().stream().filter(roleDO -> roleIds.contains(roleDO.getId())) + return roleCache.values().stream().filter(roleDO -> ids.contains(roleDO.getId())) .collect(Collectors.toList()); } @@ -67,4 +90,104 @@ public class SysRoleServiceImpl implements SysRoleService { return roleList.stream().anyMatch(roleDO -> RoleCodeEnum.ADMIN.getKey().equals(roleDO.getCode())); } + @Override + public Long createRole(SysRoleCreateReqVO reqVO) { + // 校验角色 + checkDuplicateRole(reqVO.getName(), reqVO.getCode(), null); + // 插入到数据库 + SysRoleDO role = SysRoleConvert.INSTANCE.convert(reqVO); + role.setType(RoleTypeEnum.CUSTOM.getType()); + role.setStatus(CommonStatusEnum.ENABLE.getStatus()); + roleMapper.insert(role); + // 返回 + return role.getId(); + } + + @Override + public void updateRole(SysRoleUpdateReqVO reqVO) { + // 校验更新的角色是否存在 + SysRoleDO role = roleMapper.selectById(reqVO.getId()); + if (roleMapper.selectById(reqVO.getId()) == null) { + throw ServiceExceptionUtil.exception(ROLE_NOT_EXISTS); + } + // 内置角色,不允许修改 + if (RoleTypeEnum.SYSTEM.getType().equals(role.getType())) { + throw ServiceExceptionUtil.exception(ROLE_CAN_NOT_UPDATE_SYSTEM_TYPE_ROLE); + } + // 校验角色的唯一字段是否重复 + checkDuplicateRole(reqVO.getName(), reqVO.getCode(), reqVO.getId()); + // 更新到数据库 + SysRoleDO updateObject = SysRoleConvert.INSTANCE.convert(reqVO); + roleMapper.updateById(updateObject); + } + + @Override + public void deleteRole(Long id) { + // 校验删除的角色是否存在 + SysRoleDO roleDO = roleMapper.selectById(id); + if (roleMapper.selectById(id) == null) { + throw ServiceExceptionUtil.exception(ROLE_NOT_EXISTS); + } + // 内置角色,不允许删除 + if (RoleTypeEnum.SYSTEM.getType().equals(roleDO.getType())) { + throw ServiceExceptionUtil.exception(ROLE_CAN_NOT_DELETE_SYSTEM_TYPE_ROLE); + } + // 标记删除 + roleMapper.deleteById(id); + // 删除相关数据 + permissionService.processRoleDeleted(id); + } + + @Override + public SysRoleDO getRole(Long id) { + return roleMapper.selectById(id); + } + + + @Override + public PageResult pageRole(SysRolePageReqVO reqVO) { + IPage roleDOPage = roleMapper.selectPage(reqVO); + return SysRoleConvert.INSTANCE.convertPage(roleDOPage); + } + + @Override + public void updateRoleStatus(Long id, Integer status) { + // 校验修改的角色是否存在 + SysRoleDO roleDO = roleMapper.selectById(id); + if (roleMapper.selectById(id) == null) { + throw ServiceExceptionUtil.exception(ROLE_NOT_EXISTS); + } + SysRoleDO updateObject = new SysRoleDO(); + updateObject.setId(id); + updateObject.setStatus(status); + roleMapper.updateById(updateObject); + } + + /** + * 校验角色的唯一字段是否重复 + * + * 1. 是否存在相同名字的角色 + * 2. 是否存在相同编码的角色 + * + * @param name 角色名字 + * @param code 角色额编码 + * @param id 角色编号 + */ + private void checkDuplicateRole(String name, String code, Long id) { + // 1. 该 name 名字被其它角色所使用 + SysRoleDO role = roleMapper.selectByName(name); + if (role != null && !role.getId().equals(id)) { + throw ServiceExceptionUtil.exception(ROLE_NAME_DUPLICATE, name); + } + // 2. 是否存在相同编码的角色 + if (!StringUtils.hasText(code)) { + return; + } + // 该 code 编码被其它角色所使用 + role = roleMapper.selectByCode(code); + if (role != null && !role.getId().equals(id)) { + throw ServiceExceptionUtil.exception(ROLE_CODE_DUPLICATE, name); + } + } + } diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index b66889907..ab6d44fe6 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -43,6 +43,7 @@ yudao: mybatis-plus: configuration: map-underscore-to-camel-case: true # 虽然默认为 true ,但是还是显示去指定下。 + log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 打印日志 global-config: db-config: id-type: auto # 自增 ID