use of com.hccake.ballcat.system.model.entity.SysMenu in project ballcat by ballcat-projects.
the class SysUserServiceImpl method findUserInfo.
/**
* 通过查用户的全部信息
* @param sysUser 用户
* @return 用户信息
*/
@Override
public UserInfoDTO findUserInfo(SysUser sysUser) {
UserInfoDTO userInfoDTO = new UserInfoDTO();
userInfoDTO.setSysUser(sysUser);
// 超级管理员拥有所有角色
List<SysRole> roleList;
if (adminUserChecker.isAdminUser(sysUser)) {
roleList = sysRoleService.list();
} else {
roleList = sysUserRoleService.listRoles(sysUser.getUserId());
}
// 设置角色标识
Set<String> roleCodes = new HashSet<>();
for (SysRole role : roleList) {
roleCodes.add(role.getCode());
}
userInfoDTO.setRoles(new HashSet<>(roleList));
userInfoDTO.setRoleCodes(roleCodes);
// 设置权限列表(permission)
Set<String> permissions = new HashSet<>();
Set<SysMenu> menus = new HashSet<>();
for (String roleCode : roleCodes) {
List<SysMenu> sysMenuList = sysMenuService.listByRoleCode(roleCode);
menus.addAll(sysMenuList);
List<String> permissionList = sysMenuList.stream().map(SysMenu::getPermission).filter(StrUtil::isNotEmpty).collect(Collectors.toList());
permissions.addAll(permissionList);
}
userInfoDTO.setMenus(menus);
userInfoDTO.setPermissions(permissions);
return userInfoDTO;
}
use of com.hccake.ballcat.system.model.entity.SysMenu in project ballcat by ballcat-projects.
the class SysMenuController method getUserPermission.
/**
* 返回当前用户的路由集合
* @return 当前用户的路由
*/
@GetMapping("/router")
@Operation(summary = "动态路由", description = "动态路由")
public R<List<SysMenuRouterVO>> getUserPermission() {
// 获取角色Code
User user = SecurityUtils.getUser();
Map<String, Object> attributes = user.getAttributes();
Object rolesObject = attributes.get(UserAttributeNameConstants.ROLE_CODES);
if (!(rolesObject instanceof Collection)) {
return R.ok(new ArrayList<>());
}
@SuppressWarnings("unchecked") Collection<String> roleCodes = (Collection<String>) rolesObject;
if (CollectionUtil.isEmpty(roleCodes)) {
return R.ok(new ArrayList<>());
}
// 获取符合条件的权限
Set<SysMenu> all = new HashSet<>();
roleCodes.forEach(roleCode -> all.addAll(sysMenuService.listByRoleCode(roleCode)));
// 筛选出菜单
List<SysMenuRouterVO> menuVOList = all.stream().filter(menuVo -> SysMenuType.BUTTON.getValue() != menuVo.getType()).sorted(Comparator.comparingInt(SysMenu::getSort)).map(SysMenuConverter.INSTANCE::poToRouterVo).collect(Collectors.toList());
return R.ok(menuVOList);
}
use of com.hccake.ballcat.system.model.entity.SysMenu in project ballcat by ballcat-projects.
the class SysMenuServiceImpl method create.
@Override
@Transactional(rollbackFor = Exception.class)
public boolean create(SysMenuCreateDTO sysMenuCreateDTO) {
SysMenu sysMenu = SysMenuConverter.INSTANCE.createDtoToPo(sysMenuCreateDTO);
Integer menuId = sysMenu.getId();
SysMenu existingMenu = baseMapper.selectById(menuId);
if (existingMenu != null) {
String errorMessage = String.format("ID [%s] 已被菜单 [%s] 使用,请更换其他菜单ID", menuId, existingMenu.getTitle());
throw new BusinessException(BaseResultCode.LOGIC_CHECK_ERROR.getCode(), errorMessage);
}
boolean saveSuccess = SqlHelper.retBool(baseMapper.insert(sysMenu));
Assert.isTrue(saveSuccess, () -> {
log.error("[create] 创建菜单失败,sysMenuCreateDTO: {}", sysMenuCreateDTO);
return new BusinessException(BaseResultCode.UPDATE_DATABASE_ERROR.getCode(), "创建菜单失败");
});
// 多语言保存事件发布
List<I18nMessage> i18nMessages = sysMenuCreateDTO.getI18nMessages();
if (CollectionUtil.isNotEmpty(i18nMessages)) {
eventPublisher.publishEvent(new I18nMessageCreateEvent(i18nMessages));
}
return saveSuccess;
}
use of com.hccake.ballcat.system.model.entity.SysMenu in project ballcat by ballcat-projects.
the class SysMenuServiceImpl method save.
/**
* 插入一条记录(选择字段,策略插入)
* @param sysMenu 实体对象
*/
@Override
public boolean save(SysMenu sysMenu) {
Integer menuId = sysMenu.getId();
SysMenu existingMenu = baseMapper.selectById(menuId);
if (existingMenu != null) {
String errorMessage = String.format("ID [%s] 已被菜单 [%s] 使用,请更换其他菜单ID", menuId, existingMenu.getTitle());
throw new BusinessException(BaseResultCode.LOGIC_CHECK_ERROR.getCode(), errorMessage);
}
return SqlHelper.retBool(baseMapper.insert(sysMenu));
}
use of com.hccake.ballcat.system.model.entity.SysMenu in project ballcat by ballcat-projects.
the class SysMenuServiceImpl method update.
@Override
@Transactional(rollbackFor = Exception.class)
public void update(SysMenuUpdateDTO sysMenuUpdateDTO) {
// 原来的菜单 Id
Integer originalId = sysMenuUpdateDTO.getOriginalId();
SysMenu sysMenu = SysMenuConverter.INSTANCE.updateDtoToPo(sysMenuUpdateDTO);
// 更新菜单信息
boolean updateSuccess = baseMapper.updateMenuAndId(originalId, sysMenu);
Assert.isTrue(updateSuccess, () -> {
log.error("[update] 更新菜单权限时,sql 执行异常,originalId:{},sysMenu:{}", originalId, sysMenu);
return new BusinessException(BaseResultCode.UPDATE_DATABASE_ERROR.getCode(), "更新菜单权限时,sql 执行异常");
});
// 如果未修改过 菜单id 直接返回
Integer menuId = sysMenuUpdateDTO.getId();
if (originalId.equals(menuId)) {
return;
}
// 修改过菜单id,则需要对应修改角色菜单的关联表信息,这里不需要 check,因为可能没有授权过该菜单,所以返回值为 0
sysRoleMenuService.updateMenuId(originalId, menuId);
// 更新子菜单的 parentId
baseMapper.updateParentId(originalId, menuId);
}
Aggregations