IAM初始化时自动增加初始的相关菜单权限列表数据

This commit is contained in:
Zhaoyang 2020-03-19 13:14:39 +08:00
parent 04a4265829
commit 8ebce5a1d0
3 changed files with 45 additions and 4 deletions

View File

@ -3,6 +3,7 @@ package com.diboot.iam.service;
import com.diboot.iam.dto.IamFrontendPermissionDTO; import com.diboot.iam.dto.IamFrontendPermissionDTO;
import com.diboot.iam.entity.IamFrontendPermission; import com.diboot.iam.entity.IamFrontendPermission;
import com.diboot.iam.vo.IamFrontendPermissionListVO;
import java.util.List; import java.util.List;
@ -15,6 +16,12 @@ import java.util.List;
*/ */
public interface IamFrontendPermissionService extends BaseIamService<IamFrontendPermission> { public interface IamFrontendPermissionService extends BaseIamService<IamFrontendPermission> {
/***
* 深度创建菜单权限及其所有子列表
* @param iamFrontendPermissionListVO
*/
void deepCreatePermissionAndChildren(IamFrontendPermissionListVO iamFrontendPermissionListVO);
/*** /***
* 创建菜单和按钮/权限列表 * 创建菜单和按钮/权限列表
* @param iamFrontendPermissionDTO * @param iamFrontendPermissionDTO

View File

@ -13,6 +13,7 @@ import com.diboot.iam.dto.IamFrontendPermissionDTO;
import com.diboot.iam.entity.IamFrontendPermission; import com.diboot.iam.entity.IamFrontendPermission;
import com.diboot.iam.mapper.IamFrontendPermissionMapper; import com.diboot.iam.mapper.IamFrontendPermissionMapper;
import com.diboot.iam.service.IamFrontendPermissionService; import com.diboot.iam.service.IamFrontendPermissionService;
import com.diboot.iam.vo.IamFrontendPermissionListVO;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -34,6 +35,23 @@ import java.util.stream.Collectors;
@Slf4j @Slf4j
public class IamFrontendPermissionServiceImpl extends BaseIamServiceImpl<IamFrontendPermissionMapper, IamFrontendPermission> implements IamFrontendPermissionService { public class IamFrontendPermissionServiceImpl extends BaseIamServiceImpl<IamFrontendPermissionMapper, IamFrontendPermission> 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<IamFrontendPermissionListVO> children = iamFrontendPermissionListVO.getChildren();
if (V.notEmpty(children)) {
for (IamFrontendPermissionListVO vo : children) {
vo.setParentId(iamFrontendPermission.getId());
this.deepCreatePermissionAndChildren(vo);
}
}
}
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void createMenuAndPermissions(IamFrontendPermissionDTO iamFrontendPermissionDTO) { public void createMenuAndPermissions(IamFrontendPermissionDTO iamFrontendPermissionDTO) {

View File

@ -1,5 +1,6 @@
package com.diboot.iam.starter; package com.diboot.iam.starter;
import com.diboot.core.exception.BusinessException;
import com.diboot.core.service.DictionaryService; import com.diboot.core.service.DictionaryService;
import com.diboot.core.util.JSON; import com.diboot.core.util.JSON;
import com.diboot.core.vo.DictionaryVO; 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.IamRole;
import com.diboot.iam.entity.IamUser; import com.diboot.iam.entity.IamUser;
import com.diboot.iam.entity.IamUserRole; import com.diboot.iam.entity.IamUserRole;
import com.diboot.iam.service.IamAccountService; import com.diboot.iam.service.*;
import com.diboot.iam.service.IamRoleService; import com.diboot.iam.vo.IamFrontendPermissionListVO;
import com.diboot.iam.service.IamUserRoleService;
import com.diboot.iam.service.IamUserService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -36,6 +35,8 @@ public class IamBaseInitializer{
private IamUserRoleService iamUserRoleService; private IamUserRoleService iamUserRoleService;
@Autowired @Autowired
private IamAccountService iamAccountService; 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}]}", "{\"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\"}]}" "{\"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){ for(String dictJson : DICT_INIT_DATA){
DictionaryVO dictVo = JSON.toJavaObject(dictJson, DictionaryVO.class); DictionaryVO dictVo = JSON.toJavaObject(dictJson, DictionaryVO.class);
@ -56,6 +61,17 @@ public class IamBaseInitializer{
} }
DICT_INIT_DATA = null; 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 iamRole = new IamRole();
iamRole.setName("超级管理员").setCode("SUPER_ADMIN"); iamRole.setName("超级管理员").setCode("SUPER_ADMIN");