Skip to content

Commit

Permalink
add sys switch and support gym pdf
Browse files Browse the repository at this point in the history
  • Loading branch information
HimitZH committed May 10, 2022
1 parent 4a16be6 commit 6a872cd
Show file tree
Hide file tree
Showing 46 changed files with 1,529 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import top.hcode.hoj.common.exception.*;
import top.hcode.hoj.common.result.CommonResult;
import top.hcode.hoj.common.result.ResultStatus;
import top.hcode.hoj.exception.AccessException;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -109,6 +110,14 @@ public CommonResult<Void> handleShiroException(ShiroException e,
return CommonResult.errorResponse("对不起,您无权限进行此操作,请先登录进行授权认证", ResultStatus.FORBIDDEN);
}

/**
* 403 -FORBIDDEN 处理访问api能力被禁止的异常
*/
@ResponseStatus(HttpStatus.FORBIDDEN)
@ExceptionHandler(value = AccessException.class)
public CommonResult<Void> handleAccessException(AccessException e) {
return CommonResult.errorResponse(e.getMessage(), ResultStatus.FORBIDDEN);
}

/**
* 400 - Bad Request 处理Assert的异常 断言的异常!
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package top.hcode.hoj.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @Author Himit_ZH
* @Date 2022/5/9
*/
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface HOJAccess {
HOJAccessEnum[] value() default {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package top.hcode.hoj.annotation;

/**
* @Author Himit_ZH
* @Date 2022/5/9
*/
public enum HOJAccessEnum {
/**
* 公共讨论区
*/
PUBLIC_DISCUSSION,

/**
* 团队讨论区
*/
GROUP_DISCUSSION,

/**
* 比赛评论
*/
CONTEST_COMMENT,

/**
* 公共评测
*/
PUBLIC_JUDGE,

/**
* 团队评测
*/
GROUP_JUDGE,

/**
* 比赛评测
*/
CONTEST_JUDGE
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ public class StartupRunner implements CommandLineRunner {
@Value("${SPOJ_ACCOUNT_PASSWORD_LIST:}")
private List<String> spojPasswordList;

@Value("${FORCED_UPDATE_REMOTE_JUDGE_ACCOUNT:false}")
private Boolean forcedUpdateRemoteJudgeAccount;

@Value("${spring.profiles.active}")
private String profile;

Expand Down Expand Up @@ -182,20 +185,65 @@ public void run(String... args) throws Exception {
configVo.setEmailPassword(emailPassword);
}

configVo.setHduUsernameList(hduUsernameList);
configVo.setHduPasswordList(hduPasswordList);
if (CollectionUtils.isEmpty(configVo.getHduUsernameList()) || forcedUpdateRemoteJudgeAccount) {
configVo.setHduUsernameList(hduUsernameList);
}else {
hduUsernameList = configVo.getHduUsernameList();
}

if (CollectionUtils.isEmpty(configVo.getHduPasswordList()) || forcedUpdateRemoteJudgeAccount) {
configVo.setHduPasswordList(hduPasswordList);
}else {
hduPasswordList = configVo.getHduUsernameList();
}

if (CollectionUtils.isEmpty(configVo.getCfUsernameList()) || forcedUpdateRemoteJudgeAccount) {
configVo.setCfUsernameList(cfUsernameList);
}else {
cfUsernameList = configVo.getCfUsernameList();
}

if (CollectionUtils.isEmpty(configVo.getCfPasswordList()) || forcedUpdateRemoteJudgeAccount) {
configVo.setCfUsernameList(cfPasswordList);
}else {
cfPasswordList = configVo.getCfPasswordList();
}

if (CollectionUtils.isEmpty(configVo.getPojUsernameList()) || forcedUpdateRemoteJudgeAccount) {
configVo.setPojUsernameList(pojUsernameList);
}else {
pojUsernameList = configVo.getPojUsernameList();
}

if (CollectionUtils.isEmpty(configVo.getPojPasswordList()) || forcedUpdateRemoteJudgeAccount) {
configVo.setPojPasswordList(pojPasswordList);
}else {
pojPasswordList = configVo.getPojPasswordList();
}

configVo.setCfUsernameList(cfUsernameList);
configVo.setCfPasswordList(cfPasswordList);
if (CollectionUtils.isEmpty(configVo.getAtcoderUsernameList()) || forcedUpdateRemoteJudgeAccount) {
configVo.setAtcoderUsernameList(atcoderUsernameList);
}else {
atcoderUsernameList = configVo.getAtcoderUsernameList();
}

configVo.setPojUsernameList(pojUsernameList);
configVo.setPojPasswordList(pojPasswordList);
if (CollectionUtils.isEmpty(configVo.getAtcoderPasswordList()) || forcedUpdateRemoteJudgeAccount) {
configVo.setAtcoderPasswordList(atcoderPasswordList);
}else {
atcoderPasswordList = configVo.getAtcoderPasswordList();
}

configVo.setAtcoderUsernameList(atcoderUsernameList);
configVo.setAtcoderPasswordList(atcoderUsernameList);
if (CollectionUtils.isEmpty(configVo.getSpojUsernameList()) || forcedUpdateRemoteJudgeAccount) {
configVo.setSpojUsernameList(spojUsernameList);
}else {
spojUsernameList = configVo.getSpojUsernameList();
}

configVo.setSpojUsernameList(spojUsernameList);
configVo.setSpojPasswordList(spojPasswordList);
if (CollectionUtils.isEmpty(configVo.getSpojPasswordList()) || forcedUpdateRemoteJudgeAccount) {
configVo.setSpojPasswordList(spojPasswordList);
}else {
spojPasswordList = configVo.getSpojPasswordList();
}

configManager.sendNewConfigToNacos();

Expand Down Expand Up @@ -230,7 +278,7 @@ private void addRemoteJudgeAccountToMySQL(String oj, List<String> usernameList,


if (CollectionUtils.isEmpty(usernameList) || CollectionUtils.isEmpty(passwordList) || usernameList.size() != passwordList.size()) {
log.error("[{}]: There is no account or password configured for remote judge, " +
log.error("[Init System Config] [{}]: There is no account or password configured for remote judge, " +
"username list:{}, password list:{}", oj, Arrays.toString(usernameList.toArray()),
Arrays.toString(passwordList.toArray()));
}
Expand All @@ -250,7 +298,7 @@ private void addRemoteJudgeAccountToMySQL(String oj, List<String> usernameList,
if (remoteAccountList.size() > 0) {
boolean addOk = remoteJudgeAccountEntityService.saveOrUpdateBatch(remoteAccountList);
if (!addOk) {
log.error("Remote judge initialization failed. Failed to add account for: [{}]. Please check the configuration file and restart!", oj);
log.error("[Init System Config] Remote judge initialization failed. Failed to add account for: [{}]. Please check the configuration file and restart!", oj);
}
}
}
Expand All @@ -269,7 +317,7 @@ private void upsertHOJLanguage(String... languageList) {
Language newLanguage = buildHOJLanguage(language);
boolean isOk = languageEntityService.save(newLanguage);
if (!isOk) {
log.error("[HOJ] Failed to add new language [{}]! Please check whether the language table corresponding to the database has the language!", language);
log.error("[Init System Config] [HOJ] Failed to add new language [{}]! Please check whether the language table corresponding to the database has the language!", language);
}
}
}
Expand Down Expand Up @@ -304,7 +352,7 @@ private void checkRemoteOJLanguage(Constants.RemoteOJ... remoteOJList) {
List<Language> languageList = new LanguageContext(remoteOJ).buildLanguageList();
boolean isOk = languageEntityService.saveBatch(languageList);
if (!isOk) {
log.error("[{}] Failed to initialize language list! Please check whether the language table corresponding to the database has the OJ language!", remoteOJ.getName());
log.error("[Init System Config] [{}] Failed to initialize language list! Please check whether the language table corresponding to the database has the OJ language!", remoteOJ.getName());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
package top.hcode.hoj.config;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import top.hcode.hoj.interceptor.AccessInterceptor;
import top.hcode.hoj.utils.Constants;

import java.io.File;

/**
* 解决跨域问题
* 解决跨域问题以及增加注解拦截类
*/
@Configuration
public class CorsConfig implements WebMvcConfigurer {
public class WebMvcConfig implements WebMvcConfigurer {

private static final String[] EXCLUDE_PATH_PATTERNS = new String[]{
"/api/admin/**", "/api/file/**", "/api/msg/**", "/api/public/**"
};

@Autowired
private AccessInterceptor accessInterceptor;

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
Expand All @@ -35,4 +46,11 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
"file:" + Constants.File.HOME_CAROUSEL_FOLDER.getPath() + File.separator,
"file:" + Constants.File.PROBLEM_FILE_FOLDER.getPath() + File.separator);
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(accessInterceptor)
.addPathPatterns("/api/**")
.excludePathPatterns(EXCLUDE_PATH_PATTERNS);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package top.hcode.hoj.controller.admin;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.hcode.hoj.common.result.CommonResult;
import top.hcode.hoj.pojo.dto.SwitchConfigDto;
import top.hcode.hoj.service.admin.system.ConfigService;

import javax.annotation.Resource;

/**
* @Author Himit_ZH
* @Date 2022/5/9
*/
@RestController
@RequestMapping("/api/admin/switch")
public class SwitchController {

@Resource
private ConfigService configService;

@RequiresPermissions("system_info_admin")
@RequestMapping("/info")
public CommonResult<SwitchConfigDto> getSwitchConfig() {

return configService.getSwitchConfig();
}

@RequiresPermissions("system_info_admin")
@PutMapping("/update")
public CommonResult<Void> setSwitchConfig(@RequestBody SwitchConfigDto config) {
return configService.setSwitchConfig(config);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package top.hcode.hoj.controller.group;

import top.hcode.hoj.common.result.CommonResult;
import top.hcode.hoj.pojo.entity.discussion.Discussion;
import top.hcode.hoj.service.group.discussion.GroupDiscussionService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.hcode.hoj.annotation.HOJAccess;
import top.hcode.hoj.annotation.HOJAccessEnum;
import top.hcode.hoj.common.result.CommonResult;
import top.hcode.hoj.pojo.entity.discussion.Discussion;
import top.hcode.hoj.service.group.discussion.GroupDiscussionService;

/**
* @Author: LengYun
Expand All @@ -16,6 +18,7 @@
@RestController
@RequiresAuthentication
@RequestMapping("/api/group")
@HOJAccess({HOJAccessEnum.GROUP_DISCUSSION})
public class GroupDiscussionController {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.hcode.hoj.annotation.HOJAccess;
import top.hcode.hoj.annotation.HOJAccessEnum;
import top.hcode.hoj.common.result.CommonResult;
import top.hcode.hoj.pojo.entity.problem.Category;
import top.hcode.hoj.pojo.entity.discussion.Discussion;
Expand All @@ -28,6 +30,7 @@ public class DiscussionController {


@GetMapping("/get-discussion-list")
@HOJAccess({HOJAccessEnum.PUBLIC_DISCUSSION})
public CommonResult<IPage<Discussion>> getDiscussionList(@RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit,
@RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage,
@RequestParam(value = "cid", required = false) Integer categoryId,
Expand All @@ -48,20 +51,23 @@ public CommonResult<DiscussionVo> getDiscussion(@RequestParam(value = "did", req
@PostMapping("/discussion")
@RequiresPermissions("discussion_add")
@RequiresAuthentication
@HOJAccess({HOJAccessEnum.PUBLIC_DISCUSSION})
public CommonResult<Void> addDiscussion(@RequestBody Discussion discussion) {
return discussionService.addDiscussion(discussion);
}

@PutMapping("/discussion")
@RequiresPermissions("discussion_edit")
@RequiresAuthentication
@HOJAccess({HOJAccessEnum.PUBLIC_DISCUSSION})
public CommonResult<Void> updateDiscussion(@RequestBody Discussion discussion) {
return discussionService.updateDiscussion(discussion);
}

@DeleteMapping("/discussion")
@RequiresPermissions("discussion_del")
@RequiresAuthentication
@HOJAccess({HOJAccessEnum.PUBLIC_DISCUSSION})
public CommonResult<Void> removeDiscussion(@RequestParam("did") Integer did) {
return discussionService.removeDiscussion(did);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package top.hcode.hoj.exception;

/**
* @Author Himit_ZH
* @Date 2022/5/9
*/
public class AccessException extends Exception{
public AccessException() {
super();
}

public AccessException(String message) {
super(message);
}

public AccessException(String message, Throwable cause) {
super(message, cause);
}

public AccessException(Throwable cause) {
super(cause);
}

protected AccessException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
Loading

0 comments on commit 6a872cd

Please sign in to comment.