@memberjunction/record-cloning 0.0.1-seed.1 → 6.2.0-edge.2

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/LICENSE +183 -0
  2. package/README.md +98 -3
  3. package/dist/CloneAuthorization.d.ts +50 -0
  4. package/dist/CloneAuthorization.d.ts.map +1 -0
  5. package/dist/CloneAuthorization.js +75 -0
  6. package/dist/CloneAuthorization.js.map +1 -0
  7. package/dist/CloneExecutor.d.ts +50 -0
  8. package/dist/CloneExecutor.d.ts.map +1 -0
  9. package/dist/CloneExecutor.js +350 -0
  10. package/dist/CloneExecutor.js.map +1 -0
  11. package/dist/CloneKeys.d.ts +65 -0
  12. package/dist/CloneKeys.d.ts.map +1 -0
  13. package/dist/CloneKeys.js +118 -0
  14. package/dist/CloneKeys.js.map +1 -0
  15. package/dist/CloneMaterializer.d.ts +52 -0
  16. package/dist/CloneMaterializer.d.ts.map +1 -0
  17. package/dist/CloneMaterializer.js +267 -0
  18. package/dist/CloneMaterializer.js.map +1 -0
  19. package/dist/ClonePlanHash.d.ts +11 -0
  20. package/dist/ClonePlanHash.d.ts.map +1 -0
  21. package/dist/ClonePlanHash.js +16 -0
  22. package/dist/ClonePlanHash.js.map +1 -0
  23. package/dist/ClonePlanner.d.ts +33 -0
  24. package/dist/ClonePlanner.d.ts.map +1 -0
  25. package/dist/ClonePlanner.js +870 -0
  26. package/dist/ClonePlanner.js.map +1 -0
  27. package/dist/CloneRecordProcessor.d.ts +28 -0
  28. package/dist/CloneRecordProcessor.d.ts.map +1 -0
  29. package/dist/CloneRecordProcessor.js +101 -0
  30. package/dist/CloneRecordProcessor.js.map +1 -0
  31. package/dist/RecordCloneEngine.d.ts +35 -0
  32. package/dist/RecordCloneEngine.d.ts.map +1 -0
  33. package/dist/RecordCloneEngine.js +60 -0
  34. package/dist/RecordCloneEngine.js.map +1 -0
  35. package/dist/RecordCloningStartup.d.ts +14 -0
  36. package/dist/RecordCloningStartup.d.ts.map +1 -0
  37. package/dist/RecordCloningStartup.js +32 -0
  38. package/dist/RecordCloningStartup.js.map +1 -0
  39. package/dist/__tests__/helpers/cloneAuthorizations.d.ts +7 -0
  40. package/dist/__tests__/helpers/cloneAuthorizations.d.ts.map +1 -0
  41. package/dist/__tests__/helpers/cloneAuthorizations.js +17 -0
  42. package/dist/__tests__/helpers/cloneAuthorizations.js.map +1 -0
  43. package/dist/index.d.ts +15 -0
  44. package/dist/index.d.ts.map +1 -0
  45. package/dist/index.js +15 -0
  46. package/dist/index.js.map +1 -0
  47. package/dist/operations.d.ts +64 -0
  48. package/dist/operations.d.ts.map +1 -0
  49. package/dist/operations.js +390 -0
  50. package/dist/operations.js.map +1 -0
  51. package/package.json +29 -5
package/LICENSE ADDED
@@ -0,0 +1,183 @@
1
+ Business Source License 1.1
2
+
3
+ License text copyright (c) 2024 MariaDB plc, All Rights Reserved.
4
+ "Business Source License" is a trademark of MariaDB plc.
5
+
6
+ -----------------------------------------------------------------------------
7
+
8
+ Parameters
9
+
10
+ Licensor: Blue Cypress, Inc.
11
+
12
+ Licensed Work: MemberJunction.
13
+ The Licensed Work is (c) 2023-2026 Blue Cypress, Inc.
14
+
15
+ Additional Use Grant: Subject to the terms of this License, Licensor grants
16
+ you the following additional rights to make Production
17
+ Use of the Licensed Work.
18
+
19
+ 1. Internal Use
20
+
21
+ You may make production use of the Licensed Work for
22
+ your own internal business or organizational operations.
23
+
24
+ 2. Nonprofit Use
25
+
26
+ If you are a Nonprofit, you may make production use of
27
+ the Licensed Work for the operations and activities of
28
+ your Organizational Family.
29
+
30
+ 3. MemberJunction Certified Program Use
31
+
32
+ If you are authorized by Licensor under the
33
+ MemberJunction Certified Program to provide professional
34
+ services using the Licensed Work, you may make
35
+ production use of the Licensed Work in providing such
36
+ professional services to a client, provided that:
37
+
38
+ (a) the Licensed Work is deployed in, and the applicable
39
+ production use occurs within, an environment owned,
40
+ leased, licensed, subscribed to, or otherwise controlled
41
+ by that client; and
42
+
43
+ (b) the production use is for that client's own internal
44
+ business or organizational operations or is otherwise
45
+ independently permitted to that client under this
46
+ Additional Use Grant.
47
+
48
+ 4. Definitions Applicable to the Additional Use Grant
49
+
50
+ "Affiliate" means, with respect to a specified Person,
51
+ any other Person that directly or indirectly Controls,
52
+ is Controlled by, or is under common Control with such
53
+ specified Person.
54
+
55
+ "Control" (including the terms "Controls," "Controlled
56
+ by," and "under common Control with") means the direct
57
+ or indirect possession of the power to direct or cause
58
+ the direction of the management and policies of a
59
+ Person, whether through ownership of voting interests,
60
+ by contract, or otherwise.
61
+
62
+ "Organizational Family" means, with respect to a Person,
63
+ (a) such Person and its Affiliates, and (b) any
64
+ nonprofit organization, governmental entity, chapter,
65
+ division, local affiliate, regional affiliate, state
66
+ affiliate, national affiliate, or other entity that is
67
+ formally affiliated with such Person through governing
68
+ documents, a charter, bylaws, a membership agreement, or
69
+ another written organizational instrument, and is
70
+ recognized under such documents as part of the same
71
+ organizational structure.
72
+
73
+ "Nonprofit" means a Person recognized by the Internal
74
+ Revenue Service as exempt from federal income taxation
75
+ under Section 501(c)(3), 501(c)(4), 501(c)(5), or
76
+ 501(c)(6) of the Internal Revenue Code, or a foreign
77
+ organization recognized under substantially equivalent
78
+ laws.
79
+
80
+ A Person claiming eligibility as a Nonprofit shall, upon
81
+ Licensor's reasonable request, provide documentation
82
+ reasonably sufficient to demonstrate that it qualifies
83
+ as a Nonprofit. If such Person materially misrepresents,
84
+ or is unable to demonstrate, its qualification as a
85
+ Nonprofit, the rights granted to such Person under
86
+ Section 2 of this Additional Use Grant shall terminate.
87
+
88
+ "MemberJunction Certified Program" means Licensor's
89
+ then-current program for certifying and authorizing a
90
+ Person to provide professional services using the
91
+ Licensed Work.
92
+
93
+ "Person" means any individual, corporation, limited
94
+ liability company, partnership, association, nonprofit
95
+ organization, governmental entity, or other legal or
96
+ organizational entity.
97
+
98
+ Change Date: Four (4) years from the date the Licensed Work is first
99
+ made available.
100
+
101
+ Change License: MIT License.
102
+
103
+ For information about alternative licensing arrangements for the Licensed
104
+ Work, please contact Blue Cypress, Inc.
105
+
106
+ -----------------------------------------------------------------------------
107
+
108
+ Terms
109
+
110
+ The Licensor hereby grants you the right to copy, modify, create derivative
111
+ works, redistribute, and make non-production use of the Licensed Work. The
112
+ Licensor may make an Additional Use Grant, above, permitting limited
113
+ production use.
114
+
115
+ Effective on the Change Date, or the fourth anniversary of the first publicly
116
+ available distribution of a specific version of the Licensed Work under this
117
+ License, whichever comes first, the Licensor hereby grants you rights under
118
+ the terms of the Change License, and the rights granted in the paragraph
119
+ above terminate.
120
+
121
+ If your use of the Licensed Work does not comply with the requirements
122
+ currently in effect as described in this License, you must purchase a
123
+ commercial license from the Licensor, its affiliated entities, or authorized
124
+ resellers, or you must refrain from using the Licensed Work.
125
+
126
+ All copies of the original and modified Licensed Work, and derivative works
127
+ of the Licensed Work, are subject to this License. This License applies
128
+ separately for each version of the Licensed Work and the Change Date may vary
129
+ for each version of the Licensed Work released by Licensor.
130
+
131
+ You must conspicuously display this License on each original or modified copy
132
+ of the Licensed Work. If you receive the Licensed Work in original or
133
+ modified form from a third party, the terms and conditions set forth in this
134
+ License apply to your use of that work.
135
+
136
+ Any use of the Licensed Work in violation of this License will automatically
137
+ terminate your rights under this License for the current and all other
138
+ versions of the Licensed Work.
139
+
140
+ This License does not grant you any right in any trademark or logo of
141
+ Licensor or its affiliates (provided that you may use a trademark or logo of
142
+ Licensor as expressly required by this License).
143
+
144
+ TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
145
+ AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
146
+ EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
147
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
148
+ TITLE.
149
+
150
+ MariaDB hereby grants you permission to use this License's text to license
151
+ your works, and to refer to it using the trademark "Business Source License",
152
+ as long as you comply with the Covenants of Licensor below.
153
+
154
+ -----------------------------------------------------------------------------
155
+
156
+ Covenants of Licensor
157
+
158
+ In consideration of the right to use this License's text and the "Business
159
+ Source License" name and trademark, Licensor covenants to MariaDB, and to all
160
+ other recipients of the licensed work to be provided by Licensor:
161
+
162
+ 1. To specify as the Change License the GPL Version 2.0 or any later version,
163
+ or a license that is compatible with GPL Version 2.0 or a later version,
164
+ where "compatible" means that software provided under the Change License
165
+ can be included in a program with software provided under GPL Version 2.0
166
+ or a later version. Licensor may specify additional Change Licenses without
167
+ limitation.
168
+
169
+ 2. To either: (a) specify an additional grant of rights to use that does not
170
+ impose any additional restriction on the right granted in this License, as
171
+ the Additional Use Grant; or (b) insert the text "None".
172
+
173
+ 3. To specify a Change Date.
174
+
175
+ 4. Not to modify this License in any other way.
176
+
177
+ -----------------------------------------------------------------------------
178
+
179
+ Notice
180
+
181
+ The Business Source License (this document, or the "License") is not an Open
182
+ Source license. However, the Licensed Work will eventually be made available
183
+ under an Open Source License, as stated in this License.
package/README.md CHANGED
@@ -1,4 +1,99 @@
1
1
  # @memberjunction/record-cloning
2
- Seed release published over OIDC to prove trusted publishing is configured for
3
- this package. It contains no code and is tagged `seed`, never `latest`.
4
- Real releases are published by .github/workflows/publish.yml.
2
+
3
+ Server-side engine for cloning an entity record together with the graph of records around it:
4
+ owned child rows and the references it points at. (A Deep IS-A subtype or embedded row blocks the
5
+ plan with `EDGE_KIND_UNSUPPORTED` in this release.) It copies what the entity's
6
+ `Configuration.Clone` bag allows, re-points foreign keys at the new rows, and saves everything in
7
+ one transaction with full provenance.
8
+
9
+ Pure logic lives in [`@memberjunction/record-cloning-base`](../base/README.md): policy resolution,
10
+ the field mapper, naming, JSON remap, validation and the contract types. Graph traversal comes from
11
+ [`@memberjunction/record-graph`](../../RecordGraph/README.md). The design is in
12
+ [`plans/record-cloning/README.md`](../../../plans/record-cloning/README.md).
13
+
14
+ ## Flow: Describe → Plan → Execute
15
+
16
+ | Step | What happens | Writes |
17
+ |---|---|---|
18
+ | **Describe** | Whether the user may clone this entity and what the UI may offer: presets, `UserEditable`, relationships with their default policy, and the Fire Hooks / Override Scope flags. | nothing |
19
+ | **Plan** | `ClonePlanner` walks the graph (`DependencyGraphWalker`), resolves each edge to `Deep`, `Reference` or `Skip`, runs the field pipeline on every row, applies `MaxDepth`/`MaxRecords` caps and authorization checks, and hashes the result. | nothing |
20
+ | **Execute** | Plans again. Refuses with `BLOCKED`/`FORBIDDEN` when the plan is blocked, or `PLAN_CHANGED` when the hash differs from the `ExpectedPlanHash` the user reviewed. Otherwise `CloneMaterializer` builds the in-memory graph and `CloneExecutor` saves it. | the clone and its provenance |
21
+
22
+ - **`ClonePlanner`** produces a deterministic `ClonePlan` (nodes, edges, field changes, warnings, `Blocked`).
23
+ - **`CloneMaterializer`** turns the plan into unsaved entity objects: the root, children in the
24
+ parent's writable collection (or one declared with `BaseEntity.DeclareRelatedRecordsDynamic`),
25
+ prerequisites for forward-FK targets, and sidecars.
26
+ - **`CloneExecutor`** sets a clone context on each staged row (`SetCloneContext`), then saves
27
+ prerequisites, `root.Save()` and sidecars inside `RunInEntityTransaction`, followed by the provenance.
28
+
29
+ ## Entry points
30
+
31
+ **Remote operations** (`src/operations.ts`): `RecordClone.Describe`, `RecordClone.Plan`,
32
+ `RecordClone.Execute` and `RecordClone.GetLineage`. Each server class extends the operation class
33
+ CodeGen emits into `@memberjunction/core-entities`. The input/output contracts are the types in
34
+ `metadata/remote-operations/types/record-clone-*.ts`. `Plan` output masks encrypted values.
35
+ `GetLineage` reads `ClonedFrom` record links in both directions.
36
+
37
+ **Programmatic API**, server-side:
38
+
39
+ ```typescript
40
+ import { LogError } from '@memberjunction/core';
41
+ import { RecordCloneEngine } from '@memberjunction/record-cloning';
42
+
43
+ const engine = new RecordCloneEngine(provider);
44
+ const plan = await engine.Plan({ EntityName: 'MJ: AI Agents', SourceRecordKey: key }, contextUser);
45
+ if (!plan.Blocked) {
46
+ const result = await engine.Clone(
47
+ { EntityName: 'MJ: AI Agents', SourceRecordKey: key, Options: { Preset: 'deep-prompts' } },
48
+ contextUser
49
+ );
50
+ if (!result.Success) LogError(result.ErrorMessage);
51
+ }
52
+ ```
53
+
54
+ `Clone` returns the plan without writing anything when `Options.DryRun` is true. `Describe` and
55
+ `GetLineage` are also available.
56
+
57
+ **Actions** (`packages/Actions/CoreActions/src/custom/record-cloning/`):
58
+ - `Clone Record`: `EntityName`, `RecordID`, optional `Options` JSON and `DryRun`. Outputs
59
+ `NewRecordID`, `CloneLogID`, `CreatedCount` and `Warnings`.
60
+ - `Clone Records`: takes `RecordIDs` instead and requires `Clone Records: Batch`. Each root is its
61
+ own clone and transaction. The `Results` output reports each root, and the result code is
62
+ `PARTIAL` when only some roots succeed.
63
+
64
+ **Record processes**: the `Clone` work type is `CloneRecordProcessor`. `RecordCloningStartup`
65
+ (`@RegisterForStartup`) registers it with `RecordProcessorRegistry` at server boot. The run's
66
+ `Configuration` JSON supplies `Options`, `EdgeOverrides`, `FieldOverrides` and `DryRun`. It requires
67
+ `Clone Records: Batch`.
68
+
69
+ ## Authorization (`CloneAuthorization.ts`)
70
+
71
+ `CloneAuthorizer` checks every created row against its entity's clone authorization. That is
72
+ `Configuration.Clone.RequiredAuthorization` if set. Otherwise it is `Clone Records: <Entity Name>`
73
+ if that authorization exists, and otherwise `Clone Records in Platform Schema` (for `__mj`) or
74
+ `Clone Records in Custom Schemas`. Checks walk ancestors, so a holder of `Clone Records` passes all
75
+ of them. `Clone Records: Fire Hooks` lets a request change whether Entity Actions and AI Actions fire.
76
+ `Clone Records: Override Scope` lets a request widen a clone with a `Deep` override. An authorization
77
+ missing from metadata grants nothing. The planner also requires `CanCreate` on every created entity and
78
+ the root's `RequiredUserType`, if set.
79
+
80
+ ## Provenance
81
+
82
+ A successful clone writes, in the same transaction:
83
+ - a Record Change per new row (for entities that track record changes), with `Source = 'Clone'` and `ChangeContext` JSON
84
+ - one `MJ: Record Clone Logs` header (masked plan, options, counts, `InitiatedByUserID` = the cloner)
85
+ - one `MJ: Record Clone Log Items` row per planned node (`Created` / `Referenced` / `Skipped`)
86
+ - one `MJ: Record Links` row per new record, with `LinkType = 'ClonedFrom'` (Source = clone, Target = original)
87
+ - a `Record Cloned` audit log entry, when the entity's clone authorization has `UseAuditLog` (every shipped one does)
88
+
89
+ If the log or a link fails to save, the whole clone rolls back. The log, log items and links are
90
+ saved as the **system user** when one is available, because they are the platform's record, not the
91
+ cloner's data. The audit entry is recorded as the cloner. Refused and failed runs still get a log
92
+ (`Cancelled` / `Error`) and, under the same `UseAuditLog` rule, an audit entry, written outside any transaction.
93
+
94
+ ## Transactions
95
+
96
+ A clone is all or nothing. If the provider lacks `SupportsEntityTransactions` and
97
+ `BeginEntityTransaction`, `Execute` refuses before writing anything and returns `EXECUTION_ERROR`.
98
+ It still records a refusal log. Run clones on the server, through the remote operations, the
99
+ actions or a record process.
@@ -0,0 +1,50 @@
1
+ /**
2
+ * @file CloneAuthorization.ts
3
+ * Server-side authorization checks for record cloning.
4
+ *
5
+ * Resolution for an entity (plan §9.1): `Configuration.Clone.RequiredAuthorization` when set;
6
+ * else the per-entity leaf `Clone Records: <Entity Name>` when one exists; else the schema
7
+ * node (`Clone Records in Platform Schema` for `__mj`, `Clone Records in Custom Schemas`
8
+ * otherwise). Every check walks ancestors, so a `Clone Records` holder passes them all.
9
+ *
10
+ * @see plans/record-cloning/README.md §9
11
+ */
12
+ import { AuthorizationInfo, EntityInfo, IMetadataProvider, UserInfo } from '@memberjunction/core';
13
+ /** Name of the authorization that allows firing Entity Actions / AI Actions during a clone. */
14
+ export declare const FIRE_HOOKS_AUTHORIZATION = "Clone Records: Fire Hooks";
15
+ /** Name of the authorization that allows widening a clone beyond the entity's configured scope. */
16
+ export declare const OVERRIDE_SCOPE_AUTHORIZATION = "Clone Records: Override Scope";
17
+ /** Name of the authorization that allows cloning several records in one request (the `Clone Records` action). */
18
+ export declare const BATCH_AUTHORIZATION = "Clone Records: Batch";
19
+ /** Outcome of one authorization check, in the shape `RecordClone.Describe` returns. */
20
+ export interface CloneAuthorizationResult {
21
+ /** The authorization that was checked. */
22
+ Name: string;
23
+ /** Whether the user holds it (directly or through an ancestor). */
24
+ Granted: boolean;
25
+ }
26
+ /**
27
+ * Evaluates the clone authorizations for one provider. Create one per request; it only reads
28
+ * the provider's cached `Authorizations` list.
29
+ */
30
+ export declare class CloneAuthorizer {
31
+ private readonly provider;
32
+ private readonly evaluator;
33
+ constructor(provider: IMetadataProvider);
34
+ /** The authorization that governs creating rows of `entity` in a clone. */
35
+ ResolveAuthorizationName(entity: EntityInfo): string;
36
+ /** Whether `user` may clone rows of `entity`. */
37
+ CanCloneEntity(entity: EntityInfo, user: UserInfo): CloneAuthorizationResult;
38
+ /** Whether `user` may fire Entity Actions and AI Actions during a clone. */
39
+ CanFireHooks(user: UserInfo): boolean;
40
+ /** Whether `user` may widen a clone beyond the entity's configured scope. */
41
+ CanOverrideScope(user: UserInfo): boolean;
42
+ /** Whether `user` may clone several records in one request. */
43
+ CanBatchClone(user: UserInfo): boolean;
44
+ /** An authorization missing from metadata grants nothing: the check fails closed. */
45
+ private userHolds;
46
+ /** The authorization a clone of `entity` is checked against, or undefined when it isn't seeded. */
47
+ EntityAuthorization(entity: EntityInfo): AuthorizationInfo | undefined;
48
+ private find;
49
+ }
50
+ //# sourceMappingURL=CloneAuthorization.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CloneAuthorization.d.ts","sourceRoot":"","sources":["../src/CloneAuthorization.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAA0B,iBAAiB,EAAE,UAAU,EAAE,iBAAiB,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAE1H,+FAA+F;AAC/F,eAAO,MAAM,wBAAwB,8BAA8B,CAAC;AAEpE,mGAAmG;AACnG,eAAO,MAAM,4BAA4B,kCAAkC,CAAC;AAE5E,iHAAiH;AACjH,eAAO,MAAM,mBAAmB,yBAAyB,CAAC;AAK1D,uFAAuF;AACvF,MAAM,WAAW,wBAAwB;IACrC,0CAA0C;IAC1C,IAAI,EAAE,MAAM,CAAC;IACb,mEAAmE;IACnE,OAAO,EAAE,OAAO,CAAC;CACpB;AAED;;;GAGG;AACH,qBAAa,eAAe;IAGL,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAF5C,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAgC;gBAEtB,QAAQ,EAAE,iBAAiB;IAE/D,2EAA2E;IACpE,wBAAwB,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM;IAY3D,iDAAiD;IAC1C,cAAc,CAAC,MAAM,EAAE,UAAU,EAAE,IAAI,EAAE,QAAQ,GAAG,wBAAwB;IAKnF,4EAA4E;IACrE,YAAY,CAAC,IAAI,EAAE,QAAQ,GAAG,OAAO;IAI5C,6EAA6E;IACtE,gBAAgB,CAAC,IAAI,EAAE,QAAQ,GAAG,OAAO;IAIhD,+DAA+D;IACxD,aAAa,CAAC,IAAI,EAAE,QAAQ,GAAG,OAAO;IAI7C,qFAAqF;IACrF,OAAO,CAAC,SAAS;IAMjB,mGAAmG;IAC5F,mBAAmB,CAAC,MAAM,EAAE,UAAU,GAAG,iBAAiB,GAAG,SAAS;IAI7E,OAAO,CAAC,IAAI;CAIf"}
@@ -0,0 +1,75 @@
1
+ /**
2
+ * @file CloneAuthorization.ts
3
+ * Server-side authorization checks for record cloning.
4
+ *
5
+ * Resolution for an entity (plan §9.1): `Configuration.Clone.RequiredAuthorization` when set;
6
+ * else the per-entity leaf `Clone Records: <Entity Name>` when one exists; else the schema
7
+ * node (`Clone Records in Platform Schema` for `__mj`, `Clone Records in Custom Schemas`
8
+ * otherwise). Every check walks ancestors, so a `Clone Records` holder passes them all.
9
+ *
10
+ * @see plans/record-cloning/README.md §9
11
+ */
12
+ import { AuthorizationEvaluator } from '@memberjunction/core';
13
+ /** Name of the authorization that allows firing Entity Actions / AI Actions during a clone. */
14
+ export const FIRE_HOOKS_AUTHORIZATION = 'Clone Records: Fire Hooks';
15
+ /** Name of the authorization that allows widening a clone beyond the entity's configured scope. */
16
+ export const OVERRIDE_SCOPE_AUTHORIZATION = 'Clone Records: Override Scope';
17
+ /** Name of the authorization that allows cloning several records in one request (the `Clone Records` action). */
18
+ export const BATCH_AUTHORIZATION = 'Clone Records: Batch';
19
+ /** Schema that holds MemberJunction's own entities. */
20
+ const PLATFORM_SCHEMA = '__mj';
21
+ /**
22
+ * Evaluates the clone authorizations for one provider. Create one per request; it only reads
23
+ * the provider's cached `Authorizations` list.
24
+ */
25
+ export class CloneAuthorizer {
26
+ constructor(provider) {
27
+ this.provider = provider;
28
+ this.evaluator = new AuthorizationEvaluator();
29
+ }
30
+ /** The authorization that governs creating rows of `entity` in a clone. */
31
+ ResolveAuthorizationName(entity) {
32
+ const configured = entity.CloneConfig?.RequiredAuthorization?.trim();
33
+ if (configured)
34
+ return configured;
35
+ const leaf = `Clone Records: ${entity.Name}`;
36
+ if (this.find(leaf))
37
+ return leaf;
38
+ return entity.SchemaName?.trim().toLowerCase() === PLATFORM_SCHEMA
39
+ ? 'Clone Records in Platform Schema'
40
+ : 'Clone Records in Custom Schemas';
41
+ }
42
+ /** Whether `user` may clone rows of `entity`. */
43
+ CanCloneEntity(entity, user) {
44
+ const name = this.ResolveAuthorizationName(entity);
45
+ return { Name: name, Granted: this.userHolds(name, user) };
46
+ }
47
+ /** Whether `user` may fire Entity Actions and AI Actions during a clone. */
48
+ CanFireHooks(user) {
49
+ return this.userHolds(FIRE_HOOKS_AUTHORIZATION, user);
50
+ }
51
+ /** Whether `user` may widen a clone beyond the entity's configured scope. */
52
+ CanOverrideScope(user) {
53
+ return this.userHolds(OVERRIDE_SCOPE_AUTHORIZATION, user);
54
+ }
55
+ /** Whether `user` may clone several records in one request. */
56
+ CanBatchClone(user) {
57
+ return this.userHolds(BATCH_AUTHORIZATION, user);
58
+ }
59
+ /** An authorization missing from metadata grants nothing: the check fails closed. */
60
+ userHolds(name, user) {
61
+ const auth = this.find(name);
62
+ if (!auth)
63
+ return false;
64
+ return this.evaluator.UserCanExecuteWithAncestors(auth, user, this.provider.Authorizations ?? []);
65
+ }
66
+ /** The authorization a clone of `entity` is checked against, or undefined when it isn't seeded. */
67
+ EntityAuthorization(entity) {
68
+ return this.find(this.ResolveAuthorizationName(entity));
69
+ }
70
+ find(name) {
71
+ const target = name.trim().toLowerCase();
72
+ return (this.provider.Authorizations ?? []).find((a) => a.Name?.trim().toLowerCase() === target);
73
+ }
74
+ }
75
+ //# sourceMappingURL=CloneAuthorization.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CloneAuthorization.js","sourceRoot":"","sources":["../src/CloneAuthorization.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,sBAAsB,EAA8D,MAAM,sBAAsB,CAAC;AAE1H,+FAA+F;AAC/F,MAAM,CAAC,MAAM,wBAAwB,GAAG,2BAA2B,CAAC;AAEpE,mGAAmG;AACnG,MAAM,CAAC,MAAM,4BAA4B,GAAG,+BAA+B,CAAC;AAE5E,iHAAiH;AACjH,MAAM,CAAC,MAAM,mBAAmB,GAAG,sBAAsB,CAAC;AAE1D,uDAAuD;AACvD,MAAM,eAAe,GAAG,MAAM,CAAC;AAU/B;;;GAGG;AACH,MAAM,OAAO,eAAe;IAGxB,YAAoC,QAA2B;QAA3B,aAAQ,GAAR,QAAQ,CAAmB;QAF9C,cAAS,GAAG,IAAI,sBAAsB,EAAE,CAAC;IAEQ,CAAC;IAEnE,2EAA2E;IACpE,wBAAwB,CAAC,MAAkB;QAC9C,MAAM,UAAU,GAAG,MAAM,CAAC,WAAW,EAAE,qBAAqB,EAAE,IAAI,EAAE,CAAC;QACrE,IAAI,UAAU;YAAE,OAAO,UAAU,CAAC;QAElC,MAAM,IAAI,GAAG,kBAAkB,MAAM,CAAC,IAAI,EAAE,CAAC;QAC7C,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC;YAAE,OAAO,IAAI,CAAC;QAEjC,OAAO,MAAM,CAAC,UAAU,EAAE,IAAI,EAAE,CAAC,WAAW,EAAE,KAAK,eAAe;YAC9D,CAAC,CAAC,kCAAkC;YACpC,CAAC,CAAC,iCAAiC,CAAC;IAC5C,CAAC;IAED,iDAAiD;IAC1C,cAAc,CAAC,MAAkB,EAAE,IAAc;QACpD,MAAM,IAAI,GAAG,IAAI,CAAC,wBAAwB,CAAC,MAAM,CAAC,CAAC;QACnD,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,CAAC;IAC/D,CAAC;IAED,4EAA4E;IACrE,YAAY,CAAC,IAAc;QAC9B,OAAO,IAAI,CAAC,SAAS,CAAC,wBAAwB,EAAE,IAAI,CAAC,CAAC;IAC1D,CAAC;IAED,6EAA6E;IACtE,gBAAgB,CAAC,IAAc;QAClC,OAAO,IAAI,CAAC,SAAS,CAAC,4BAA4B,EAAE,IAAI,CAAC,CAAC;IAC9D,CAAC;IAED,+DAA+D;IACxD,aAAa,CAAC,IAAc;QAC/B,OAAO,IAAI,CAAC,SAAS,CAAC,mBAAmB,EAAE,IAAI,CAAC,CAAC;IACrD,CAAC;IAED,qFAAqF;IAC7E,SAAS,CAAC,IAAY,EAAE,IAAc;QAC1C,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC7B,IAAI,CAAC,IAAI;YAAE,OAAO,KAAK,CAAC;QACxB,OAAO,IAAI,CAAC,SAAS,CAAC,2BAA2B,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,cAAc,IAAI,EAAE,CAAC,CAAC;IACtG,CAAC;IAED,mGAAmG;IAC5F,mBAAmB,CAAC,MAAkB;QACzC,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,wBAAwB,CAAC,MAAM,CAAC,CAAC,CAAC;IAC5D,CAAC;IAEO,IAAI,CAAC,IAAY;QACrB,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;QACzC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,cAAc,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,WAAW,EAAE,KAAK,MAAM,CAAC,CAAC;IACrG,CAAC;CACJ"}
@@ -0,0 +1,50 @@
1
+ /**
2
+ * @file CloneExecutor.ts
3
+ * Executes deterministic clone plans inside an atomic database transaction.
4
+ * Materializes graphs, verifies plan hash stability, enforces root.Save() arbitration,
5
+ * and writes provenance: Record Changes, a Clone Log with one item per planned row, ClonedFrom
6
+ * Record Links, and a Record Cloned audit entry. Refused and failed runs are logged too.
7
+ * @see plans/record-cloning/README.md §6, §8, §10, §13.1
8
+ */
9
+ import { IMetadataProvider, UserInfo } from '@memberjunction/core';
10
+ import { ClonePlan, RecordCloneResult } from '@memberjunction/record-cloning-base';
11
+ export interface CloneExecutorOptions {
12
+ Provider?: IMetadataProvider;
13
+ }
14
+ export declare class CloneExecutor {
15
+ private _provider?;
16
+ constructor(options?: CloneExecutorOptions);
17
+ protected get Provider(): IMetadataProvider;
18
+ /**
19
+ * Executes a ClonePlan atomically.
20
+ */
21
+ Execute(plan: ClonePlan, contextUser: UserInfo): Promise<RecordCloneResult>;
22
+ /**
23
+ * Save options for the clone's own rows, from the plan's effective options: Entity Actions run
24
+ * by default and AI Actions don't, unless the entity's configuration (or a Fire Hooks holder's
25
+ * request) says otherwise.
26
+ */
27
+ private static saveOptionsFor;
28
+ /**
29
+ * Records a refused run (blocked, or the plan changed since review) as a Cancelled clone log,
30
+ * so every Execute leaves a trace. Written outside any transaction; never throws.
31
+ * @returns the log's ID, or null when it could not be written.
32
+ */
33
+ WriteRefusalLog(plan: ClonePlan, contextUser: UserInfo, reason: string): Promise<string | null>;
34
+ /**
35
+ * The identity provenance rows are saved as. Logs and lineage links are the platform's record
36
+ * of what happened, not the cloner's own data (the UI role may only read them), so they are
37
+ * written as the system user when this process has one. `InitiatedByUserID` still names the cloner.
38
+ */
39
+ private provenanceUser;
40
+ /** Clone log, one log item per plan node, ClonedFrom links, and the audit entry. Throws if the log or a link can't be saved. */
41
+ private writeSuccessProvenance;
42
+ /**
43
+ * Saves one clone log header. Returns whether it saved; failures are logged, never thrown, so the
44
+ * failure and refusal paths can call it after a rollback.
45
+ */
46
+ private writeLog;
47
+ /** One `Record Cloned` audit entry per Execute, as the cloner. Best effort, like all audit logging. */
48
+ private writeAudit;
49
+ }
50
+ //# sourceMappingURL=CloneExecutor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CloneExecutor.d.ts","sourceRoot":"","sources":["../src/CloneExecutor.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAMH,iBAAiB,EAIjB,QAAQ,EAEX,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EACH,SAAS,EAKT,iBAAiB,EACpB,MAAM,qCAAqC,CAAC;AAgC7C,MAAM,WAAW,oBAAoB;IACjC,QAAQ,CAAC,EAAE,iBAAiB,CAAC;CAChC;AAED,qBAAa,aAAa;IACtB,OAAO,CAAC,SAAS,CAAC,CAAoB;gBAEnB,OAAO,CAAC,EAAE,oBAAoB;IAIjD,SAAS,KAAK,QAAQ,IAAI,iBAAiB,CAE1C;IAED;;OAEG;IACU,OAAO,CAAC,IAAI,EAAE,SAAS,EAAE,WAAW,EAAE,QAAQ,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAmLxF;;;;OAIG;IACH,OAAO,CAAC,MAAM,CAAC,cAAc;IAO7B;;;;OAIG;IACU,eAAe,CAAC,IAAI,EAAE,SAAS,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IAO5G;;;;OAIG;YACW,cAAc;IAI5B,gIAAgI;YAClH,sBAAsB;IAsEpC;;;OAGG;YACW,QAAQ;IAmCtB,uGAAuG;YACzF,UAAU;CAqB3B"}