Search in sources :

Example 1 with PermissionEntity

use of com.hb0730.boot.admin.project.system.permission.model.entity.PermissionEntity in project boot-admin by hb0730.

the class MenuEventListener method findMenuByUser.

private List<TreeMenuDTO> findMenuByUser(UserDTO user) {
    if (null == user) {
        return null;
    }
    if (user.getIsAdmin() == 1) {
        MenuParams params = new MenuParams();
        params.setSortType(SortTypeEnum.ASC.getValue());
        params.setSortColumn(Collections.singletonList(MenuEntity.SORT));
        QueryWrapper<MenuEntity> query = QueryWrapperUtils.getQuery(params);
        List<MenuEntity> entities = menuService.list(query);
        return BeanUtil.copyToList(entities, TreeMenuDTO.class);
    }
    Collection<Long> permissionIds = user.getPermissionIds();
    if (CollectionUtils.isEmpty(permissionIds)) {
        return Lists.newArrayList();
    }
    LambdaQueryWrapper<PermissionEntity> queryWrapper = Wrappers.lambdaQuery(PermissionEntity.class).in(PermissionEntity::getId, permissionIds).select(PermissionEntity::getMenuId, PermissionEntity::getPermission);
    // 权限
    List<PermissionEntity> permissionEntities = ((UserInfoServiceImpl) userInfoService.getThis()).getPermissionService().list(queryWrapper);
    if (CollectionUtils.isEmpty(permissionEntities)) {
        return Lists.newArrayList();
    }
    Map<Long, List<String>> permissionMapping = permissionEntities.stream().collect(Collectors.groupingBy(PermissionEntity::getMenuId, Collectors.mapping(PermissionEntity::getPermission, Collectors.toList())));
    // 菜单
    List<Long> menuIds = permissionEntities.parallelStream().map(PermissionEntity::getMenuId).collect(Collectors.toList());
    Set<MenuEntity> entities = Sets.newHashSet();
    for (Long menuId : menuIds) {
        entities.addAll(menuService.getSuperior(menuId, Lists.newArrayList()));
    }
    List<MenuEntity> menuEntities = entities.stream().sorted(Comparator.comparing(MenuEntity::getSort)).collect(Collectors.toList());
    List<TreeMenuDTO> treeMenu = BeanUtil.copyToList(menuEntities, TreeMenuDTO.class);
    for (TreeMenuDTO menu : treeMenu) {
        menu.setAuthority(permissionMapping.get(menu.getId()));
    }
    return treeMenu;
}
Also used : MenuParams(com.hb0730.boot.admin.project.system.menu.model.query.MenuParams) MenuEntity(com.hb0730.boot.admin.project.system.menu.model.entity.MenuEntity) TreeMenuDTO(com.hb0730.boot.admin.project.system.menu.model.dto.TreeMenuDTO) List(java.util.List) PermissionEntity(com.hb0730.boot.admin.project.system.permission.model.entity.PermissionEntity)

Example 2 with PermissionEntity

use of com.hb0730.boot.admin.project.system.permission.model.entity.PermissionEntity in project boot-admin by hb0730.

the class RolePermissionServiceImpl method savePermissionIdByRoleId.

@Override
@Transactional(rollbackFor = Exception.class)
public boolean savePermissionIdByRoleId(@Nonnull Long id, @Nonnull Collection<Long> permissionIds) {
    Assert.notNull(id, "角色id为空");
    Assert.notEmpty(permissionIds, "权限id为空");
    LambdaQueryWrapper<PermissionEntity> queryWrapper = Wrappers.lambdaQuery(PermissionEntity.class).in(PermissionEntity::getId, permissionIds).select(PermissionEntity::getId);
    List<PermissionEntity> entities = permissionMapper.selectList(queryWrapper);
    if (CollectionUtils.isEmpty(entities)) {
        throw new BusinessException("请传入正确的权限id");
    }
    Set<Long> ids = entities.parallelStream().map(PermissionEntity::getId).collect(Collectors.toSet());
    List<RolePermissionEntity> rolePermissionEntities = new ArrayList<>(ids.size());
    for (Long permissionId : ids) {
        RolePermissionEntity entity = new RolePermissionEntity();
        entity.setPermissionId(permissionId);
        entity.setRoleId(id);
        rolePermissionEntities.add(entity);
    }
    return super.saveBatch(rolePermissionEntities);
}
Also used : BusinessException(com.hb0730.boot.admin.exceptions.BusinessException) PermissionEntity(com.hb0730.boot.admin.project.system.permission.model.entity.PermissionEntity) RolePermissionEntity(com.hb0730.boot.admin.project.system.role.model.entity.RolePermissionEntity) RolePermissionEntity(com.hb0730.boot.admin.project.system.role.model.entity.RolePermissionEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with PermissionEntity

use of com.hb0730.boot.admin.project.system.permission.model.entity.PermissionEntity in project boot-admin by hb0730.

the class UserInfoServiceImpl method loadUserByUsername.

@Override
public UserDTO loadUserByUsername(String username) {
    UserAccountEntity accountEntity = accountService.findUserAccountByUsername(username);
    if (null == accountEntity) {
        return null;
    }
    Long userId = accountEntity.getUserId();
    UserInfoEntity entity = super.getById(userId);
    UserDTO user = BeanUtil.toBean(entity, UserDTO.class);
    assert user != null;
    user.setUsername(accountEntity.getUsername());
    user.setPassword(accountEntity.getPassword());
    // 用户角色
    Collection<Long> roleIds = userRoleService.findRoleByUserId(userId);
    if (CollectionUtils.isEmpty(roleIds)) {
        return user;
    }
    List<RoleEntity> roles = roleService.findEnabledRoleByIds(roleIds);
    if (CollectionUtils.isEmpty(roles)) {
        return user;
    }
    Map<Long, String> roleMap = roles.parallelStream().collect(Collectors.toMap(RoleEntity::getId, RoleEntity::getCode));
    user.setRoleIds(roleMap.keySet());
    user.setRole(roleMap.values());
    // 权限
    Map<Long, List<Long>> permission = rolePermissionService.findPermissionIdByRoleId(roleIds);
    if (CollectionUtils.isEmpty(permission)) {
        return user;
    }
    Set<Long> permissionIds = permission.values().stream().flatMap(List::stream).collect(Collectors.toSet());
    List<PermissionEntity> permissionEntities = permissionService.findEnabledPermissionByIds(permissionIds);
    if (CollectionUtils.isEmpty(permissionEntities)) {
        return user;
    }
    Map<Long, String> permissionMap = permissionEntities.parallelStream().collect(Collectors.toMap(PermissionEntity::getId, PermissionEntity::getPermission));
    user.setPermission(permissionMap.values());
    user.setPermissionIds(permissionMap.keySet());
    // 岗位
    List<Long> postIds = userPostService.findPostIdByUserIds(Collections.singletonList(userId));
    user.setPostIds(postIds);
    return user;
}
Also used : UserAccountEntity(com.hb0730.boot.admin.project.system.user.model.entity.UserAccountEntity) UserDTO(com.hb0730.boot.admin.project.system.user.model.dto.UserDTO) UserInfoEntity(com.hb0730.boot.admin.project.system.user.model.entity.UserInfoEntity) UserRoleEntity(com.hb0730.boot.admin.project.system.user.model.entity.UserRoleEntity) RoleEntity(com.hb0730.boot.admin.project.system.role.model.entity.RoleEntity) List(java.util.List) PermissionEntity(com.hb0730.boot.admin.project.system.permission.model.entity.PermissionEntity)

Example 4 with PermissionEntity

use of com.hb0730.boot.admin.project.system.permission.model.entity.PermissionEntity in project boot-admin by hb0730.

the class MenuServiceImpl method queryMenuPermissionTree.

@Override
public List<MenuPermissionVO> queryMenuPermissionTree() {
    List<PermissionEntity> permissionEntities = permissionMapper.selectList(null);
    List<MenuEntity> entities = super.list();
    List<MenuPermissionVO> trees = new ArrayList<>();
    for (MenuEntity menuEntity : entities) {
        MenuPermissionVO menuPermission = new MenuPermissionVO();
        if (menuEntity.getParentId() == null || menuEntity.getParentId() == -1) {
            menuPermission.setId(menuEntity.getId());
            menuPermission.setName(menuEntity.getTitle());
            menuPermission.setIsPermission(false);
            trees.add(menuPermission);
        }
        for (MenuEntity entity : entities) {
            if (menuEntity.getId().equals(entity.getParentId())) {
                if (menuPermission.getChildren() == null) {
                    menuPermission.setChildren(new ArrayList<>());
                }
                MenuPermissionVO menuPermissionInfo = new MenuPermissionVO();
                menuPermissionInfo.setName(entity.getTitle());
                menuPermissionInfo.setId(entity.getId());
                menuPermissionInfo.setIsPermission(false);
                menuPermission.getChildren().add(menuPermissionInfo);
                // 权限
                List<PermissionEntity> permissionList = permissionEntities.stream().filter(e -> e.getMenuId().equals(entity.getId())).collect(Collectors.toList());
                for (PermissionEntity permissionEntity : permissionList) {
                    MenuPermissionVO permission = new MenuPermissionVO();
                    permission.setIsPermission(true);
                    permission.setId(permissionEntity.getId());
                    permission.setName(permissionEntity.getName());
                    if (menuPermissionInfo.getChildren() == null) {
                        menuPermissionInfo.setChildren(new ArrayList<>());
                    }
                    menuPermissionInfo.getChildren().add(permission);
                }
            }
        }
    }
    return trees;
}
Also used : UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) SuperBaseServiceImpl(com.hb0730.boot.admin.domain.service.impl.SuperBaseServiceImpl) SneakyThrows(lombok.SneakyThrows) VueMenuVO(com.hb0730.boot.admin.project.system.menu.model.vo.VueMenuVO) RequiredArgsConstructor(lombok.RequiredArgsConstructor) SortTypeEnum(com.hb0730.boot.admin.commons.enums.SortTypeEnum) SecurityUtils(com.hb0730.boot.admin.security.utils.SecurityUtils) BeanUtil(cn.hutool.core.bean.BeanUtil) HashOperations(org.springframework.data.redis.core.HashOperations) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) TreeMenuDTO(com.hb0730.boot.admin.project.system.menu.model.dto.TreeMenuDTO) IPermissionMapper(com.hb0730.boot.admin.project.system.permission.mapper.IPermissionMapper) Lists(com.google.common.collect.Lists) MenuMetaVO(com.hb0730.boot.admin.project.system.menu.model.vo.MenuMetaVO) Service(org.springframework.stereotype.Service) RedisTemplate(org.springframework.data.redis.core.RedisTemplate) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) EnabledEnum(com.hb0730.boot.admin.commons.enums.EnabledEnum) LinkedList(java.util.LinkedList) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) MenuDTO(com.hb0730.boot.admin.project.system.menu.model.dto.MenuDTO) PermissionEntity(com.hb0730.boot.admin.project.system.permission.model.entity.PermissionEntity) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) Wrappers(com.baomidou.mybatisplus.core.toolkit.Wrappers) LoginException(com.hb0730.boot.admin.exceptions.LoginException) CollectionUtil(cn.hutool.core.collection.CollectionUtil) MenuPermissionVO(com.hb0730.boot.admin.project.system.menu.model.vo.MenuPermissionVO) RedisConstant(com.hb0730.boot.admin.commons.constant.RedisConstant) Set(java.util.Set) ResponseStatusEnum(com.hb0730.boot.admin.commons.enums.ResponseStatusEnum) Collectors(java.util.stream.Collectors) ROOT_PATH(com.hb0730.commons.lang.constants.PathConst.ROOT_PATH) Sets(com.google.common.collect.Sets) Serializable(java.io.Serializable) StrUtil(cn.hutool.core.util.StrUtil) List(java.util.List) User(com.hb0730.boot.admin.security.model.User) IMenuService(com.hb0730.boot.admin.project.system.menu.service.IMenuService) MenuEntity(com.hb0730.boot.admin.project.system.menu.model.entity.MenuEntity) MenuParams(com.hb0730.boot.admin.project.system.menu.model.query.MenuParams) Collections(java.util.Collections) MenuEvent(com.hb0730.boot.admin.event.menu.MenuEvent) IMenuMapper(com.hb0730.boot.admin.project.system.menu.mapper.IMenuMapper) Transactional(org.springframework.transaction.annotation.Transactional) Assert(org.springframework.util.Assert) MenuEntity(com.hb0730.boot.admin.project.system.menu.model.entity.MenuEntity) MenuPermissionVO(com.hb0730.boot.admin.project.system.menu.model.vo.MenuPermissionVO) ArrayList(java.util.ArrayList) PermissionEntity(com.hb0730.boot.admin.project.system.permission.model.entity.PermissionEntity)

Aggregations

PermissionEntity (com.hb0730.boot.admin.project.system.permission.model.entity.PermissionEntity)4 List (java.util.List)3 TreeMenuDTO (com.hb0730.boot.admin.project.system.menu.model.dto.TreeMenuDTO)2 MenuEntity (com.hb0730.boot.admin.project.system.menu.model.entity.MenuEntity)2 MenuParams (com.hb0730.boot.admin.project.system.menu.model.query.MenuParams)2 BeanUtil (cn.hutool.core.bean.BeanUtil)1 CollectionUtil (cn.hutool.core.collection.CollectionUtil)1 StrUtil (cn.hutool.core.util.StrUtil)1 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)1 UpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper)1 Wrappers (com.baomidou.mybatisplus.core.toolkit.Wrappers)1 Lists (com.google.common.collect.Lists)1 Sets (com.google.common.collect.Sets)1 RedisConstant (com.hb0730.boot.admin.commons.constant.RedisConstant)1 EnabledEnum (com.hb0730.boot.admin.commons.enums.EnabledEnum)1 ResponseStatusEnum (com.hb0730.boot.admin.commons.enums.ResponseStatusEnum)1 SortTypeEnum (com.hb0730.boot.admin.commons.enums.SortTypeEnum)1 SuperBaseServiceImpl (com.hb0730.boot.admin.domain.service.impl.SuperBaseServiceImpl)1 MenuEvent (com.hb0730.boot.admin.event.menu.MenuEvent)1 BusinessException (com.hb0730.boot.admin.exceptions.BusinessException)1