midway-fatcms 0.0.10 → 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,6 +6,7 @@ import { BaseApiController } from '../base/BaseApiController';
6
6
  export declare class AsyncTaskController extends BaseApiController {
7
7
  protected ctx: Context;
8
8
  private asyncTaskService;
9
+ private crudStdService;
9
10
  getMyTasks(): Promise<import("../../libs/crud-pro/models/ExecuteContext").ExecuteContext>;
10
11
  createTask(): Promise<import("../../libs/crud-pro/models/ExecuteContext").ExecuteContext>;
11
12
  cancelTask(id: number): Promise<{
@@ -15,6 +15,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.AsyncTaskController = void 0;
16
16
  const fs = require("fs");
17
17
  const fs2 = require("node:fs/promises");
18
+ const md5 = require("md5");
18
19
  const core_1 = require("@midwayjs/core");
19
20
  const BaseApiController_1 = require("../base/BaseApiController");
20
21
  const AsyncTaskService_1 = require("../../service/asyncTask/AsyncTaskService");
@@ -24,6 +25,8 @@ const SystemTables_1 = require("../../models/SystemTables");
24
25
  const exceptions_1 = require("../../libs/crud-pro/exceptions");
25
26
  const permission_middleware_1 = require("../../middleware/permission.middleware");
26
27
  const functions_1 = require("../../libs/utils/functions");
28
+ const CrudStdService_1 = require("../../service/crudstd/CrudStdService");
29
+ const devops_1 = require("../../models/devops");
27
30
  function fixMyTasksCondition(body, ctx) {
28
31
  if (!body.condition) {
29
32
  throw new exceptions_1.CommonException('参数不正确');
@@ -43,6 +46,27 @@ function fixCancelBodyData(body, id) {
43
46
  dataObj.task_status = AsyncTaskModel_1.SysAsyncTaskStatus.CANCELLED;
44
47
  body.data = dataObj;
45
48
  }
49
+ /**
50
+ * 验证 STD_CRUD 类型的异步任务权限
51
+ * 根据 settingKey 配置的权限项进行鉴权
52
+ */
53
+ async function validateStdCrudTaskPermission(crudStdService, inputParams) {
54
+ var _a;
55
+ if (inputParams.appType !== 'STD_CRUD' || !inputParams.settingKey) {
56
+ return; // 非 STD_CRUD 类型或无 settingKey,无需鉴权
57
+ }
58
+ const stdAction = {
59
+ appCode: inputParams.appCode,
60
+ settingKey: inputParams.settingKey,
61
+ };
62
+ const appInfo = await crudStdService.getParsedCrudStdAppForSettingKey(stdAction);
63
+ if (!appInfo || appInfo.status !== 1) {
64
+ throw new devops_1.BizException('应用不存在或已下线:' + inputParams.appCode);
65
+ }
66
+ if (!((_a = appInfo.settingKeyActionCfg) === null || _a === void 0 ? void 0 : _a.hasOperationPerm)) {
67
+ throw new devops_1.BizException('没有操作权限:settingKey=' + inputParams.settingKey);
68
+ }
69
+ }
46
70
  function fixCreateBodyData(body, ctx) {
47
71
  if (!body.data) {
48
72
  throw new exceptions_1.CommonException('参数不正确');
@@ -57,6 +81,11 @@ function fixCreateBodyData(body, ctx) {
57
81
  host: headers.host,
58
82
  origin: headers.origin,
59
83
  };
84
+ dataObj.task_uuid = md5(JSON.stringify({
85
+ input_params: input_params,
86
+ created_by: sessionInfo.accountId,
87
+ created_time: Date.now(),
88
+ }));
60
89
  dataObj.task_status = AsyncTaskModel_1.SysAsyncTaskStatus.PENDING;
61
90
  dataObj.created_by = sessionInfo.accountId;
62
91
  dataObj.created_user_session = JSON.stringify(sessionInfo); // 创建人的session信息。用于执行时的鉴权。
@@ -73,9 +102,14 @@ let AsyncTaskController = class AsyncTaskController extends BaseApiController_1.
73
102
  }
74
103
  // 创建任务
75
104
  async createTask() {
76
- //每个用户:5秒内只能创建1次任务
105
+ var _a;
106
+ // 每个用户:5秒内只能创建1次任务
77
107
  await this.checkUserActionTimeLimit('AsyncTaskController_createTask', 5);
78
- fixCreateBodyData(this.ctx.request.body, this.ctx);
108
+ // 解析 input_params 并进行权限鉴权
109
+ const body = this.ctx.request.body;
110
+ const inputParams = (0, functions_1.parseJsonObject)((_a = body.data) === null || _a === void 0 ? void 0 : _a.input_params) || {};
111
+ await validateStdCrudTaskPermission(this.crudStdService, inputParams);
112
+ fixCreateBodyData(body, this.ctx);
79
113
  const res = await this.executeSysSimpleSQL(SystemTables_1.SystemTables.sys_async_tasks, keys_1.KeysOfSimpleSQL.SIMPLE_INSERT);
80
114
  await this.asyncTaskService.startTask();
81
115
  return res;
@@ -148,6 +182,10 @@ __decorate([
148
182
  (0, core_1.Inject)(),
149
183
  __metadata("design:type", AsyncTaskService_1.AsyncTaskService)
150
184
  ], AsyncTaskController.prototype, "asyncTaskService", void 0);
185
+ __decorate([
186
+ (0, core_1.Inject)(),
187
+ __metadata("design:type", CrudStdService_1.CrudStdService)
188
+ ], AsyncTaskController.prototype, "crudStdService", void 0);
151
189
  __decorate([
152
190
  (0, core_1.Post)('/getMyTasks'),
153
191
  __metadata("design:type", Function),
@@ -2,7 +2,7 @@ import { Context } from '@midwayjs/koa';
2
2
  import { CurdMixService } from '../curd/CurdMixService';
3
3
  import { IRequestModel } from '../../libs/crud-pro/interfaces';
4
4
  import { KeysOfSimpleSQL } from '../../libs/crud-pro/models/keys';
5
- import { ICrudStdAppInfo } from '../../models/bizmodels';
5
+ import { ICrudStdAppInfo, ICrudStdAppInfoForSettingKey } from '../../models/bizmodels';
6
6
  import { ExecuteContext } from '../../libs/crud-pro/models/ExecuteContext';
7
7
  import { CrudStdActionService } from './CrudStdActionService';
8
8
  import { CrudStdRelationService } from './CrudStdRelationService';
@@ -37,9 +37,8 @@ export declare class CrudStdService extends ApiBaseService {
37
37
  * 获取appInfo 并且拿到当前settingKey相关的信息
38
38
  * @param appCode
39
39
  * @param settingKey
40
- * @private
41
40
  */
42
- private getParsedCrudStdAppForSettingKey;
41
+ getParsedCrudStdAppForSettingKey(stdAction: ICrudStdActionParams): Promise<ICrudStdAppInfoForSettingKey>;
43
42
  /**
44
43
  * 执行动作
45
44
  * @param stdAction
@@ -152,7 +152,6 @@ let CrudStdService = class CrudStdService extends ApiBaseService_1.ApiBaseServic
152
152
  * 获取appInfo 并且拿到当前settingKey相关的信息
153
153
  * @param appCode
154
154
  * @param settingKey
155
- * @private
156
155
  */
157
156
  async getParsedCrudStdAppForSettingKey(stdAction) {
158
157
  const { appCode, settingKey, buttonSettingKey } = stdAction || {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "midway-fatcms",
3
- "version": "0.0.10",
3
+ "version": "0.0.11",
4
4
  "description": "This is a midway component sample",
5
5
  "main": "dist/index.js",
6
6
  "typings": "index.d.ts",
@@ -1,5 +1,6 @@
1
1
  import * as fs from 'fs';
2
2
  import * as fs2 from 'node:fs/promises';
3
+ import * as md5 from 'md5';
3
4
  import { Controller, Inject, Post, Query, Get } from '@midwayjs/core';
4
5
  import { Context } from '@midwayjs/koa';
5
6
  import { BaseApiController } from '../base/BaseApiController';
@@ -10,6 +11,8 @@ import { SystemTables } from '@/models/SystemTables';
10
11
  import { CommonException } from '@/libs/crud-pro/exceptions';
11
12
  import { checkLogin } from '@/middleware/permission.middleware';
12
13
  import { parseJsonObject } from '@/libs/utils/functions';
14
+ import { CrudStdService, ICrudStdActionParams } from '@/service/crudstd/CrudStdService';
15
+ import { BizException } from '@/models/devops';
13
16
 
14
17
  function fixMyTasksCondition(body: any, ctx: Context) {
15
18
  if (!body.condition) {
@@ -34,6 +37,31 @@ function fixCancelBodyData(body: any, id: number) {
34
37
  body.data = dataObj;
35
38
  }
36
39
 
40
+ /**
41
+ * 验证 STD_CRUD 类型的异步任务权限
42
+ * 根据 settingKey 配置的权限项进行鉴权
43
+ */
44
+ async function validateStdCrudTaskPermission(
45
+ crudStdService: CrudStdService,
46
+ inputParams: any
47
+ ): Promise<void> {
48
+ if (inputParams.appType !== 'STD_CRUD' || !inputParams.settingKey) {
49
+ return; // 非 STD_CRUD 类型或无 settingKey,无需鉴权
50
+ }
51
+
52
+ const stdAction: ICrudStdActionParams = {
53
+ appCode: inputParams.appCode,
54
+ settingKey: inputParams.settingKey,
55
+ };
56
+ const appInfo = await crudStdService.getParsedCrudStdAppForSettingKey(stdAction);
57
+ if (!appInfo || appInfo.status !== 1) {
58
+ throw new BizException('应用不存在或已下线:' + inputParams.appCode);
59
+ }
60
+ if (!appInfo.settingKeyActionCfg?.hasOperationPerm) {
61
+ throw new BizException('没有操作权限:settingKey=' + inputParams.settingKey);
62
+ }
63
+ }
64
+
37
65
  function fixCreateBodyData(body: any, ctx: Context) {
38
66
  if (!body.data) {
39
67
  throw new CommonException('参数不正确');
@@ -49,6 +77,11 @@ function fixCreateBodyData(body: any, ctx: Context) {
49
77
  host: headers.host,
50
78
  origin: headers.origin,
51
79
  };
80
+ dataObj.task_uuid = md5(JSON.stringify({
81
+ input_params: input_params,
82
+ created_by: sessionInfo.accountId,
83
+ created_time: Date.now(),
84
+ }));
52
85
  dataObj.task_status = SysAsyncTaskStatus.PENDING;
53
86
  dataObj.created_by = sessionInfo.accountId;
54
87
  dataObj.created_user_session = JSON.stringify(sessionInfo); // 创建人的session信息。用于执行时的鉴权。
@@ -66,6 +99,9 @@ export class AsyncTaskController extends BaseApiController {
66
99
  @Inject()
67
100
  private asyncTaskService: AsyncTaskService;
68
101
 
102
+ @Inject()
103
+ private crudStdService: CrudStdService;
104
+
69
105
  // 获取任务列表
70
106
  @Post('/getMyTasks')
71
107
  async getMyTasks() {
@@ -76,10 +112,15 @@ export class AsyncTaskController extends BaseApiController {
76
112
  // 创建任务
77
113
  @Post('/createTask')
78
114
  async createTask() {
79
- //每个用户:5秒内只能创建1次任务
115
+ // 每个用户:5秒内只能创建1次任务
80
116
  await this.checkUserActionTimeLimit('AsyncTaskController_createTask', 5);
81
117
 
82
- fixCreateBodyData(this.ctx.request.body, this.ctx);
118
+ // 解析 input_params 并进行权限鉴权
119
+ const body = this.ctx.request.body as any;
120
+ const inputParams = parseJsonObject(body.data?.input_params) || {};
121
+ await validateStdCrudTaskPermission(this.crudStdService, inputParams);
122
+
123
+ fixCreateBodyData(body, this.ctx);
83
124
  const res = await this.executeSysSimpleSQL(SystemTables.sys_async_tasks, KeysOfSimpleSQL.SIMPLE_INSERT);
84
125
  await this.asyncTaskService.startTask();
85
126
  return res;
@@ -216,9 +216,8 @@ export class CrudStdService extends ApiBaseService {
216
216
  * 获取appInfo 并且拿到当前settingKey相关的信息
217
217
  * @param appCode
218
218
  * @param settingKey
219
- * @private
220
219
  */
221
- private async getParsedCrudStdAppForSettingKey(stdAction: ICrudStdActionParams): Promise<ICrudStdAppInfoForSettingKey> {
220
+ public async getParsedCrudStdAppForSettingKey(stdAction: ICrudStdActionParams): Promise<ICrudStdAppInfoForSettingKey> {
222
221
  const { appCode, settingKey, buttonSettingKey } = stdAction || {};
223
222
  if (!appCode) {
224
223
  throw new BizException('缺少参数:curdStdAppCode');