|
1 | 1 | import { z } from 'zod'; |
2 | 2 | import { SnakeCaseIdentifierSchema } from '../shared/identifiers.zod'; |
3 | | - |
4 | | -/** |
5 | | - * Row-Level Security Rule Schema (Simplified) |
6 | | - * |
7 | | - * Simplified RLS rule definition that can be embedded in permission sets. |
8 | | - * For comprehensive RLS features, see ../permission/rls.zod.ts |
9 | | - * |
10 | | - * This schema allows permission sets to include basic row-level filters |
11 | | - * that restrict data access based on user context. |
12 | | - * |
13 | | - * @example Tenant isolation rule |
14 | | - * ```typescript |
15 | | - * { |
16 | | - * name: 'tenant_isolation', |
17 | | - * objectName: 'account', |
18 | | - * operation: 'read', |
19 | | - * filter: { |
20 | | - * field: 'tenant_id', |
21 | | - * operator: 'eq', |
22 | | - * value: { contextVariable: 'current_user.tenant_id' } |
23 | | - * }, |
24 | | - * enabled: true, |
25 | | - * priority: 0 |
26 | | - * } |
27 | | - * ``` |
28 | | - */ |
29 | | -export const RLSRuleSchema = z.object({ |
30 | | - name: z.string() |
31 | | - .regex(/^[a-z_][a-z0-9_]*$/) |
32 | | - .describe('Rule unique identifier (snake_case)'), |
33 | | - objectName: z.string().describe('Target object name'), |
34 | | - operation: z.enum(['read', 'create', 'update', 'delete']) |
35 | | - .describe('Database operation this rule applies to'), |
36 | | - filter: z.object({ |
37 | | - field: z.string().describe('Field name to filter on'), |
38 | | - operator: z.enum(['eq', 'ne', 'in', 'nin', 'gt', 'gte', 'lt', 'lte']) |
39 | | - .describe('Filter operator'), |
40 | | - value: z.union([ |
41 | | - z.string(), |
42 | | - z.number(), |
43 | | - z.boolean(), |
44 | | - z.array(z.any()), |
45 | | - z.object({ contextVariable: z.string() }) |
46 | | - .describe('Reference to context variable (e.g., { contextVariable: "current_user.tenant_id" })'), |
47 | | - ]).describe('Filter value or context variable reference'), |
48 | | - }).describe('Filter condition for row-level access'), |
49 | | - enabled: z.boolean().default(true).describe('Whether this rule is active'), |
50 | | - priority: z.number().default(0).describe('Rule evaluation priority (higher = evaluated first)'), |
51 | | -}); |
52 | | - |
53 | | -export type RLSRule = z.infer<typeof RLSRuleSchema>; |
| 3 | +import { RowLevelSecurityPolicySchema } from './rls.zod'; |
54 | 4 |
|
55 | 5 | /** |
56 | 6 | * Entity (Object) Level Permissions |
@@ -146,26 +96,27 @@ export const PermissionSetSchema = z.object({ |
146 | 96 | /** |
147 | 97 | * Row-Level Security Rules |
148 | 98 | * |
149 | | - * Simplified RLS rules that filter records based on user context. |
| 99 | + * Row-level security policies that filter records based on user context. |
150 | 100 | * These rules are applied in addition to object-level permissions. |
151 | 101 | * |
152 | | - * For comprehensive RLS features, use the dedicated RLS protocol in ../permission/rls.zod.ts |
| 102 | + * Uses the canonical RLS protocol from rls.zod.ts for comprehensive |
| 103 | + * row-level security features including PostgreSQL-style USING and CHECK clauses. |
| 104 | + * |
| 105 | + * @see {@link RowLevelSecurityPolicySchema} for full RLS specification |
| 106 | + * @see {@link file://../permission/rls.zod.ts} for comprehensive RLS documentation |
153 | 107 | * |
154 | 108 | * @example Multi-tenant isolation |
155 | 109 | * ```typescript |
156 | 110 | * rls: [{ |
157 | 111 | * name: 'tenant_filter', |
158 | | - * objectName: 'account', |
159 | | - * operation: 'read', |
160 | | - * filter: { |
161 | | - * field: 'tenant_id', |
162 | | - * operator: 'eq', |
163 | | - * value: { contextVariable: 'current_user.tenant_id' } |
164 | | - * } |
| 112 | + * object: 'account', |
| 113 | + * operation: 'select', |
| 114 | + * using: 'tenant_id = current_user.tenant_id' |
165 | 115 | * }] |
166 | 116 | * ``` |
167 | 117 | */ |
168 | | - rls: z.array(RLSRuleSchema).optional().describe('Row-level security rules'), |
| 118 | + rowLevelSecurity: z.array(RowLevelSecurityPolicySchema).optional() |
| 119 | + .describe('Row-level security policies (see rls.zod.ts for full spec)'), |
169 | 120 |
|
170 | 121 | /** |
171 | 122 | * Context-Based Access Control Variables |
|
0 commit comments