Skip to content

Commit 879f51b

Browse files
committed
feat: 更新共享规则模型,增加组织范围默认值和共享接收者类型
1 parent 511af0b commit 879f51b

File tree

1 file changed

+68
-34
lines changed

1 file changed

+68
-34
lines changed
Lines changed: 68 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
import { z } from 'zod';
22

3+
/**
4+
* Organization-Wide Defaults (OWD)
5+
* The baseline security posture for an object.
6+
*/
7+
export const OWDModel = z.enum([
8+
'private', // Only owner can see
9+
'public_read', // Everyone can see, owner can edit
10+
'public_read_write', // Everyone can see and edit
11+
'controlled_by_parent' // Access derived from parent record (Master-Detail)
12+
]);
13+
314
/**
415
* Sharing Rule Type
516
* How is the data shared?
617
*/
718
export const SharingRuleType = z.enum([
819
'owner', // Based on record ownership (Role Hierarchy)
920
'criteria', // Based on field values (e.g. Status = 'Open')
10-
'manual', // Ad-hoc sharing (User specific)
11-
'guest' // Public access
1221
]);
1322

1423
/**
@@ -17,50 +26,75 @@ export const SharingRuleType = z.enum([
1726
*/
1827
export const SharingLevel = z.enum([
1928
'read', // Read Only
20-
'edit' // Read / Write
29+
'edit', // Read / Write
30+
'full' // Full Access (Transfer, Share, Delete)
2131
]);
2232

2333
/**
24-
* Sharing Rule Schema
25-
* Defines AUTOMATIC access grants based on logic.
26-
* The core engine of the governance layer.
34+
* Recipient Type
35+
* Who receives the access?
2736
*/
28-
export const SharingRuleSchema = z.object({
29-
name: z.string().regex(/^[a-z_][a-z0-9_]*$/).describe('Unique rule name'),
30-
label: z.string().optional(),
31-
active: z.boolean().default(true),
32-
33-
/** Target Object */
34-
object: z.string().describe('Object to share'),
35-
36-
/** Grant Logic */
37-
type: SharingRuleType.default('criteria'),
37+
export const ShareRecipientType = z.enum([
38+
'user',
39+
'group',
40+
'role',
41+
'role_and_subordinates',
42+
'guest' // for public sharing
43+
]);
44+
45+
/**
46+
* Base Sharing Rule
47+
* Common metadata for all sharing strategies.
48+
*/
49+
const BaseSharingRuleSchema = z.object({
50+
// Identification
51+
name: z.string().regex(/^[a-z_][a-z0-9_]*$/).describe('Unique rule name (snake_case)'),
52+
label: z.string().optional().describe('Human-readable label'),
53+
description: z.string().optional().describe('Administrative notes'),
3854

39-
/**
40-
* Criteria (for type='criteria')
41-
* SQL-like condition: "department = 'Sales' AND amount > 10000"
42-
*/
43-
criteria: z.string().optional(),
55+
// Scope
56+
object: z.string().describe('Target Object Name'),
57+
active: z.boolean().default(true),
4458

45-
/** Access Level */
59+
// Access
4660
accessLevel: SharingLevel.default('read'),
4761

48-
/**
49-
* Target Audience (Whom to share with)
50-
* ID of a Group, Role, or User.
51-
*/
52-
sharedWith: z.string().describe('Group/Role ID to share records with'),
62+
// Recipient (Whom to share with)
63+
sharedWith: z.object({
64+
type: ShareRecipientType,
65+
value: z.string().describe('ID or Code of the User/Group/Role'),
66+
}).describe('The recipient of the shared access'),
5367
});
5468

5569
/**
56-
* Organization-Wide Defaults (OWD)
57-
* The baseline security posture for an object.
70+
* 1. Criteria-Based Sharing Rule
71+
* Share records that meet specific field criteria.
5872
*/
59-
export const OWDModel = z.enum([
60-
'private', // Only owner can see
61-
'public_read', // Everyone can see, owner can edit
62-
'public_read_write' // Everyone can see and edit
73+
export const CriteriaSharingRuleSchema = BaseSharingRuleSchema.extend({
74+
type: z.literal('criteria'),
75+
condition: z.string().describe('Formula condition (e.g. "department = \'Sales\'")'),
76+
});
77+
78+
/**
79+
* 2. Owner-Based Sharing Rule
80+
* Share records owned by a specific group of users.
81+
*/
82+
export const OwnerSharingRuleSchema = BaseSharingRuleSchema.extend({
83+
type: z.literal('owner'),
84+
ownedBy: z.object({
85+
type: ShareRecipientType,
86+
value: z.string(),
87+
}).describe('Source group/role whose records are being shared'),
88+
});
89+
90+
/**
91+
* Master Sharing Rule Schema
92+
*/
93+
export const SharingRuleSchema: z.ZodType<any> = z.discriminatedUnion('type', [
94+
CriteriaSharingRuleSchema,
95+
OwnerSharingRuleSchema
6396
]);
6497

6598
export type SharingRule = z.infer<typeof SharingRuleSchema>;
66-
export type SharingRuleType = z.infer<typeof SharingRuleType>;
99+
export type CriteriaSharingRule = z.infer<typeof CriteriaSharingRuleSchema>;
100+
export type OwnerSharingRule = z.infer<typeof OwnerSharingRuleSchema>;

0 commit comments

Comments
 (0)