Search in sources :

Example 1 with User

use of com.hb0730.boot.admin.security.model.User in project boot-admin by hb0730.

the class MyBatisPlusMetaObjectHandler method insertFill.

/**
 * 新增填充
 *
 * @param metaObject {@link MetaObject}
 */
@Override
public void insertFill(MetaObject metaObject) {
    User currentUser = SecurityUtils.getCurrentUser();
    if (null != currentUser) {
        this.fillStrategy(metaObject, "createUserId", currentUser.getId());
    }
    this.fillStrategy(metaObject, "createTime", LocalDateTime.now());
    this.fillStrategy(metaObject, "version", 1);
}
Also used : User(com.hb0730.boot.admin.security.model.User)

Example 2 with User

use of com.hb0730.boot.admin.security.model.User in project boot-admin by hb0730.

the class MyBatisPlusMetaObjectHandler method updateFill.

/**
 * 修改填充
 *
 * @param metaObject {@link MetaObject}
 */
@Override
public void updateFill(MetaObject metaObject) {
    User currentUser = SecurityUtils.getCurrentUser();
    if (null != currentUser) {
        this.fillStrategy(metaObject, "updateUserId", currentUser.getId());
    }
    this.fillStrategy(metaObject, "updateTime", LocalDateTime.now());
}
Also used : User(com.hb0730.boot.admin.security.model.User)

Example 3 with User

use of com.hb0730.boot.admin.security.model.User in project boot-admin by hb0730.

the class LogAspectj method handleLog.

/**
 * 日志处理
 */
void handleLog(final JoinPoint joinPoint, Object jsonResult, final Exception e) {
    try {
        Log log = getAnnotationLog(joinPoint);
        if (check(joinPoint, log)) {
            return;
        }
        // 当前用户
        OperLogEntity entity = new OperLogEntity();
        // 状态
        entity.setStatus(StatusEnum.SUCCESS.getValue());
        // 操作类型
        entity.setOperType(log.businessType().getValue());
        // 请求用户
        User currentUser = SecurityUtils.getCurrentUser();
        if (null != currentUser) {
            entity.setUsername(currentUser.getUsername());
            entity.setCreateUserId(currentUser.getId());
            entity.setCreateTime(LocalDateTimeUtils.now());
        }
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        if (null == attributes) {
            return;
        }
        // 请求ip
        String ip = "";
        // 请求地址
        String requestUrl = "";
        // 请求方式
        String requestMethod = "";
        if (attributes instanceof ServletRequestAttributes) {
            HttpServletRequest request = ((ServletRequestAttributes) attributes).getRequest();
            ip = ServletUtil.getClientIP(request);
            // 请求地址
            requestUrl = request.getRequestURI();
            requestMethod = request.getMethod();
        }
        entity.setOperIp(ip);
        // 描述
        // 类描述
        String controllerDescription = "";
        ClassDescribe classDescribe = joinPoint.getTarget().getClass().getAnnotation(ClassDescribe.class);
        if (null != classDescribe) {
            controllerDescription = classDescribe.value();
        }
        // 方法描述
        String controllerMethodDescription = getDescribe(log);
        if (log.controllerApiValue()) {
            entity.setDescription(StringUtils.join(controllerDescription, controllerMethodDescription));
        } else {
            entity.setDescription(controllerMethodDescription);
        }
        // 请求地址
        entity.setRequestUrl(requestUrl);
        // 请求方式
        entity.setRequestMethod(requestMethod);
        // 操作方法
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        entity.setOperMethod(className + "." + methodName);
        // 请求参数
        if (log.request()) {
            HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
            String requestParamsValue = getRequestValue(joinPoint, request, log.paramsName());
            entity.setRequestParams(requestParamsValue);
        }
        // 返回参数
        if (log.response()) {
            String result = JsonUtils.objectToJson(null == jsonResult ? "" : jsonResult);
            entity.setRequestResult(result);
        }
        if (null != e) {
            if (log.requestByError()) {
                String result = JsonUtils.objectToJson(null == jsonResult ? "" : jsonResult);
                entity.setRequestResult(result);
                String message = ExceptionUtils.getExceptionMessage(e);
                entity.setErrorMessage(StringUtils.substring(message, 0, 2000));
                entity.setStatus(StatusEnum.FAIL.getValue());
            }
        }
        AsyncManager.me().execute(AsyncFactory.recordOperLog(entity));
    } catch (Exception e1) {
        e1.printStackTrace();
        if (null != e) {
            e.printStackTrace();
        }
        // 记录本地异常日志
        LOGGER.error("==前置通知异常==");
        LOGGER.error("异常信息:{}", e1.getMessage());
        if (null != e) {
            throw new BusinessException(e.getMessage());
        } else {
            throw new BusinessException(e1.getMessage());
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) BusinessException(com.hb0730.boot.admin.exceptions.BusinessException) User(com.hb0730.boot.admin.security.model.User) Log(com.hb0730.boot.admin.annotation.Log) OperLogEntity(com.hb0730.boot.admin.project.monitor.operation.model.entity.OperLogEntity) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ClassDescribe(com.hb0730.boot.admin.annotation.ClassDescribe) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) BusinessException(com.hb0730.boot.admin.exceptions.BusinessException)

Example 4 with User

use of com.hb0730.boot.admin.security.model.User 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 5 with User

use of com.hb0730.boot.admin.security.model.User in project boot-admin by hb0730.

the class MenuServiceImpl method updateCurrentMenu.

@Override
public boolean updateCurrentMenu() {
    User currentUser = SecurityUtils.getCurrentUser();
    if (null == currentUser) {
        throw new LoginException(ResponseStatusEnum.USE_LOGIN_ERROR, "当前用户未登录,请登录后重试");
    }
    eventPublisher.publishEvent(new MenuEvent(this, currentUser.getId()));
    return true;
}
Also used : User(com.hb0730.boot.admin.security.model.User) LoginException(com.hb0730.boot.admin.exceptions.LoginException) MenuEvent(com.hb0730.boot.admin.event.menu.MenuEvent)

Aggregations

User (com.hb0730.boot.admin.security.model.User)13 List (java.util.List)4 MenuEvent (com.hb0730.boot.admin.event.menu.MenuEvent)3 LoginException (com.hb0730.boot.admin.exceptions.LoginException)3 TreeMenuDTO (com.hb0730.boot.admin.project.system.menu.model.dto.TreeMenuDTO)3 UserDTO (com.hb0730.boot.admin.project.system.user.model.dto.UserDTO)3 UserDetails (org.springframework.security.core.userdetails.UserDetails)3 PermissionEntity (com.hb0730.boot.admin.project.system.permission.model.entity.PermissionEntity)2 LoginUser (com.hb0730.boot.admin.security.model.LoginUser)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 SneakyThrows (lombok.SneakyThrows)2 Test (org.junit.Test)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 MvcResult (org.springframework.test.web.servlet.MvcResult)2 RequestAttributes (org.springframework.web.context.request.RequestAttributes)2 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)2 UpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper)1 ClassDescribe (com.hb0730.boot.admin.annotation.ClassDescribe)1 Log (com.hb0730.boot.admin.annotation.Log)1