optim cron check

This commit is contained in:
bwcx_jzy 2022-02-07 10:14:48 +08:00
parent f7e051bd27
commit 9a43877f99
No known key found for this signature in database
GPG Key ID: 5E48E9372088B9E5
6 changed files with 30 additions and 52 deletions

View File

@ -22,8 +22,11 @@
*/
package io.jpom.common;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.cron.pattern.CronPattern;
import io.jpom.common.interceptor.LoginInterceptor;
import io.jpom.common.interceptor.PermissionInterceptor;
import io.jpom.model.data.NodeModel;
import io.jpom.model.data.UserModel;
import io.jpom.service.node.NodeService;
@ -62,6 +65,25 @@ public abstract class BaseServerController extends BaseJpomController {
return nodeService.getByKey(nodeId);
}
/**
* 验证 cron 表达式, demo 账号不能开启 cron
*
* @param cron cron
* @return 原样返回
*/
protected String checkCron(String cron) {
if (StrUtil.isNotEmpty(cron)) {
UserModel user = getUser();
Assert.state(!user.isDemoUser(), PermissionInterceptor.DEMO_TIP);
try {
new CronPattern(cron);
} catch (Exception e) {
throw new IllegalArgumentException("cron 表达式格式不正确");
}
}
return ObjectUtil.defaultIfNull(cron, StrUtil.EMPTY);
}
@Override
public void resetInfo() {
USER_MODEL_THREAD_LOCAL.set(getUserModel());

View File

@ -29,7 +29,6 @@ import cn.hutool.core.lang.Tuple;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.cron.pattern.CronPattern;
import cn.jiangzeyin.common.JsonMessage;
import cn.jiangzeyin.common.validator.ValidatorConfig;
import cn.jiangzeyin.common.validator.ValidatorItem;
@ -40,14 +39,12 @@ import io.jpom.build.BuildExecuteService;
import io.jpom.build.BuildUtil;
import io.jpom.build.DockerYmlDsl;
import io.jpom.common.BaseServerController;
import io.jpom.common.interceptor.PermissionInterceptor;
import io.jpom.model.AfterOpt;
import io.jpom.model.BaseEnum;
import io.jpom.model.PageResultDto;
import io.jpom.model.data.BuildInfoModel;
import io.jpom.model.data.RepositoryModel;
import io.jpom.model.data.SshModel;
import io.jpom.model.data.UserModel;
import io.jpom.model.enums.BuildReleaseMethod;
import io.jpom.plugin.ClassFeature;
import io.jpom.plugin.Feature;
@ -196,16 +193,7 @@ public class BuildInfoController extends BaseServerController {
if (StrUtil.isNotEmpty(webhook)) {
Validator.validateMatchRegex(RegexPool.URL_HTTP, webhook, "WebHooks 地址不合法");
}
if (StrUtil.isNotEmpty(autoBuildCron)) {
UserModel user = getUser();
Assert.state(!user.isDemoUser(), PermissionInterceptor.DEMO_TIP);
try {
new CronPattern(autoBuildCron);
} catch (Exception e) {
throw new IllegalArgumentException("定时构建表达式格式不正确");
}
}
buildInfoModel.setAutoBuildCron(autoBuildCron);
buildInfoModel.setAutoBuildCron(this.checkCron(autoBuildCron));
buildInfoModel.setWebhook(webhook);
buildInfoModel.setRepositoryId(repositoryId);
buildInfoModel.setName(name);

View File

@ -23,13 +23,11 @@
package io.jpom.controller.monitor;
import cn.hutool.core.util.StrUtil;
import cn.hutool.cron.pattern.CronPattern;
import cn.jiangzeyin.common.JsonMessage;
import cn.jiangzeyin.common.validator.ValidatorItem;
import cn.jiangzeyin.common.validator.ValidatorRule;
import com.alibaba.fastjson.JSONArray;
import io.jpom.common.BaseServerController;
import io.jpom.common.interceptor.PermissionInterceptor;
import io.jpom.model.PageResultDto;
import io.jpom.model.data.MonitorModel;
import io.jpom.model.data.UserModel;
@ -122,15 +120,6 @@ public class MonitorListController extends BaseServerController {
@ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "监控名称不能为空") String name,
@ValidatorItem(msg = "请配置监控周期") String execCron,
String notifyUser) {
UserModel user = getUser();
Assert.state(!user.isDemoUser(), PermissionInterceptor.DEMO_TIP);
try {
new CronPattern(execCron);
} catch (Exception e) {
throw new IllegalArgumentException("定时构建表达式格式不正确");
}
String status = getParameter("status");
String autoRestart = getParameter("autoRestart");
@ -159,7 +148,7 @@ public class MonitorListController extends BaseServerController {
monitorModel = new MonitorModel();
}
monitorModel.setAutoRestart("on".equalsIgnoreCase(autoRestart));
monitorModel.setExecCron(execCron);
monitorModel.setExecCron(this.checkCron(execCron));
monitorModel.projects(nodeProjects);
monitorModel.setStatus(start);
monitorModel.notifyUser(notifyUserList);

View File

@ -7,11 +7,9 @@ import cn.jiangzeyin.common.validator.ValidatorItem;
import io.jpom.common.BaseServerController;
import io.jpom.common.forward.NodeForward;
import io.jpom.common.forward.NodeUrl;
import io.jpom.common.interceptor.PermissionInterceptor;
import io.jpom.model.PageResultDto;
import io.jpom.model.data.NodeModel;
import io.jpom.model.node.ScriptCacheModel;
import io.jpom.model.data.UserModel;
import io.jpom.permission.NodeDataPermission;
import io.jpom.permission.SystemPermission;
import io.jpom.plugin.ClassFeature;
@ -21,7 +19,6 @@ import io.jpom.service.node.script.NodeScriptExecuteLogServer;
import io.jpom.service.node.script.NodeScriptServer;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
@ -88,10 +85,7 @@ public class NodeScriptController extends BaseServerController {
@Feature(method = MethodFeature.EDIT)
public String save(String autoExecCron) {
NodeModel node = getNode();
if (StrUtil.isNotEmpty(autoExecCron)) {
UserModel user = getUser();
Assert.state(!user.isDemoUser(), PermissionInterceptor.DEMO_TIP);
}
this.checkCron(autoExecCron);
JsonMessage<Object> request = NodeForward.request(node, getRequest(), NodeUrl.Script_Save);
if (request.getCode() == HttpStatus.OK.value()) {
nodeScriptServer.syncNode(node);

View File

@ -25,14 +25,12 @@ package io.jpom.controller.script;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.cron.pattern.CronPattern;
import cn.jiangzeyin.common.JsonMessage;
import cn.jiangzeyin.common.validator.ValidatorItem;
import com.alibaba.fastjson.JSONObject;
import io.jpom.common.BaseServerController;
import io.jpom.common.forward.NodeForward;
import io.jpom.common.forward.NodeUrl;
import io.jpom.common.interceptor.PermissionInterceptor;
import io.jpom.model.PageResultDto;
import io.jpom.model.data.NodeModel;
import io.jpom.model.data.UserModel;
@ -102,18 +100,7 @@ public class ScriptController extends BaseServerController {
Assert.hasText(scriptModel.getContext(), "内容为空");
//
if (StrUtil.isNotEmpty(autoExecCron)) {
UserModel user = getUser();
Assert.state(!user.isDemoUser(), PermissionInterceptor.DEMO_TIP);
try {
new CronPattern(autoExecCron);
} catch (Exception e) {
throw new IllegalArgumentException("定时执行表达式格式不正确");
}
scriptModel.setAutoExecCron(autoExecCron);
} else {
scriptModel.setAutoExecCron(StrUtil.EMPTY);
}
scriptModel.setAutoExecCron(this.checkCron(autoExecCron));
//
String oldNodeIds = null;
if (StrUtil.isEmpty(id)) {

View File

@ -33,12 +33,12 @@ import cn.jiangzeyin.common.validator.ValidatorItem;
import cn.jiangzeyin.common.validator.ValidatorRule;
import com.alibaba.fastjson.JSONObject;
import io.jpom.common.BaseServerController;
import io.jpom.common.interceptor.PermissionInterceptor;
import io.jpom.model.data.MailAccountModel;
import io.jpom.model.data.UserModel;
import io.jpom.model.data.WorkspaceModel;
import io.jpom.monitor.EmailUtil;
import io.jpom.service.system.SystemParametersServer;
import io.jpom.service.system.WorkspaceService;
import io.jpom.service.user.UserBindWorkspaceService;
import io.jpom.service.user.UserService;
import io.jpom.util.TwoFactorAuthUtils;
@ -68,16 +68,13 @@ public class UserBasicInfoController extends BaseServerController {
private final SystemParametersServer systemParametersServer;
private final UserBindWorkspaceService userBindWorkspaceService;
private final UserService userService;
private final WorkspaceService workspaceService;
public UserBasicInfoController(SystemParametersServer systemParametersServer,
UserBindWorkspaceService userBindWorkspaceService,
UserService userService,
WorkspaceService workspaceService) {
UserService userService) {
this.systemParametersServer = systemParametersServer;
this.userBindWorkspaceService = userBindWorkspaceService;
this.userService = userService;
this.workspaceService = workspaceService;
}
@ -206,8 +203,9 @@ public class UserBasicInfoController extends BaseServerController {
UserModel user = getUser();
boolean bindMfa = userService.hasBindMfa(user.getId());
Assert.state(!bindMfa, "当前账号已经绑定 mfa 啦");
// demo
Assert.state(!user.isDemoUser(), PermissionInterceptor.DEMO_TIP);
//
Assert.state(user.isSuperSystemUser(), "当前用户不支持绑定");
boolean tfaCode = TwoFactorAuthUtils.validateTFACode(mfa, twoCode);
Assert.state(tfaCode, " mfa 验证码不正确");
userService.bindMfa(user.getId(), mfa);