use of cn.edu.sdu.qd.oj.common.entity.UserSessionDTO in project sduoj-server by SDUOJ.
the class UserController method register.
@PostMapping("/register")
@ResponseBody
public ResponseResult<UserSessionDTO> register(HttpServletResponse response, @Valid @RequestBody UserDTO userDTO, @RealIp String ipv4, @RequestHeader("user-agent") String userAgent) {
userDTO.setRoles(null);
UserSessionDTO userSessionDTO = this.userService.register(userDTO, ipv4, userAgent);
writeSessionToHeader(response, userSessionDTO);
return ResponseResult.ok(userSessionDTO);
}
use of cn.edu.sdu.qd.oj.common.entity.UserSessionDTO in project sduoj-server by SDUOJ.
the class ContestService method queryRank.
/**
* @Description 查询榜单数据,并做越权、脱敏、榜单冻结
*/
@Cacheable(value = ContestCacheTypeManager.RANK, key = "#contestId+'-'+#userSessionDTO.userId")
public List<ContestRankDTO> queryRank(long contestId, UserSessionDTO userSessionDTO) throws InternalApiException {
// 查比赛
ContestDO contestDO = queryContestAndValidate(contestId, userSessionDTO.getUserId());
// 提出 contestFeature
ContestFeatureDTO contestFeatureDTO = ContestConvertUtils.featuresTo(contestDO.getFeatures());
ContestFeatureDTO.InfoOpenness infoOpenness = contestDO.getGmtEnd().after(new Date()) ? contestFeatureDTO.getContestRunning() : contestFeatureDTO.getContestEnd();
ContestModeEnum contestMode = ContestModeEnum.of(contestFeatureDTO.getMode());
// 查询 raw 的榜单数据
List<ContestRankDTO> contestRankDTOList = queryRawRankData(contestDO);
// 置挂星参赛者
Set<Long> unofficialParticipantIds = new HashSet<>(ContestConvertUtils.participantsToUserIdList(contestDO.getUnofficialParticipants()));
contestRankDTOList.stream().filter(o -> unofficialParticipantIds.contains(o.getUserId())).forEach(o -> o.setOfficial(false));
// 比赛管理员 直接获取所有榜单,无封榜无脱敏
if (contestCommonService.isContestManager(contestDO, userSessionDTO)) {
return contestRankDTOList;
}
// 禁止显示榜单,脱敏其他人的提交信息
if (infoOpenness.getDisplayRank() == 0) {
contestRankDTOList = contestRankDTOList.stream().filter(o -> userSessionDTO.userIdEquals(o.getUserId())).collect(Collectors.toList());
}
// 禁止显示分数
if (infoOpenness.getDisplayJudgeScore() == 0) {
contestRankDTOList.stream().map(ContestRankDTO::getSubmissions).forEach(o -> o.forEach(s -> s.setJudgeScore(0)));
}
// 榜单冻结
if (contestFeatureDTO.getFrozenTime() != 0) {
Date frozenTime = new Date(contestDO.getGmtEnd().getTime() - contestFeatureDTO.getFrozenTime() * 60000);
contestRankDTOList.forEach(o -> {
o.frozenRank(frozenTime);
o.toComputeProblemResults(contestMode);
});
}
// 比赛进行时,需要进行 submissions 转 problemResults,将提交中的蕴涵的信息脱掉
if (contestDO.getGmtEnd().after(new Date())) {
contestRankDTOList.forEach(o -> o.toComputeProblemResults(contestMode));
}
return contestRankDTOList;
}
use of cn.edu.sdu.qd.oj.common.entity.UserSessionDTO in project sduoj-server by SDUOJ.
the class LoginFilter method filter.
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();
String requestUrl = request.getPath().toString();
String realIp = FilterUtils.getRealIp(request);
log.info("Filter From: {}\tUrl: {}\tParams: {}", realIp, requestUrl, request.getQueryParams());
// 取 token 并解密
UserSessionDTO userSessionDTO = Optional.of(exchange).map(ServerWebExchange::getSession).map(Mono::block).map(WebSession::getAttributes).map(map -> map.get(UserSessionDTO.HEADER_KEY)).map(o -> (String) o).map(o -> JSON.parseObject(o, UserSessionDTO.class)).orElse(null);
// 无 session,非 allowUrl
boolean isAllowPath = isAllowPath(requestUrl);
if (userSessionDTO == null && !isAllowPath) {
return FilterUtils.returnWithStatus(exchange, HttpStatus.UNAUTHORIZED, " 你的账号没有该权限或未登录! ");
}
// 鉴权
if (userSessionDTO != null) {
List<String> urlRoles = NonExceptionOptional.ofNullable(() -> permissionClient.urlToRoles(requestUrl.replace("/api", ""))).orElse(Lists.newArrayList());
List<String> roles = NonExceptionOptional.ofNullable(() -> userClient.queryRolesById(userSessionDTO.getUserId())).orElse(Lists.newArrayList());
if (!urlRoles.contains(PermissionEnum.ALL.name) && Collections.disjoint(roles, urlRoles) && !isAllowPath) {
log.warn("have not permission {} {}", userSessionDTO, requestUrl);
return FilterUtils.returnWithStatus(exchange, HttpStatus.UNAUTHORIZED, String.format("This User has no permission on '%s'", requestUrl));
}
// 装饰器 修改 getHeaders 方法
ServerHttpRequestDecorator decorator = new ServerHttpRequestDecorator(exchange.getRequest()) {
@Override
public HttpHeaders getHeaders() {
MultiValueMap<String, String> multiValueMap = CollectionUtils.toMultiValueMap(new LinkedCaseInsensitiveMap(8, Locale.ENGLISH));
super.getHeaders().forEach((key, value) -> multiValueMap.put(key, value));
// multiValueMap.remove("cookie"); // 在此处已解码 token, 故不下传省流量, 如果后续有多值 cookie 此处需要修改
multiValueMap.remove(UserSessionDTO.HEADER_KEY);
multiValueMap.add(UserSessionDTO.HEADER_KEY, JSON.toJSONString(userSessionDTO));
multiValueMap.remove(UserSessionDTO.HEADER_KEY_USERID);
multiValueMap.add(UserSessionDTO.HEADER_KEY_USERID, userSessionDTO.getUserId().toString());
// }
return new HttpHeaders(multiValueMap);
}
};
return chain.filter(exchange.mutate().request(decorator).build()).then(thenHandleSession(exchange));
}
return chain.filter(exchange).then(thenHandleSession(exchange));
}
use of cn.edu.sdu.qd.oj.common.entity.UserSessionDTO in project sduoj-server by SDUOJ.
the class UserController method thirdPartyBinding.
@PostMapping("/thirdPartyBinding")
@ResponseBody
public ResponseResult<UserSessionDTO> thirdPartyBinding(@RequestBody UserThirdPartyBindingReqDTO reqDTO, HttpServletResponse response, @RealIp String ipv4, @RequestHeader("user-agent") String userAgent) {
UserSessionDTO userSessionDTO = userService.thirdPartyBinding(reqDTO, ipv4, userAgent);
writeSessionToHeader(response, userSessionDTO);
return ResponseResult.ok(userSessionDTO);
}
use of cn.edu.sdu.qd.oj.common.entity.UserSessionDTO in project sduoj-server by SDUOJ.
the class UserController method thirdPartyRegister.
@PostMapping("/thirdPartyRegister")
@ResponseBody
public ResponseResult<UserSessionDTO> thirdPartyRegister(@RequestBody UserThirdPartyRegisterReqDTO reqDTO, HttpServletResponse response, @RealIp String ipv4, @RequestHeader("user-agent") String userAgent) {
UserSessionDTO userSessionDTO = userService.thirdPartyRegister(reqDTO, ipv4, userAgent);
writeSessionToHeader(response, userSessionDTO);
return ResponseResult.ok(userSessionDTO);
}
Aggregations