Skip to content

Commit

Permalink
instance authorize
Browse files Browse the repository at this point in the history
  • Loading branch information
ponfee committed Dec 16, 2023
1 parent 7b7c987 commit 2d96a0c
Show file tree
Hide file tree
Showing 19 changed files with 375 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
initHeader();
// 初始化表体
initBody();
// 初始化数据服务
initServer();
if (options.firstLoad !== false) {
// 初始化数据服务
initServer();
}
// 动态设置表头宽度
autoTheadWidth(true);
// 缓存target对象
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ var table = {
pageSize: 10,
pageList: [10, 25, 50],
expandColumn: 1,
firstLoad: true,
showSearch: true,
showRefresh: true,
showColumns: true,
Expand All @@ -654,6 +655,7 @@ var table = {
pageSize: options.pageSize, // 每页的记录行数
pageList: options.pageList, // 可供选择的每页的行数
expandColumn: options.expandColumn, // 在哪一列上面显示展开按钮
firstLoad: options.firstLoad, // 是否首次请求加载数据,对于数据较大可以配置false
striped: options.striped, // 是否显示行间隔色
bordered: options.bordered, // 是否显示边框
toolbar: '#' + options.toolbar, // 指定工作栏
Expand Down Expand Up @@ -709,19 +711,20 @@ var table = {
// 表单封装处理
form: {
// 表单重置
reset: function(formId, tableId, pageNumber, pageSize) {
reset: function(formId, tableId, pageSize) {
table.set(tableId);
formId = $.common.isEmpty(formId) ? $('form').attr('id') : formId;
$("#" + formId)[0].reset();
var tableId = $.common.isEmpty(tableId) ? table.options.id : tableId;
if (table.options.type == table_type.bootstrapTable) {
var params = $("#" + tableId).bootstrapTable('getOptions');
if (table.options.type === table_type.bootstrapTable) {
var tableObj = $("#" + tableId);
var params = tableObj.bootstrapTable('getOptions');
params.pageNumber = 1;
if ($.common.isNotEmpty(pageSize)) {
params.pageSize = pageSize;
}
$("#" + tableId).bootstrapTable('refresh', params);
} else if (table.options.type == table_type.bootstrapTreeTable) {
tableObj.bootstrapTable('refresh', params);
} else if (table.options.type === table_type.bootstrapTreeTable) {
$("#" + tableId).bootstrapTreeTable('refresh', table.options.ajaxParams);
}
resetDate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,35 @@
package cn.ponfee.disjob.admin.controller;

import cn.ponfee.disjob.admin.util.PageUtils;
import cn.ponfee.disjob.common.model.PageResponse;
import cn.ponfee.disjob.common.util.Jsons;
import cn.ponfee.disjob.common.util.Numbers;
import cn.ponfee.disjob.common.util.SleepWaitUtils;
import cn.ponfee.disjob.common.util.TextTokenizer;
import cn.ponfee.disjob.core.enums.RunState;
import cn.ponfee.disjob.core.exception.KeyNotExistsException;
import cn.ponfee.disjob.supervisor.application.AuthorizeGroupService;
import cn.ponfee.disjob.supervisor.application.OpenapiService;
import cn.ponfee.disjob.supervisor.application.SchedGroupService;
import cn.ponfee.disjob.supervisor.application.request.SchedInstancePageRequest;
import cn.ponfee.disjob.supervisor.application.request.SearchJobRequest;
import cn.ponfee.disjob.supervisor.application.response.SchedInstanceResponse;
import cn.ponfee.disjob.supervisor.application.response.SchedTaskResponse;
import cn.ponfee.disjob.supervisor.component.DistributedJobQuerier;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

/**
* 任务实例Controller
Expand All @@ -42,11 +53,26 @@ public class DisjobInstanceController extends BaseController {

private static final int WAIT_SLEEP_ROUND = 9;
private static final long[] WAIT_SLEEP_MILLIS = {2500, 500};
private static final Pattern NUMBER_PATTERN = Pattern.compile("^[1-9]\\d*$");

private final OpenapiService openapiService;
private final DistributedJobQuerier jobQuerier;
private final AuthorizeGroupService authorizeGroupService;

public DisjobInstanceController(OpenapiService openapiService) {
public DisjobInstanceController(OpenapiService openapiService,
DistributedJobQuerier jobQuerier,
AuthorizeGroupService authorizeGroupService) {
this.openapiService = openapiService;
this.jobQuerier = jobQuerier;
this.authorizeGroupService = authorizeGroupService;
}

@RequiresPermissions(PERMISSION_INSTANCE)
@GetMapping("/match_job")
@ResponseBody
public AjaxResult matchJob(@RequestParam(value = "term") String term) {
SearchJobRequest req = parseTerm(term);
return AjaxResult.success(req == null ? Collections.emptyList() : jobQuerier.searchJob(req));
}

@RequiresPermissions(PERMISSION_INSTANCE)
Expand All @@ -61,7 +87,15 @@ public String instance() {
@RequiresPermissions(PERMISSION_INSTANCE)
@PostMapping("/tree")
@ResponseBody
public TableDataInfo tree(SchedInstancePageRequest request) {
public Object tree(SchedInstancePageRequest request) {
try {
request.authorize(getLoginName(), authorizeGroupService);
} catch (KeyNotExistsException e) {
return PageUtils.toTableDataInfo(PageResponse.empty());
} catch (Exception e) {
return AjaxResult.error(e.getMessage());
}

request.setParent(true);
request.setPageNumber(super.getPageNumber());
request.setPageSize(super.getPageSize());
Expand All @@ -74,7 +108,15 @@ public TableDataInfo tree(SchedInstancePageRequest request) {
@RequiresPermissions(PERMISSION_INSTANCE)
@PostMapping("/flat")
@ResponseBody
public TableDataInfo flat(SchedInstancePageRequest request) {
public Object flat(SchedInstancePageRequest request) {
try {
request.authorize(getLoginName(), authorizeGroupService);
} catch (KeyNotExistsException e) {
return PageUtils.toTableDataInfo(PageResponse.empty());
} catch (Exception e) {
return AjaxResult.error(e.getMessage());
}

request.setParent(false);
request.setPageNumber(super.getPageNumber());
request.setPageSize(super.getPageSize());
Expand All @@ -85,6 +127,8 @@ public TableDataInfo flat(SchedInstancePageRequest request) {
@PostMapping("/children")
@ResponseBody
public List<SchedInstanceResponse> children(@RequestParam("pnstanceId") Long pnstanceId) {
authorizeGroupService.authorizeInstance(getLoginName(), pnstanceId);

return openapiService.listInstanceChildren(pnstanceId);
}

Expand All @@ -108,6 +152,8 @@ public List<SchedInstanceResponse> children(@RequestParam("pnstanceId") Long pns
@RequiresPermissions(PERMISSION_INSTANCE)
@GetMapping("/tasks/{instanceId}")
public String tasks(@PathVariable("instanceId") Long instanceId, ModelMap mmap) {
authorizeGroupService.authorizeInstance(getLoginName(), instanceId);

List<SchedTaskResponse> tasks = openapiService.getInstanceTasks(instanceId);
mmap.put("tasks", Jsons.toJson(tasks));
return PREFIX + "/tasks";
Expand All @@ -123,6 +169,8 @@ public String tasks(@PathVariable("instanceId") Long instanceId, ModelMap mmap)
@PostMapping("/remove/{instanceId}")
@ResponseBody
public AjaxResult remove(@PathVariable("instanceId") Long instanceId) {
authorizeGroupService.authorizeInstance(getLoginName(), instanceId);

openapiService.deleteInstance(instanceId);
return success();
}
Expand All @@ -135,6 +183,8 @@ public AjaxResult remove(@PathVariable("instanceId") Long instanceId) {
@PostMapping("/pause/{instanceId}")
@ResponseBody
public AjaxResult pause(@PathVariable("instanceId") Long instanceId) {
authorizeGroupService.authorizeInstance(getLoginName(), instanceId);

openapiService.pauseInstance(instanceId);
SleepWaitUtils.waitUntil(WAIT_SLEEP_ROUND, WAIT_SLEEP_MILLIS, () -> {
SchedInstanceResponse instance = openapiService.getInstance(instanceId, false);
Expand All @@ -151,6 +201,8 @@ public AjaxResult pause(@PathVariable("instanceId") Long instanceId) {
@PostMapping("/resume/{instanceId}")
@ResponseBody
public AjaxResult resume(@PathVariable("instanceId") Long instanceId) {
authorizeGroupService.authorizeInstance(getLoginName(), instanceId);

openapiService.resumeInstance(instanceId);
SleepWaitUtils.waitUntil(WAIT_SLEEP_ROUND, new long[]{500, 200}, () -> {
SchedInstanceResponse instance = openapiService.getInstance(instanceId, false);
Expand All @@ -167,6 +219,8 @@ public AjaxResult resume(@PathVariable("instanceId") Long instanceId) {
@PostMapping("/cancel/{instanceId}")
@ResponseBody
public AjaxResult cancel(@PathVariable("instanceId") Long instanceId) {
authorizeGroupService.authorizeInstance(getLoginName(), instanceId);

openapiService.cancelInstance(instanceId);
SleepWaitUtils.waitUntil(WAIT_SLEEP_ROUND, WAIT_SLEEP_MILLIS, () -> {
SchedInstanceResponse instance = openapiService.getInstance(instanceId, false);
Expand All @@ -175,4 +229,41 @@ public AjaxResult cancel(@PathVariable("instanceId") Long instanceId) {
return success();
}

// ------------------------------------------------------------private methods

private SearchJobRequest parseTerm(String term) {
if (StringUtils.isBlank(term)) {
return null;
}

SearchJobRequest request = new SearchJobRequest();
Set<String> groups = null;
String user = getLoginName();
if ("*".equals(term)) {
request.setGroups(AuthorizeGroupService.truncateGroup(SchedGroupService.myGroups(user)));
return request;
}

TextTokenizer tokenizer = new TextTokenizer(term, ": ");
if (tokenizer.hasNext()) {
String str = tokenizer.next().trim();
if (SchedGroupService.myGroups(user).contains(str)) {
groups = Collections.singleton(str);
term = tokenizer.tail();
}
}
if (groups == null) {
groups = AuthorizeGroupService.truncateGroup(SchedGroupService.myGroups(user));
}

term = term.trim();
request.setGroups(groups);
request.setJobName(term);
if (NUMBER_PATTERN.matcher(term).matches()) {
request.setJobId(Numbers.toWrapLong(term));
}

return request;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@
<div class="select-list">
<ul>
<li>
<label>JobId:</label>
<input type="text" name="jobId" autocomplete="off" />
<label>Job:</label>
<select class="form-control" name="jobId">
<option value=""></option>
</select>
</li>
<li>
<label>InstanceId:</label>
<input type="text" name="instanceId" autocomplete="off" />
</li>
<li>
<label>运行状态:</label>
Expand Down Expand Up @@ -59,8 +65,14 @@
<div class="select-list">
<ul>
<li>
<label>JobId:</label>
<input type="text" name="jobId" autocomplete="off" />
<label>Job:</label>
<select class="form-control" name="jobId">
<option value=""></option>
</select>
</li>
<li>
<label>InstanceId:</label>
<input type="text" name="instanceId" autocomplete="off" />
</li>
<li>
<label>运行状态:</label>
Expand Down Expand Up @@ -254,7 +266,8 @@
code: "instanceId",
parentCode: "pnstanceId",
url: prefix + "/tree",
dataUrl: prefix + "/children"
dataUrl: prefix + "/children",
firstLoad: false
}, defaultOps);
$.treeTable.init(treeOps);

Expand Down Expand Up @@ -342,11 +355,36 @@
}
});
});

// ---------------------------------select2
$("select[name='jobId']").select2({
ajax: {
url: prefix + "/match_job",
type: "get",
dataType: 'json',
delay: 1000,
cache: true,
data: function (params) {
return {"term": params.term};
},
processResults: function (resp) {
return {results: resp.data };
}
},
placeholder: "`name`或`group: name`搜索",
minimumInputLength: 1,
templateSelection: function (repo) {
var array = repo.text.split(" | ");
return array.length > 1 ? array[1] : array[0];
},
width: "resolve"
});

});

function resetForm(formId, tableId) {
$.form.reset(formId, tableId);
["runState", "runType"].forEach(function (item) {
["jobId", "runState", "runType"].forEach(function (item) {
var elem = $("#" + formId).find("select[name='" + item + "']");
elem.val(elem.val()).trigger("change");
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package cn.ponfee.disjob.common.dag;

import cn.ponfee.disjob.common.util.TextTokenizer;
import org.springframework.util.Assert;

import java.beans.Transient;
Expand Down Expand Up @@ -119,10 +120,10 @@ public String toString() {
}

public static DAGNode fromString(String str) {
int pos = -1;
int section = Integer.parseInt(str.substring(pos += 1, pos = str.indexOf(COLON, pos)));
int ordinal = Integer.parseInt(str.substring(pos += 1, pos = str.indexOf(COLON, pos)));
String name = str.substring(pos + 1);
TextTokenizer tokenizer = new TextTokenizer(str, COLON);
int section = Integer.parseInt(tokenizer.next());
int ordinal = Integer.parseInt(tokenizer.next());
String name = tokenizer.tail();
if (START.equals(section, ordinal, name)) {
return START;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import lombok.Setter;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

Expand Down Expand Up @@ -68,4 +69,8 @@ public static int computeTotalPages(int pageSize, long total) {
return (int) ((total + pageSize - 1) / pageSize);
}

public static <T> PageResponse<T> empty() {
return new PageResponse<>(Collections.emptyList(), 0);
}

}
Loading

0 comments on commit 2d96a0c

Please sign in to comment.