Search in sources :

Example 1 with SysMenu

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;
}
Also used : SysMenu(com.hccake.ballcat.system.model.entity.SysMenu) SysRole(com.hccake.ballcat.system.model.entity.SysRole) UserInfoDTO(com.hccake.ballcat.system.model.dto.UserInfoDTO)

Example 2 with SysMenu

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);
}
Also used : SysMenuUpdateDTO(com.hccake.ballcat.system.model.dto.SysMenuUpdateDTO) PathVariable(org.springframework.web.bind.annotation.PathVariable) SysMenuConverter(com.hccake.ballcat.system.converter.SysMenuConverter) SysMenuRouterVO(com.hccake.ballcat.system.model.vo.SysMenuRouterVO) SysMenuService(com.hccake.ballcat.system.service.SysMenuService) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequiredArgsConstructor(lombok.RequiredArgsConstructor) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ArrayList(java.util.ArrayList) RequestBody(org.springframework.web.bind.annotation.RequestBody) Valid(javax.validation.Valid) HashSet(java.util.HashSet) R(com.hccake.ballcat.common.model.result.R) UserAttributeNameConstants(com.hccake.ballcat.common.security.constant.UserAttributeNameConstants) Operation(io.swagger.v3.oas.annotations.Operation) PutMapping(org.springframework.web.bind.annotation.PutMapping) Map(java.util.Map) UpdateOperationLogging(com.hccake.ballcat.common.log.operation.annotation.UpdateOperationLogging) GetMapping(org.springframework.web.bind.annotation.GetMapping) SysMenuQO(com.hccake.ballcat.system.model.qo.SysMenuQO) SysMenu(com.hccake.ballcat.system.model.entity.SysMenu) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) PostMapping(org.springframework.web.bind.annotation.PostMapping) SecurityUtils(com.hccake.ballcat.common.security.util.SecurityUtils) SysMenuType(com.hccake.ballcat.system.enums.SysMenuType) CollectionUtil(cn.hutool.core.collection.CollectionUtil) BaseResultCode(com.hccake.ballcat.common.model.result.BaseResultCode) Collection(java.util.Collection) Set(java.util.Set) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) User(com.hccake.ballcat.common.security.userdetails.User) CreateOperationLogging(com.hccake.ballcat.common.log.operation.annotation.CreateOperationLogging) List(java.util.List) SysMenuPageVO(com.hccake.ballcat.system.model.vo.SysMenuPageVO) SysMenuCreateDTO(com.hccake.ballcat.system.model.dto.SysMenuCreateDTO) Tag(io.swagger.v3.oas.annotations.tags.Tag) SysMenuGrantVO(com.hccake.ballcat.system.model.vo.SysMenuGrantVO) Comparator(java.util.Comparator) DeleteOperationLogging(com.hccake.ballcat.common.log.operation.annotation.DeleteOperationLogging) User(com.hccake.ballcat.common.security.userdetails.User) SysMenuConverter(com.hccake.ballcat.system.converter.SysMenuConverter) SysMenuRouterVO(com.hccake.ballcat.system.model.vo.SysMenuRouterVO) SysMenu(com.hccake.ballcat.system.model.entity.SysMenu) Collection(java.util.Collection) HashSet(java.util.HashSet) GetMapping(org.springframework.web.bind.annotation.GetMapping) Operation(io.swagger.v3.oas.annotations.Operation)

Example 3 with SysMenu

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;
}
Also used : BusinessException(com.hccake.ballcat.common.core.exception.BusinessException) SysMenu(com.hccake.ballcat.system.model.entity.SysMenu) I18nMessage(com.hccake.ballcat.common.i18n.I18nMessage) I18nMessageCreateEvent(com.hccake.ballcat.common.i18n.I18nMessageCreateEvent) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with SysMenu

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));
}
Also used : BusinessException(com.hccake.ballcat.common.core.exception.BusinessException) SysMenu(com.hccake.ballcat.system.model.entity.SysMenu)

Example 5 with 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);
}
Also used : BusinessException(com.hccake.ballcat.common.core.exception.BusinessException) SysMenu(com.hccake.ballcat.system.model.entity.SysMenu) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

SysMenu (com.hccake.ballcat.system.model.entity.SysMenu)5 BusinessException (com.hccake.ballcat.common.core.exception.BusinessException)3 Transactional (org.springframework.transaction.annotation.Transactional)2 CollectionUtil (cn.hutool.core.collection.CollectionUtil)1 I18nMessage (com.hccake.ballcat.common.i18n.I18nMessage)1 I18nMessageCreateEvent (com.hccake.ballcat.common.i18n.I18nMessageCreateEvent)1 CreateOperationLogging (com.hccake.ballcat.common.log.operation.annotation.CreateOperationLogging)1 DeleteOperationLogging (com.hccake.ballcat.common.log.operation.annotation.DeleteOperationLogging)1 UpdateOperationLogging (com.hccake.ballcat.common.log.operation.annotation.UpdateOperationLogging)1 BaseResultCode (com.hccake.ballcat.common.model.result.BaseResultCode)1 R (com.hccake.ballcat.common.model.result.R)1 UserAttributeNameConstants (com.hccake.ballcat.common.security.constant.UserAttributeNameConstants)1 User (com.hccake.ballcat.common.security.userdetails.User)1 SecurityUtils (com.hccake.ballcat.common.security.util.SecurityUtils)1 SysMenuConverter (com.hccake.ballcat.system.converter.SysMenuConverter)1 SysMenuType (com.hccake.ballcat.system.enums.SysMenuType)1 SysMenuCreateDTO (com.hccake.ballcat.system.model.dto.SysMenuCreateDTO)1 SysMenuUpdateDTO (com.hccake.ballcat.system.model.dto.SysMenuUpdateDTO)1 UserInfoDTO (com.hccake.ballcat.system.model.dto.UserInfoDTO)1 SysRole (com.hccake.ballcat.system.model.entity.SysRole)1