From 8ebce5a1d09c740c5c8df9d9f7967bfa97efeb74 Mon Sep 17 00:00:00 2001 From: Zhaoyang Date: Thu, 19 Mar 2020 13:14:39 +0800 Subject: [PATCH] =?UTF-8?q?IAM=E5=88=9D=E5=A7=8B=E5=8C=96=E6=97=B6?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=A2=9E=E5=8A=A0=E5=88=9D=E5=A7=8B=E7=9A=84?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E8=8F=9C=E5=8D=95=E6=9D=83=E9=99=90=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/IamFrontendPermissionService.java | 7 ++++++ .../IamFrontendPermissionServiceImpl.java | 18 ++++++++++++++ .../iam/starter/IamBaseInitializer.java | 24 +++++++++++++++---- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/iam-base-starter/src/main/java/com/diboot/iam/service/IamFrontendPermissionService.java b/iam-base-starter/src/main/java/com/diboot/iam/service/IamFrontendPermissionService.java index df55928..e1241ea 100644 --- a/iam-base-starter/src/main/java/com/diboot/iam/service/IamFrontendPermissionService.java +++ b/iam-base-starter/src/main/java/com/diboot/iam/service/IamFrontendPermissionService.java @@ -3,6 +3,7 @@ package com.diboot.iam.service; import com.diboot.iam.dto.IamFrontendPermissionDTO; import com.diboot.iam.entity.IamFrontendPermission; +import com.diboot.iam.vo.IamFrontendPermissionListVO; import java.util.List; @@ -15,6 +16,12 @@ import java.util.List; */ public interface IamFrontendPermissionService extends BaseIamService { + /*** + * 深度创建菜单权限及其所有子列表 + * @param iamFrontendPermissionListVO + */ + void deepCreatePermissionAndChildren(IamFrontendPermissionListVO iamFrontendPermissionListVO); + /*** * 创建菜单和按钮/权限列表 * @param iamFrontendPermissionDTO diff --git a/iam-base-starter/src/main/java/com/diboot/iam/service/impl/IamFrontendPermissionServiceImpl.java b/iam-base-starter/src/main/java/com/diboot/iam/service/impl/IamFrontendPermissionServiceImpl.java index 0527780..74d8e57 100644 --- a/iam-base-starter/src/main/java/com/diboot/iam/service/impl/IamFrontendPermissionServiceImpl.java +++ b/iam-base-starter/src/main/java/com/diboot/iam/service/impl/IamFrontendPermissionServiceImpl.java @@ -13,6 +13,7 @@ import com.diboot.iam.dto.IamFrontendPermissionDTO; import com.diboot.iam.entity.IamFrontendPermission; import com.diboot.iam.mapper.IamFrontendPermissionMapper; import com.diboot.iam.service.IamFrontendPermissionService; +import com.diboot.iam.vo.IamFrontendPermissionListVO; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -34,6 +35,23 @@ import java.util.stream.Collectors; @Slf4j public class IamFrontendPermissionServiceImpl extends BaseIamServiceImpl implements IamFrontendPermissionService { + @Override + @Transactional(rollbackFor = Exception.class) + public void deepCreatePermissionAndChildren(IamFrontendPermissionListVO iamFrontendPermissionListVO) { + IamFrontendPermission iamFrontendPermission = (IamFrontendPermission) iamFrontendPermissionListVO; + if(!super.createEntity(iamFrontendPermission)){ + log.warn("新建菜单权限失败,displayType="+iamFrontendPermission.getDisplayType()); + throw new BusinessException(Status.FAIL_OPERATION, "新建菜单权限失败"); + } + List children = iamFrontendPermissionListVO.getChildren(); + if (V.notEmpty(children)) { + for (IamFrontendPermissionListVO vo : children) { + vo.setParentId(iamFrontendPermission.getId()); + this.deepCreatePermissionAndChildren(vo); + } + } + } + @Override @Transactional(rollbackFor = Exception.class) public void createMenuAndPermissions(IamFrontendPermissionDTO iamFrontendPermissionDTO) { diff --git a/iam-base-starter/src/main/java/com/diboot/iam/starter/IamBaseInitializer.java b/iam-base-starter/src/main/java/com/diboot/iam/starter/IamBaseInitializer.java index 104932b..bc5fd00 100644 --- a/iam-base-starter/src/main/java/com/diboot/iam/starter/IamBaseInitializer.java +++ b/iam-base-starter/src/main/java/com/diboot/iam/starter/IamBaseInitializer.java @@ -1,5 +1,6 @@ package com.diboot.iam.starter; +import com.diboot.core.exception.BusinessException; import com.diboot.core.service.DictionaryService; import com.diboot.core.util.JSON; import com.diboot.core.vo.DictionaryVO; @@ -8,10 +9,8 @@ import com.diboot.iam.entity.IamAccount; import com.diboot.iam.entity.IamRole; import com.diboot.iam.entity.IamUser; import com.diboot.iam.entity.IamUserRole; -import com.diboot.iam.service.IamAccountService; -import com.diboot.iam.service.IamRoleService; -import com.diboot.iam.service.IamUserRoleService; -import com.diboot.iam.service.IamUserService; +import com.diboot.iam.service.*; +import com.diboot.iam.vo.IamFrontendPermissionListVO; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -36,6 +35,8 @@ public class IamBaseInitializer{ private IamUserRoleService iamUserRoleService; @Autowired private IamAccountService iamAccountService; + @Autowired + private IamFrontendPermissionService iamFrontendPermissionService; /** * 插入初始化数据 @@ -49,6 +50,10 @@ public class IamBaseInitializer{ "{\"type\":\"PERMISSION_TYPE\", \"itemName\":\"权限类型\", \"description\":\"IAM权限类型\", \"children\":[{\"itemName\":\"菜单\", \"itemValue\":\"MENU\", \"sortId\":1},{\"itemName\":\"操作\", \"itemValue\":\"OPERATION\", \"sortId\":2}]}", "{\"itemName\":\"前端按钮/权限编码\",\"type\":\"FRONTEND_PERMISSION_CODE\",\"description\":\"前端按钮/权限编码 常用选项\",\"children\":[{\"sortId\":1,\"itemName\":\"详情\",\"itemValue\":\"detail\"},{\"sortId\":2,\"itemName\":\"新建\",\"itemValue\":\"create\"},{\"sortId\":3,\"itemName\":\"更新\",\"itemValue\":\"update\"},{\"sortId\":4,\"itemName\":\"删除\",\"itemValue\":\"delete\"},{\"sortId\":5,\"itemName\":\"导出\",\"itemValue\":\"export\"},{\"sortId\":6,\"itemName\":\"导入\",\"itemValue\":\"import\"}]}" }; + // 插入iam-base所需的初始权限数据 + String[] FRONTEND_PERMISSION_DATA = { + "{\"displayType\":\"MENU\",\"displayName\":\"系统管理\",\"frontendCode\":\"system\",\"children\":[{\"displayType\":\"MENU\",\"displayName\":\"数据字典管理\",\"frontendCode\":\"Dictionary\",\"apiSet\":\"GET:/dictionary/list\",\"sortId\":\"22\",\"children\":[{\"displayType\":\"PERMISSION\",\"displayName\":\"详情\",\"frontendCode\":\"detail\",\"apiSet\":\"GET:/dictionary/{id}\",\"sortId\":\"6\"},{\"displayType\":\"PERMISSION\",\"displayName\":\"新建\",\"frontendCode\":\"create\",\"apiSet\":\"POST:/dictionary/\",\"sortId\":\"5\"},{\"displayType\":\"PERMISSION\",\"displayName\":\"更新\",\"frontendCode\":\"update\",\"apiSet\":\"PUT:/dictionary/{id}\",\"sortId\":\"4\"},{\"displayType\":\"PERMISSION\",\"displayName\":\"删除\",\"frontendCode\":\"delete\",\"apiSet\":\"DELETE:/dictionary/{id}\",\"sortId\":\"3\"}]},{\"displayType\":\"MENU\",\"displayName\":\"系统用户管理\",\"frontendCode\":\"IamUser\",\"apiSet\":\"GET:/iam/user/list\",\"sortId\":\"17\",\"children\":[{\"displayType\":\"PERMISSION\",\"displayName\":\"详情\",\"frontendCode\":\"detail\",\"apiSet\":\"GET:/iam/user/{id}\",\"sortId\":\"11\"},{\"displayType\":\"PERMISSION\",\"displayName\":\"新建\",\"frontendCode\":\"create\",\"apiSet\":\"POST:/iam/user/\",\"sortId\":\"10\"},{\"displayType\":\"PERMISSION\",\"displayName\":\"更新\",\"frontendCode\":\"update\",\"apiSet\":\"PUT:/iam/user/{id}\",\"sortId\":\"9\"},{\"displayType\":\"PERMISSION\",\"displayName\":\"删除\",\"frontendCode\":\"delete\",\"apiSet\":\"DELETE:/iam/user/{id}\",\"sortId\":\"8\"}]},{\"displayType\":\"MENU\",\"displayName\":\"角色权限管理\",\"frontendCode\":\"IamRole\",\"apiSet\":\"GET:/iam/role/list\",\"sortId\":\"12\",\"children\":[{\"displayType\":\"PERMISSION\",\"displayName\":\"详情\",\"frontendCode\":\"detail\",\"apiSet\":\"GET:/iam/role/{id}\",\"sortId\":\"16\"},{\"displayType\":\"PERMISSION\",\"displayName\":\"新建\",\"frontendCode\":\"create\",\"apiSet\":\"POST:/iam/role/\",\"sortId\":\"15\"},{\"displayType\":\"PERMISSION\",\"displayName\":\"更新\",\"frontendCode\":\"update\",\"apiSet\":\"PUT:/iam/role/{id}\",\"sortId\":\"14\"},{\"displayType\":\"PERMISSION\",\"displayName\":\"删除\",\"frontendCode\":\"delete\",\"apiSet\":\"DELETE:/iam/role/{id}\",\"sortId\":\"13\"}]},{\"displayType\":\"MENU\",\"displayName\":\"菜单权限管理\",\"frontendCode\":\"IamFrontendPermission\",\"apiSet\":\"GET:/iam/frontendPermission/list\",\"sortId\":\"7\",\"children\":[{\"displayType\":\"PERMISSION\",\"displayName\":\"详情\",\"frontendCode\":\"detail\",\"apiSet\":\"GET:/iam/frontendPermission/{id}\",\"sortId\":\"23\"},{\"displayType\":\"PERMISSION\",\"displayName\":\"新建\",\"frontendCode\":\"create\",\"apiSet\":\"POST:/frontendPermission/user/\",\"sortId\":\"21\"},{\"displayType\":\"PERMISSION\",\"displayName\":\"更新\",\"frontendCode\":\"update\",\"apiSet\":\"PUT:/iam/frontendPermission/{id}\",\"sortId\":\"20\"},{\"displayType\":\"PERMISSION\",\"displayName\":\"删除\",\"frontendCode\":\"delete\",\"apiSet\":\"DELETE:/iam/frontendPermission/{id}\",\"sortId\":\"19\"},{\"displayType\":\"PERMISSION\",\"displayName\":\"排序\",\"frontendCode\":\"sort\",\"apiSet\":\"POST:/iam/frontendPermission/sortList\",\"sortId\":\"18\"}]},{\"displayType\":\"MENU\",\"displayName\":\"登录日志查看\",\"frontendCode\":\"IamLoginTrace\",\"apiSet\":\"GET:/iam/loginTrace/list\",\"sortId\":\"2\"}]}" + }; // 插入数据字典 for(String dictJson : DICT_INIT_DATA){ DictionaryVO dictVo = JSON.toJavaObject(dictJson, DictionaryVO.class); @@ -56,6 +61,17 @@ public class IamBaseInitializer{ } DICT_INIT_DATA = null; + // 插入多层级菜单权限初始数据 + try { + for (String frontendPermissionJson : FRONTEND_PERMISSION_DATA) { + IamFrontendPermissionListVO permissionListVO = JSON.toJavaObject(frontendPermissionJson, IamFrontendPermissionListVO.class); + iamFrontendPermissionService.deepCreatePermissionAndChildren(permissionListVO); + } + FRONTEND_PERMISSION_DATA = null; + } catch (BusinessException e){ + log.error("初始化菜单权限数据出错,请手动配置菜单初始的权限数据", e.getMessage()); + } + // 插入超级管理员用户及角色 IamRole iamRole = new IamRole(); iamRole.setName("超级管理员").setCode("SUPER_ADMIN");