cabloy 5.1.106 → 5.1.107

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.
Files changed (51) hide show
  1. package/.cabloy-version +1 -1
  2. package/CHANGELOG.md +12 -0
  3. package/cabloy-docs/backend/backend-resource-module-contract-chain.md +1 -0
  4. package/cabloy-docs/backend/controller-aop-guide.md +12 -0
  5. package/cabloy-docs/backend/controller-guide.md +45 -3
  6. package/cabloy-docs/backend/crud-workflow.md +14 -0
  7. package/cabloy-docs/backend/unit-testing.md +8 -2
  8. package/cabloy-docs/fullstack/openapi-to-sdk.md +1 -0
  9. package/cabloy-docs/fullstack/tutorial-2-first-crud.md +3 -0
  10. package/package.json +1 -1
  11. package/vona/packages-cli/cli/package.json +1 -1
  12. package/vona/packages-cli/cli-set-api/cli/templates/tools/crud/boilerplate/src/bean/ssrMenu.{{resourceName}}.ts_ +1 -0
  13. package/vona/packages-cli/cli-set-api/cli/templates/tools/crudBasic/boilerplate/src/controller/{{resourceName}}.ts_ +13 -4
  14. package/vona/packages-cli/cli-set-api/cli/templates/tools/crudBasic/boilerplate/test/{{resourceName}}.test.ts_ +22 -3
  15. package/vona/packages-cli/cli-set-api/cli/templates/tools/crudStart/boilerplate/src/controller/{{resourceName}}.ts_ +13 -4
  16. package/vona/packages-cli/cli-set-api/cli/templates/tools/crudStart/boilerplate/test/{{resourceName}}.test.ts_ +22 -3
  17. package/vona/packages-cli/cli-set-api/package.json +1 -1
  18. package/vona/packages-vona/vona/package.json +1 -1
  19. package/vona/src/suite/a-home/modules/home-user/src/.metadata/index.ts +2 -2
  20. package/vona/src/suite/a-home/modules/home-user/src/bean/meta.version.ts +10 -8
  21. package/vona/src/suite/a-home/modules/home-user/src/config/config.ts +6 -0
  22. package/vona/src/suite/a-home/modules/home-user/src/config/locale/en-us.ts +2 -0
  23. package/vona/src/suite/a-home/modules/home-user/src/config/locale/zh-cn.ts +2 -0
  24. package/vona/src/suite/a-home/modules/home-user/src/config/roles.ts +24 -0
  25. package/vona/src/suite/a-home/modules/home-user/src/entity/role.ts +7 -0
  26. package/vona/src/suite/a-home/modules/home-user/test/role.test.ts +45 -0
  27. package/vona/src/suite/a-training/modules/training-record/src/controller/record.ts +13 -4
  28. package/vona/src/suite/a-training/modules/training-record/test/record.test.ts +25 -2
  29. package/vona/src/suite/a-training/modules/training-student/src/controller/student.ts +18 -6
  30. package/vona/src/suite/a-training/modules/training-student/test/student.test.ts +33 -5
  31. package/vona/src/suite-vendor/a-cabloy/modules/a-ssr/cli/ssrMenuAdmin/boilerplate/{{sceneName}}.{{beanName}}.ts_ +1 -0
  32. package/vona/src/suite-vendor/a-cabloy/modules/a-ssr/package.json +1 -1
  33. package/vona/src/suite-vendor/a-cabloy/modules/a-ssr/src/service/ssrHandler.ts +1 -1
  34. package/vona/src/suite-vendor/a-cabloy/package.json +1 -1
  35. package/vona/src/suite-vendor/a-vona/modules/a-user/package.json +1 -1
  36. package/vona/src/suite-vendor/a-vona/modules/a-user/src/types/role.ts +3 -0
  37. package/vona/src/suite-vendor/a-vona/package.json +1 -1
  38. package/zova/packages-cli/cli/package.json +2 -2
  39. package/zova/packages-cli/cli-set-front/package.json +1 -1
  40. package/zova/packages-cli/cli-set-front/src/lib/bean/cli.openapi.generate.ts +4 -0
  41. package/zova/packages-zova/zova/package.json +2 -2
  42. package/zova/src/suite/a-home/modules/home-api/mock/passport.fake.ts +50 -1
  43. package/zova/src/suite/a-home/modules/home-api/src/api/openapi/types.ts +15 -7
  44. package/zova/src/suite/a-home/modules/home-base/src/.metadata/index.ts +10 -0
  45. package/zova/src/suite/a-home/modules/home-base/src/page/errorAccessDenied/controller.tsx +1 -1
  46. package/zova/src/suite/a-training/modules/training-student/src/api/openapi/types.ts +26 -17
  47. package/zova/src/suite/cabloy-basic/modules/basic-file/src/api/openapi/types.ts +26 -17
  48. package/zova/src/suite/cabloy-basic/modules/basic-image/src/api/openapi/types.ts +26 -17
  49. package/zova/src/suite-vendor/a-zova/modules/a-ssrserver/package.json +1 -1
  50. package/zova/src/suite-vendor/a-zova/modules/a-ssrserver/src/service/ssrHandler.ts +1 -1
  51. package/zova/src/suite-vendor/a-zova/package.json +2 -2
package/.cabloy-version CHANGED
@@ -1 +1 @@
1
- 5.1.106
1
+ 5.1.107
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # Changelog
2
2
 
3
+ ## 5.1.107
4
+
5
+ ### Features
6
+
7
+ - Localize persisted home roles.
8
+ - Define response contracts for resource mutations, including null responses.
9
+ - Secure generated CRUD actions.
10
+
11
+ ### Improvements
12
+
13
+ - Clarify built-in role configuration.
14
+
3
15
  ## 5.1.106
4
16
 
5
17
  ### Improvements
@@ -143,6 +143,7 @@ Representative source facts from this file:
143
143
  - the class is decorated with `@Controller<IControllerOptionsStudent>('student')`
144
144
  - the class is also decorated with `@Resource()`
145
145
  - CRUD actions are declared with `@Web.post()`, `@Web.get()`, `@Web.get(':id')`, `@Web.patch(':id')`, and `@Web.delete(':id')`
146
+ - the standard CRUD actions and the `summary` / `deleteForce` management actions explicitly use `@Passport.systemAdmin()`
146
147
  - request shapes use `@Arg.body(...)`, `@Arg.param(...)`, and `@Arg.filter(...)`
147
148
  - actions delegate to `this.scope.service.student`
148
149
 
@@ -99,6 +99,18 @@ Typical jobs include:
99
99
 
100
100
  These shorthands still map back to the generic aspect model.
101
101
 
102
+ ### Passport inheritance and action policy
103
+
104
+ The global Passport guard is the baseline for controller actions: without a local Passport decorator, an action requires an authenticated and activated user. It is not public.
105
+
106
+ Use a local Passport or domain guard when an action needs a policy beyond that baseline:
107
+
108
+ - use `@Passport.public()` only when anonymous access is intentional
109
+ - use `@Passport.systemAdmin()`, `@Passport.roleName(...)`, or a domain-specific guard when access needs additional restriction
110
+ - leave an action without a local Passport guard when authenticated-and-activated access is intentionally sufficient
111
+
112
+ The CRUD generator applies `@Passport.systemAdmin()` to each generated CRUD action rather than to the controller class. This keeps the administrative default explicit while allowing a future custom action to deliberately inherit the baseline or define its own policy.
113
+
102
114
  For the underlying auth, passport, and user-access model, see [Auth Guide](/backend/auth-guide) and [User Access Guide](/backend/user-access-guide).
103
115
 
104
116
  ## Interceptor
@@ -148,11 +148,11 @@ Representative pattern:
148
148
 
149
149
  ```typescript
150
150
  @Web.patch('updateUser/:id')
151
- updateUser(
151
+ async updateUser(
152
152
  @Arg.param('id') id: TableIdentity,
153
153
  @Arg.body(v.object(DtoUserUpdate)) user: DtoUserUpdate,
154
- ) {
155
- return this.scope.model.user.update(user, { where: { id } });
154
+ ): Promise<void> {
155
+ await this.scope.service.user.update(id, user);
156
156
  }
157
157
  ```
158
158
 
@@ -208,6 +208,48 @@ A practical rule is:
208
208
  - use return-type inference when the contract is obvious and simple
209
209
  - use explicit `@Api.body(...)` when the response shape needs more control or the inference boundary becomes unclear
210
210
 
211
+ ## Resource mutation response contract
212
+
213
+ Standard resource `update` and `delete` actions are command-style actions. Their public contract is successful completion, not an implicit persistence-model payload.
214
+
215
+ Representative pattern:
216
+
217
+ ```typescript
218
+ import { z } from 'zod';
219
+
220
+ @Web.patch(':id')
221
+ @Api.body(z.null())
222
+ @Passport.systemAdmin()
223
+ async update(
224
+ @Arg.param('id', v.tableIdentity()) id: TableIdentity,
225
+ @Arg.body() student: DtoStudentUpdate,
226
+ ): Promise<void> {
227
+ await this.scope.service.student.update(id, student);
228
+ }
229
+ ```
230
+
231
+ Use `await` without `return` at the controller boundary. A service or model may still return mutation data for internal orchestration, but that data is not automatically a stable public representation of the resource.
232
+
233
+ `Promise<void>` expresses the controller's command semantics, while `@Api.body(z.null())` explicitly supplies the response schema that runtime reflection cannot recover from `Promise<void>`. It makes the existing no-payload response visible to emitted OpenAPI and generated SDK consumers.
234
+
235
+ For a standard JSON action, Vona maps this no-result controller completion to its normal HTTP `200` success wrapper:
236
+
237
+ ```typescript
238
+ {
239
+ code: 0,
240
+ message: 'success',
241
+ data: null,
242
+ }
243
+ ```
244
+
245
+ This is Vona's default resource-mutation convention. It does not use HTTP `204 No Content`, so consumers can keep one uniform success-envelope protocol.
246
+
247
+ ### When a mutation should return data
248
+
249
+ Return data only when it is an intentional consumer-facing contract, such as an updated response DTO, a version token, or a job handle. Declare that shape explicitly with `@Api.body(...)`; add `@Core.serializer()` when the response depends on serializer transforms, exclusions, getters, or replacements. Do not forward raw ORM or service mutation results merely because the delegated method returns a value.
250
+
251
+ `create` and read actions commonly expose an identity or DTO and therefore use their own explicit response contracts. For generated resource defaults, see [CRUD Workflow](/backend/crud-workflow#generated-mutation-response-default). For action-level verification, see [Unit Testing](/backend/unit-testing#testing-controllers-through-actions). When a declared response shape changes, follow the [Backend OpenAPI to Frontend SDK](/fullstack/openapi-to-sdk) contract loop.
252
+
211
253
  ## Response wrapper behavior
212
254
 
213
255
  By default, Vona wraps the response body in a standard wrapper object.
@@ -43,6 +43,20 @@ The generator creates a connected set of files, typically including:
43
43
 
44
44
  This is exactly why this generator should be the default starting point. It gives a consistent starting shape across the backend thread.
45
45
 
46
+ ### Generated authorization default
47
+
48
+ The standard generated CRUD actions (`create`, `select`, `view`, `update`, and `delete`) each use `@Passport.systemAdmin()`. This is an explicit administrative whitelist for the generated Admin resource API, matching the generated SSR menu's `systemAdmin` visibility policy.
49
+
50
+ The guard is attached to individual actions rather than the controller class. When you add a custom action later, no local Passport guard intentionally means the global authenticated-and-activated baseline applies; it does not mean the action is public. Use `@Passport.public()` to intentionally allow anonymous access, or add a role/domain guard when an action needs stronger restriction.
51
+
52
+ SSR menu roles control navigation disclosure only. They never authorize a controller action, API, or resource; keep the action guard as the server-side authorization boundary. See [Controller AOP Guide](/backend/controller-aop-guide) and [Menu Guide](/backend/menu-guide).
53
+
54
+ ### Generated mutation response default
55
+
56
+ Generated resource `update` and `delete` actions are command-style actions. Their controllers use `Promise<void>`, await the service call without forwarding its result, and declare `@Api.body(z.null())` so OpenAPI and generated SDKs describe the normal HTTP `200` success wrapper accurately as `data: null`.
57
+
58
+ This controller-facing default does not require the service or model mutation method to return `void`. Those lower layers may retain mutation data for internal orchestration. See [Controller Guide](/backend/controller-guide#resource-mutation-response-contract) when a mutation deliberately needs a consumer-facing response payload.
59
+
46
60
  A practical generated-output checklist usually includes:
47
61
 
48
62
  - controller
@@ -121,13 +121,19 @@ A practical rule is:
121
121
  A representative contract-verification pattern is:
122
122
 
123
123
  ```typescript
124
- await app.bean.executor.performAction('patch', '/test/rest/product/:id', {
124
+ const updateRes = await app.bean.executor.performAction('patch', '/test/rest/product/:id', {
125
125
  params: { id: productId },
126
126
  body: dataUpdate,
127
127
  });
128
+ assert.equal(updateRes, null);
129
+
130
+ const deleteRes = await app.bean.executor.performAction('delete', '/test/rest/product/:id', {
131
+ params: { id: productId },
132
+ });
133
+ assert.equal(deleteRes, null);
128
134
  ```
129
135
 
130
- This is a good default because the same test can exercise params, body, route wiring, validation, and response behavior together.
136
+ For standard resource command mutations, these assertions verify the controller-facing no-payload runtime contract. Follow them with a query/read-back assertion to verify the persisted update or deletion. When the API is generated for frontend consumers, add a focused OpenAPI structural assertion that its wrapped response schema declares `data: null`; this verifies SDK-contract fidelity separately from responder behavior. The same test then exercises params, body, route wiring, validation, response behavior, and persistence effects together.
131
137
 
132
138
  ## Authentication simulation
133
139
 
@@ -68,6 +68,7 @@ For the deeper frontend perspective, see:
68
68
  If you want the shortest accurate mental model, use this forward chain:
69
69
 
70
70
  1. backend controller signatures define request and response entry surfaces
71
+ - command-style mutations that complete with Vona's wrapped `data: null` must declare `@Api.body(z.null())`; `Promise<void>` alone cannot preserve that generic type through runtime reflection
71
72
  2. backend DTO and entity fields shape named and shared contract structure
72
73
  3. validation rules and `v` helpers refine the machine-readable contract
73
74
  4. Vona emits OpenAPI output from those backend declarations
@@ -95,6 +95,9 @@ As you inspect the generated files, pay attention to the division of responsibil
95
95
 
96
96
  This is the backend contract thread that later tutorials will extend with `level`, `mobile`, render metadata, OpenAPI output, and row actions.
97
97
 
98
+ > [!NOTE]
99
+ > The generated Admin CRUD actions explicitly require the `systemAdmin` role. If this resource should serve another audience, adjust the relevant controller action guards as well as any SSR menu visibility; changing the menu alone does not authorize an API.
100
+
98
101
  ## Verification
99
102
 
100
103
  1. make sure the local dev workflow is running:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cabloy",
3
- "version": "5.1.106",
3
+ "version": "5.1.107",
4
4
  "gitHead": "2c5c19284bab738e492856189acb6fad74b8a7b7",
5
5
  "description": "A Node.js fullstack framework",
6
6
  "keywords": [
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vona-cli",
3
- "version": "1.1.129",
3
+ "version": "1.1.130",
4
4
  "gitHead": "a79189b882c17af5911573896a781bbb0046d37d",
5
5
  "description": "vona cli",
6
6
  "keywords": [
@@ -20,6 +20,7 @@ export interface ISsrMenuOptions<%=argv.resourceNameCapitalize%> extends IDecora
20
20
  },
21
21
  },
22
22
  group: '<%=argv.ssrSiteGroupName%>',
23
+ roles: ['systemAdmin'],
23
24
  },
24
25
  },
25
26
  site: ['<%=argv.ssrSiteOnionName%>'],
@@ -4,6 +4,8 @@ import type { IDecoratorControllerOptions } from 'vona-module-a-web';
4
4
 
5
5
  import { BeanBase } from 'vona';
6
6
  import { Api, Resource, v } from 'vona-module-a-openapiutils';
7
+ import { z } from 'zod';
8
+ import { Passport } from 'vona-module-a-user';
7
9
  import { Arg, Controller, Web } from 'vona-module-a-web';
8
10
 
9
11
  import type { Model<%=argv.resourceNameCapitalize%> } from '../model/<%=argv.resourceName%>.ts';
@@ -21,29 +23,36 @@ export interface IControllerOptions<%=argv.resourceNameCapitalize%> extends IDec
21
23
  export class Controller<%=argv.resourceNameCapitalize%> extends BeanBase {
22
24
  @Web.post()
23
25
  @Api.body(v.tableIdentity())
26
+ @Passport.systemAdmin()
24
27
  async create(@Arg.body() <%=argv.resourceName%>: Dto<%=argv.resourceNameCapitalize%>Create): Promise<TableIdentity> {
25
28
  return (await this.scope.service.<%=argv.resourceName%>.create(<%=argv.resourceName%>)).id;
26
29
  }
27
30
 
28
31
  @Web.get()
29
32
  @Api.body(Dto<%=argv.resourceNameCapitalize%>SelectRes)
33
+ @Passport.systemAdmin()
30
34
  async select(@Arg.filter(Dto<%=argv.resourceNameCapitalize%>SelectReq) params: IQueryParams<Model<%=argv.resourceNameCapitalize%>>): Promise<Dto<%=argv.resourceNameCapitalize%>SelectRes> {
31
35
  return await this.scope.service.<%=argv.resourceName%>.select(params);
32
36
  }
33
37
 
34
38
  @Web.get(':id')
35
39
  @Api.body(v.optional(), v.object(Dto<%=argv.resourceNameCapitalize%>View))
40
+ @Passport.systemAdmin()
36
41
  async view(@Arg.param('id', v.tableIdentity()) id: TableIdentity): Promise<Dto<%=argv.resourceNameCapitalize%>View | undefined> {
37
42
  return await this.scope.service.<%=argv.resourceName%>.view(id);
38
43
  }
39
44
 
40
45
  @Web.patch(':id')
41
- async update(@Arg.param('id', v.tableIdentity()) id: TableIdentity, @Arg.body() <%=argv.resourceName%>: Dto<%=argv.resourceNameCapitalize%>Update) {
42
- return await this.scope.service.<%=argv.resourceName%>.update(id, <%=argv.resourceName%>);
46
+ @Api.body(z.null())
47
+ @Passport.systemAdmin()
48
+ async update(@Arg.param('id', v.tableIdentity()) id: TableIdentity, @Arg.body() <%=argv.resourceName%>: Dto<%=argv.resourceNameCapitalize%>Update): Promise<void> {
49
+ await this.scope.service.<%=argv.resourceName%>.update(id, <%=argv.resourceName%>);
43
50
  }
44
51
 
45
52
  @Web.delete(':id')
46
- async delete(@Arg.param('id', v.tableIdentity()) id: TableIdentity) {
47
- return await this.scope.service.<%=argv.resourceName%>.delete(id);
53
+ @Api.body(z.null())
54
+ @Passport.systemAdmin()
55
+ async delete(@Arg.param('id', v.tableIdentity()) id: TableIdentity): Promise<void> {
56
+ await this.scope.service.<%=argv.resourceName%>.delete(id);
48
57
  }
49
58
  }
@@ -15,7 +15,24 @@ describe('<%=argv.resourceName%>.test.ts', () => {
15
15
  name: '__TomNew__',
16
16
  description: 'This is a test',
17
17
  };
18
- // login
18
+ // role-less authenticated users cannot access generated admin actions
19
+ await app.bean.passport.signinMock();
20
+ try {
21
+ app.bean.passport.current!.roles = [];
22
+ const actions = ['create', 'select', 'view', 'update', 'delete'];
23
+ const permissions = await Promise.all(
24
+ actions.map(action =>
25
+ app.bean.permission.retrievePermissionAction(
26
+ '<%=argv.moduleInfo.relativeName%>:<%=argv.resourceName%>',
27
+ action,
28
+ ),
29
+ ),
30
+ );
31
+ assert.deepEqual(permissions, actions.map(() => false));
32
+ } finally {
33
+ await app.bean.passport.signout();
34
+ }
35
+ // login as system admin
19
36
  await app.bean.passport.signinMock();
20
37
  // create
21
38
  const <%=argv.resourceName%>Id = await app.bean.executor.performAction('post', '<%=argv.moduleActionPathRaw%>', { body: data });
@@ -24,15 +41,17 @@ describe('<%=argv.resourceName%>.test.ts', () => {
24
41
  const selectRes: Dto<%=argv.resourceNameCapitalize%>SelectRes = await app.bean.executor.performAction('get', '<%=argv.moduleActionPathRaw%>');
25
42
  assert.equal(selectRes.list.findIndex(item => item.name === data.name) > -1, true);
26
43
  // update
27
- await app.bean.executor.performAction('patch', '<%=argv.moduleActionPathRaw%>/:id', {
44
+ const updateRes = await app.bean.executor.performAction('patch', '<%=argv.moduleActionPathRaw%>/:id', {
28
45
  params: { id: <%=argv.resourceName%>Id },
29
46
  body: dataUpdate,
30
47
  });
48
+ assert.equal(updateRes, null);
31
49
  // findOne
32
50
  let <%=argv.resourceName%>: Entity<%=argv.resourceNameCapitalize%> = await app.bean.executor.performAction('get', '<%=argv.moduleActionPathRaw%>/:id', { params: { id: <%=argv.resourceName%>Id } });
33
51
  assert.equal(<%=argv.resourceName%>.name, dataUpdate.name);
34
52
  // delete
35
- await app.bean.executor.performAction('delete', '<%=argv.moduleActionPathRaw%>/:id', { params: { id: <%=argv.resourceName%>.id } });
53
+ const deleteRes = await app.bean.executor.performAction('delete', '<%=argv.moduleActionPathRaw%>/:id', { params: { id: <%=argv.resourceName%>.id } });
54
+ assert.equal(deleteRes, null);
36
55
  // findOne
37
56
  <%=argv.resourceName%> = await app.bean.executor.performAction('get', '<%=argv.moduleActionPathRaw%>/:id', { params: { id: <%=argv.resourceName%>.id } });
38
57
  assert.equal(<%=argv.resourceName%>, undefined);
@@ -4,6 +4,8 @@ import type { IDecoratorControllerOptions } from 'vona-module-a-web';
4
4
 
5
5
  import { BeanBase } from 'vona';
6
6
  import { Api, Resource, v } from 'vona-module-a-openapiutils';
7
+ import { z } from 'zod';
8
+ import { Passport } from 'vona-module-a-user';
7
9
  import { Arg, Controller, Web } from 'vona-module-a-web';
8
10
 
9
11
  import type { Model<%=argv.resourceNameCapitalize%> } from '../model/<%=argv.resourceName%>.ts';
@@ -21,29 +23,36 @@ export interface IControllerOptions<%=argv.resourceNameCapitalize%> extends IDec
21
23
  export class Controller<%=argv.resourceNameCapitalize%> extends BeanBase {
22
24
  @Web.post()
23
25
  @Api.body(v.tableIdentity())
26
+ @Passport.systemAdmin()
24
27
  async create(@Arg.body() <%=argv.resourceName%>: Dto<%=argv.resourceNameCapitalize%>Create): Promise<TableIdentity> {
25
28
  return (await this.scope.service.<%=argv.resourceName%>.create(<%=argv.resourceName%>)).id;
26
29
  }
27
30
 
28
31
  @Web.get()
29
32
  @Api.body(Dto<%=argv.resourceNameCapitalize%>SelectRes)
33
+ @Passport.systemAdmin()
30
34
  async select(@Arg.filter(Dto<%=argv.resourceNameCapitalize%>SelectReq) params: IQueryParams<Model<%=argv.resourceNameCapitalize%>>): Promise<Dto<%=argv.resourceNameCapitalize%>SelectRes> {
31
35
  return await this.scope.service.<%=argv.resourceName%>.select(params);
32
36
  }
33
37
 
34
38
  @Web.get(':id')
35
39
  @Api.body(v.optional(), v.object(Dto<%=argv.resourceNameCapitalize%>View))
40
+ @Passport.systemAdmin()
36
41
  async view(@Arg.param('id', v.tableIdentity()) id: TableIdentity): Promise<Dto<%=argv.resourceNameCapitalize%>View | undefined> {
37
42
  return await this.scope.service.<%=argv.resourceName%>.view(id);
38
43
  }
39
44
 
40
45
  @Web.patch(':id')
41
- async update(@Arg.param('id', v.tableIdentity()) id: TableIdentity, @Arg.body() <%=argv.resourceName%>: Dto<%=argv.resourceNameCapitalize%>Update) {
42
- return await this.scope.service.<%=argv.resourceName%>.update(id, <%=argv.resourceName%>);
46
+ @Api.body(z.null())
47
+ @Passport.systemAdmin()
48
+ async update(@Arg.param('id', v.tableIdentity()) id: TableIdentity, @Arg.body() <%=argv.resourceName%>: Dto<%=argv.resourceNameCapitalize%>Update): Promise<void> {
49
+ await this.scope.service.<%=argv.resourceName%>.update(id, <%=argv.resourceName%>);
43
50
  }
44
51
 
45
52
  @Web.delete(':id')
46
- async delete(@Arg.param('id', v.tableIdentity()) id: TableIdentity) {
47
- return await this.scope.service.<%=argv.resourceName%>.delete(id);
53
+ @Api.body(z.null())
54
+ @Passport.systemAdmin()
55
+ async delete(@Arg.param('id', v.tableIdentity()) id: TableIdentity): Promise<void> {
56
+ await this.scope.service.<%=argv.resourceName%>.delete(id);
48
57
  }
49
58
  }
@@ -15,7 +15,24 @@ describe('<%=argv.resourceName%>.test.ts', () => {
15
15
  name: '__TomNew__',
16
16
  description: 'This is a test',
17
17
  };
18
- // login
18
+ // role-less authenticated users cannot access generated admin actions
19
+ await app.bean.passport.signinMock();
20
+ try {
21
+ app.bean.passport.current!.roles = [];
22
+ const actions = ['create', 'select', 'view', 'update', 'delete'];
23
+ const permissions = await Promise.all(
24
+ actions.map(action =>
25
+ app.bean.permission.retrievePermissionAction(
26
+ '<%=argv.moduleInfo.relativeName%>:<%=argv.resourceName%>',
27
+ action,
28
+ ),
29
+ ),
30
+ );
31
+ assert.deepEqual(permissions, actions.map(() => false));
32
+ } finally {
33
+ await app.bean.passport.signout();
34
+ }
35
+ // login as system admin
19
36
  await app.bean.passport.signinMock();
20
37
  // create
21
38
  const <%=argv.resourceName%>Id = await app.bean.executor.performAction('post', '<%=argv.moduleActionPathRaw%>', { body: data });
@@ -24,15 +41,17 @@ describe('<%=argv.resourceName%>.test.ts', () => {
24
41
  const selectRes: Dto<%=argv.resourceNameCapitalize%>SelectRes = await app.bean.executor.performAction('get', '<%=argv.moduleActionPathRaw%>');
25
42
  assert.equal(selectRes.list.findIndex(item => item.name === data.name) > -1, true);
26
43
  // update
27
- await app.bean.executor.performAction('patch', '<%=argv.moduleActionPathRaw%>/:id', {
44
+ const updateRes = await app.bean.executor.performAction('patch', '<%=argv.moduleActionPathRaw%>/:id', {
28
45
  params: { id: <%=argv.resourceName%>Id },
29
46
  body: dataUpdate,
30
47
  });
48
+ assert.equal(updateRes, null);
31
49
  // findOne
32
50
  let <%=argv.resourceName%>: Entity<%=argv.resourceNameCapitalize%> = await app.bean.executor.performAction('get', '<%=argv.moduleActionPathRaw%>/:id', { params: { id: <%=argv.resourceName%>Id } });
33
51
  assert.equal(<%=argv.resourceName%>.name, dataUpdate.name);
34
52
  // delete
35
- await app.bean.executor.performAction('delete', '<%=argv.moduleActionPathRaw%>/:id', { params: { id: <%=argv.resourceName%>.id } });
53
+ const deleteRes = await app.bean.executor.performAction('delete', '<%=argv.moduleActionPathRaw%>/:id', { params: { id: <%=argv.resourceName%>.id } });
54
+ assert.equal(deleteRes, null);
36
55
  // findOne
37
56
  <%=argv.resourceName%> = await app.bean.executor.performAction('get', '<%=argv.moduleActionPathRaw%>/:id', { params: { id: <%=argv.resourceName%>.id } });
38
57
  assert.equal(<%=argv.resourceName%>, undefined);
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vona-cli-set-api",
3
- "version": "1.1.127",
3
+ "version": "1.1.128",
4
4
  "gitHead": "a79189b882c17af5911573896a781bbb0046d37d",
5
5
  "description": "vona cli-set-api",
6
6
  "keywords": [
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vona",
3
- "version": "5.1.74",
3
+ "version": "5.1.75",
4
4
  "gitHead": "a79189b882c17af5911573896a781bbb0046d37d",
5
5
  "description": "Vona is an intuitive, elegant and powerful Node.js framework for rapidly developing enterprise applications of any size",
6
6
  "keywords": [
@@ -144,7 +144,7 @@ import { SymbolKeyEntity, SymbolKeyEntityMeta, SymbolKeyModelOptions } from 'von
144
144
  declare module 'vona-module-home-user' {
145
145
  export interface IModelOptionsUser {
146
146
  relations: {
147
- roles: IModelRelationBelongsToMany<ModelRoleUser, ModelRole, false, undefined,undefined,undefined,undefined>;
147
+ roles: IModelRelationBelongsToMany<'home-user:roleUser', 'home-user:role', false, undefined,undefined,undefined,undefined>;
148
148
  };
149
149
  }
150
150
  export interface ModelRole {
@@ -511,8 +511,8 @@ export interface IApiPathPostRecord{
511
511
  '/home/user/passport/logout': undefined;
512
512
  '/home/user/passport/register': undefined;
513
513
  '/home/user/passport/login': undefined;
514
- '/home/user/passport/createPassportJwtFromOauthCode': undefined;
515
514
  '/home/user/passport/refreshAuthToken': undefined;
515
+ '/home/user/passport/createPassportJwtFromOauthCode': undefined;
516
516
  '/home/user/passport/createTempAuthToken': undefined;
517
517
  }
518
518
 
@@ -18,6 +18,8 @@ export class MetaVersion extends BeanBase implements IMetaVersionUpdate, IMetaVe
18
18
  table.comment(entityRole.$comment.$table);
19
19
  table.basicFields();
20
20
  table.string(entityRole.name, 255).comment(entityRole.$comment.name);
21
+ table.string(entityRole.title, 255).comment(entityRole.$comment.title);
22
+ table.json(entityRole.locales).comment(entityRole.$comment.locales);
21
23
  table.json(entityRole.siteIds).comment(entityRole.$comment.siteIds);
22
24
  });
23
25
  // homeUser
@@ -49,14 +51,14 @@ export class MetaVersion extends BeanBase implements IMetaVersionUpdate, IMetaVe
49
51
  async init(options: IMetaVersionInitOptions) {
50
52
  if (options.version === 1) {
51
53
  // roles
52
- await this.scope.model.role.insert({
53
- name: 'registeredUser',
54
- siteIds: ['web'],
55
- });
56
- await this.scope.model.role.insert({
57
- name: 'systemAdmin',
58
- siteIds: ['web', 'admin'],
59
- });
54
+ for (const [name, role] of Object.entries(this.scope.config.builtinRoles)) {
55
+ await this.scope.model.role.insert({
56
+ name,
57
+ title: role.title,
58
+ locales: role.locales ?? {},
59
+ siteIds: role.siteIds,
60
+ });
61
+ }
60
62
  // user: admin
61
63
  if (!this.scope.config.disableUserAdmin) {
62
64
  await this.bean.auth.authenticate('auth-simple:simple', {
@@ -1,5 +1,10 @@
1
1
  import type { VonaApplication } from 'vona';
2
2
 
3
+ import { builtinRoles } from './roles.ts';
4
+
5
+ export { builtinRoles } from './roles.ts';
6
+ export type { IRoleConfig } from './roles.ts';
7
+
3
8
  export function config(_app: VonaApplication) {
4
9
  return {
5
10
  passwordDefault: {
@@ -7,5 +12,6 @@ export function config(_app: VonaApplication) {
7
12
  },
8
13
  disableBootstrapSystemAdmin: false,
9
14
  disableUserAdmin: false,
15
+ builtinRoles,
10
16
  };
11
17
  }
@@ -4,6 +4,8 @@ export default {
4
4
  PasswordResetEmailExpired: 'This password reset link has expired',
5
5
  Role: 'Role',
6
6
  RoleName: 'Role Name',
7
+ RoleTitle: 'Role Title',
8
+ RoleLocales: 'Role Locales',
7
9
  User: 'User',
8
10
  UserName: 'User Name',
9
11
  UserAvatar: 'Avatar',
@@ -4,6 +4,8 @@ export default {
4
4
  PasswordResetEmailExpired: '重置密码链接已经过期',
5
5
  Role: '角色',
6
6
  RoleName: '角色名',
7
+ RoleTitle: '角色标题',
8
+ RoleLocales: '角色本地化文本',
7
9
  User: '用户',
8
10
  UserName: '用户名',
9
11
  UserAvatar: '头像',
@@ -0,0 +1,24 @@
1
+ import type { ILocaleRecord } from 'vona';
2
+
3
+ export interface IRoleConfig {
4
+ title: string;
5
+ locales?: Partial<Record<keyof ILocaleRecord, string>>;
6
+ siteIds: string[];
7
+ }
8
+
9
+ export const builtinRoles: Record<string, IRoleConfig> = {
10
+ registeredUser: {
11
+ title: 'Registered User',
12
+ locales: {
13
+ 'zh-cn': '注册用户',
14
+ },
15
+ siteIds: ['web'],
16
+ },
17
+ systemAdmin: {
18
+ title: 'System Administrator',
19
+ locales: {
20
+ 'zh-cn': '系统管理员',
21
+ },
22
+ siteIds: ['web', 'admin'],
23
+ },
24
+ };
@@ -1,3 +1,4 @@
1
+ import type { ILocaleRecord } from 'vona';
1
2
  import type { IDecoratorEntityOptions } from 'vona-module-a-orm';
2
3
  import type { IRole } from 'vona-module-a-user';
3
4
 
@@ -14,6 +15,12 @@ export class EntityRole extends EntityBase implements IRole {
14
15
  @Api.field(v.title($locale('RoleName')))
15
16
  name: string;
16
17
 
18
+ @Api.field(v.title($locale('RoleTitle')))
19
+ title: string;
20
+
21
+ @Api.field(v.title($locale('RoleLocales')), v.optional(), z.record(z.string(), z.string()))
22
+ locales?: Partial<Record<keyof ILocaleRecord, string>>;
23
+
17
24
  @Api.field(v.array(z.string()))
18
25
  siteIds: string[];
19
26
  }
@@ -0,0 +1,45 @@
1
+ import assert from 'node:assert';
2
+ import { describe, it } from 'node:test';
3
+ import { app } from 'vona-mock';
4
+
5
+ describe('role.test.ts', () => {
6
+ it('action:roleSeedAndPassport', async () => {
7
+ await app.bean.executor.mockCtx(async () => {
8
+ const scope = app.scope('home-user');
9
+ const roleRegisteredUser = await scope.model.role.getByName('registeredUser');
10
+ assert.equal(roleRegisteredUser?.title, 'Registered User');
11
+ assert.deepEqual(roleRegisteredUser?.locales, { 'zh-cn': '注册用户' });
12
+ assert.deepEqual(roleRegisteredUser?.siteIds, ['web']);
13
+
14
+ const roleSystemAdmin = await scope.model.role.getByName('systemAdmin');
15
+ assert.equal(roleSystemAdmin?.title, 'System Administrator');
16
+ assert.deepEqual(roleSystemAdmin?.locales, { 'zh-cn': '系统管理员' });
17
+ assert.deepEqual(roleSystemAdmin?.siteIds, ['web', 'admin']);
18
+
19
+ await app.bean.passport.signinMock();
20
+ const passport = await app.bean.executor.performAction('get', '/home/user/passport/current');
21
+ assert.deepEqual(
22
+ passport.roles.map(role => ({
23
+ name: role.name,
24
+ title: role.title,
25
+ locales: role.locales,
26
+ siteIds: role.siteIds,
27
+ })),
28
+ [
29
+ {
30
+ name: 'registeredUser',
31
+ title: 'Registered User',
32
+ locales: { 'zh-cn': '注册用户' },
33
+ siteIds: ['web'],
34
+ },
35
+ {
36
+ name: 'systemAdmin',
37
+ title: 'System Administrator',
38
+ locales: { 'zh-cn': '系统管理员' },
39
+ siteIds: ['web', 'admin'],
40
+ },
41
+ ],
42
+ );
43
+ });
44
+ });
45
+ });