@snowtop/ent 0.1.0-alpha160-test4 → 0.1.0-alpha160-test6
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.
- package/dist/package.json +64 -0
- package/{scripts → dist/scripts}/custom_compiler.js +0 -0
- package/{scripts → dist/scripts}/custom_graphql.js +0 -0
- package/package.json +48 -6
- package/src/action/action.ts +330 -0
- package/src/action/executor.ts +453 -0
- package/src/action/experimental_action.ts +277 -0
- package/src/action/index.ts +31 -0
- package/src/action/operations.ts +967 -0
- package/src/action/orchestrator.ts +1527 -0
- package/src/action/privacy.ts +37 -0
- package/src/action/relative_value.ts +242 -0
- package/src/action/transaction.ts +38 -0
- package/src/auth/auth.ts +77 -0
- package/src/auth/index.ts +8 -0
- package/src/core/base.ts +367 -0
- package/src/core/clause.ts +1065 -0
- package/src/core/config.ts +219 -0
- package/src/core/const.ts +5 -0
- package/src/core/context.ts +135 -0
- package/src/core/convert.ts +106 -0
- package/src/core/date.ts +23 -0
- package/src/core/db.ts +498 -0
- package/src/core/ent.ts +1740 -0
- package/src/core/global_schema.ts +49 -0
- package/src/core/loaders/assoc_count_loader.ts +99 -0
- package/src/core/loaders/assoc_edge_loader.ts +250 -0
- package/src/core/loaders/index.ts +12 -0
- package/src/core/loaders/loader.ts +66 -0
- package/src/core/loaders/object_loader.ts +489 -0
- package/src/core/loaders/query_loader.ts +314 -0
- package/src/core/loaders/raw_count_loader.ts +175 -0
- package/src/core/logger.ts +49 -0
- package/src/core/privacy.ts +660 -0
- package/src/core/query/assoc_query.ts +240 -0
- package/src/core/query/custom_clause_query.ts +174 -0
- package/src/core/query/custom_query.ts +302 -0
- package/src/core/query/index.ts +9 -0
- package/src/core/query/query.ts +674 -0
- package/src/core/query_impl.ts +32 -0
- package/src/core/viewer.ts +52 -0
- package/src/ent.code-workspace +73 -0
- package/src/graphql/builtins/connection.ts +25 -0
- package/src/graphql/builtins/edge.ts +16 -0
- package/src/graphql/builtins/node.ts +12 -0
- package/src/graphql/graphql.ts +891 -0
- package/src/graphql/graphql_field_helpers.ts +221 -0
- package/src/graphql/index.ts +42 -0
- package/src/graphql/mutations/union.ts +39 -0
- package/src/graphql/node_resolver.ts +122 -0
- package/src/graphql/query/connection_type.ts +113 -0
- package/src/graphql/query/edge_connection.ts +171 -0
- package/src/graphql/query/page_info.ts +34 -0
- package/src/graphql/query/shared_edge_connection.ts +287 -0
- package/src/graphql/scalars/orderby_direction.ts +13 -0
- package/src/graphql/scalars/time.ts +38 -0
- package/src/imports/dataz/example1/_auth.ts +51 -0
- package/src/imports/dataz/example1/_viewer.ts +35 -0
- package/src/imports/index.ts +213 -0
- package/src/index.ts +145 -0
- package/src/parse_schema/parse.ts +585 -0
- package/src/schema/base_schema.ts +224 -0
- package/src/schema/field.ts +1087 -0
- package/src/schema/index.ts +53 -0
- package/src/schema/json_field.ts +94 -0
- package/src/schema/schema.ts +1028 -0
- package/src/schema/struct_field.ts +234 -0
- package/src/schema/union_field.ts +105 -0
- package/src/scripts/custom_compiler.ts +331 -0
- package/src/scripts/custom_graphql.ts +550 -0
- package/src/scripts/migrate_v0.1.ts +41 -0
- package/src/scripts/move_types.ts +131 -0
- package/src/scripts/read_schema.ts +67 -0
- package/src/setupPackage.js +42 -0
- package/src/testutils/action/complex_schemas.ts +517 -0
- package/src/testutils/builder.ts +422 -0
- package/src/testutils/context/test_context.ts +25 -0
- package/src/testutils/db/fixture.ts +32 -0
- package/src/testutils/db/temp_db.ts +941 -0
- package/src/testutils/db/value.ts +294 -0
- package/src/testutils/db_mock.ts +351 -0
- package/src/testutils/db_time_zone.ts +40 -0
- package/src/testutils/ent-graphql-tests/index.ts +653 -0
- package/src/testutils/fake_comms.ts +50 -0
- package/src/testutils/fake_data/const.ts +64 -0
- package/src/testutils/fake_data/events_query.ts +145 -0
- package/src/testutils/fake_data/fake_contact.ts +150 -0
- package/src/testutils/fake_data/fake_event.ts +150 -0
- package/src/testutils/fake_data/fake_tag.ts +139 -0
- package/src/testutils/fake_data/fake_user.ts +232 -0
- package/src/testutils/fake_data/index.ts +1 -0
- package/src/testutils/fake_data/internal.ts +8 -0
- package/src/testutils/fake_data/tag_query.ts +56 -0
- package/src/testutils/fake_data/test_helpers.ts +388 -0
- package/src/testutils/fake_data/user_query.ts +524 -0
- package/src/testutils/fake_log.ts +52 -0
- package/src/testutils/mock_date.ts +10 -0
- package/src/testutils/mock_log.ts +39 -0
- package/src/testutils/parse_sql.ts +685 -0
- package/src/testutils/test_edge_global_schema.ts +49 -0
- package/src/testutils/write.ts +70 -0
- package/src/tsc/ast.ts +351 -0
- package/src/tsc/compilerOptions.ts +85 -0
- package/src/tsc/move_generated.ts +191 -0
- package/src/tsc/transform.ts +226 -0
- package/src/tsc/transform_action.ts +224 -0
- package/src/tsc/transform_ent.ts +66 -0
- package/src/tsc/transform_schema.ts +546 -0
- package/tsconfig.json +20 -0
- package/core/query/shared_assoc_test.d.ts +0 -2
- package/core/query/shared_assoc_test.js +0 -804
- package/core/query/shared_test.d.ts +0 -21
- package/core/query/shared_test.js +0 -736
- package/graphql/query/shared_assoc_test.d.ts +0 -1
- package/graphql/query/shared_assoc_test.js +0 -203
- /package/{action → dist/action}/action.d.ts +0 -0
- /package/{action → dist/action}/action.js +0 -0
- /package/{action → dist/action}/executor.d.ts +0 -0
- /package/{action → dist/action}/executor.js +0 -0
- /package/{action → dist/action}/experimental_action.d.ts +0 -0
- /package/{action → dist/action}/experimental_action.js +0 -0
- /package/{action → dist/action}/index.d.ts +0 -0
- /package/{action → dist/action}/index.js +0 -0
- /package/{action → dist/action}/operations.d.ts +0 -0
- /package/{action → dist/action}/operations.js +0 -0
- /package/{action → dist/action}/orchestrator.d.ts +0 -0
- /package/{action → dist/action}/orchestrator.js +0 -0
- /package/{action → dist/action}/privacy.d.ts +0 -0
- /package/{action → dist/action}/privacy.js +0 -0
- /package/{action → dist/action}/relative_value.d.ts +0 -0
- /package/{action → dist/action}/relative_value.js +0 -0
- /package/{action → dist/action}/transaction.d.ts +0 -0
- /package/{action → dist/action}/transaction.js +0 -0
- /package/{auth → dist/auth}/auth.d.ts +0 -0
- /package/{auth → dist/auth}/auth.js +0 -0
- /package/{auth → dist/auth}/index.d.ts +0 -0
- /package/{auth → dist/auth}/index.js +0 -0
- /package/{core → dist/core}/base.d.ts +0 -0
- /package/{core → dist/core}/base.js +0 -0
- /package/{core → dist/core}/clause.d.ts +0 -0
- /package/{core → dist/core}/clause.js +0 -0
- /package/{core → dist/core}/config.d.ts +0 -0
- /package/{core → dist/core}/config.js +0 -0
- /package/{core → dist/core}/const.d.ts +0 -0
- /package/{core → dist/core}/const.js +0 -0
- /package/{core → dist/core}/context.d.ts +0 -0
- /package/{core → dist/core}/context.js +0 -0
- /package/{core → dist/core}/convert.d.ts +0 -0
- /package/{core → dist/core}/convert.js +0 -0
- /package/{core → dist/core}/date.d.ts +0 -0
- /package/{core → dist/core}/date.js +0 -0
- /package/{core → dist/core}/db.d.ts +0 -0
- /package/{core → dist/core}/db.js +0 -0
- /package/{core → dist/core}/ent.d.ts +0 -0
- /package/{core → dist/core}/ent.js +0 -0
- /package/{core → dist/core}/global_schema.d.ts +0 -0
- /package/{core → dist/core}/global_schema.js +0 -0
- /package/{core → dist/core}/loaders/assoc_count_loader.d.ts +0 -0
- /package/{core → dist/core}/loaders/assoc_count_loader.js +0 -0
- /package/{core → dist/core}/loaders/assoc_edge_loader.d.ts +0 -0
- /package/{core → dist/core}/loaders/assoc_edge_loader.js +0 -0
- /package/{core → dist/core}/loaders/index.d.ts +0 -0
- /package/{core → dist/core}/loaders/index.js +0 -0
- /package/{core → dist/core}/loaders/loader.d.ts +0 -0
- /package/{core → dist/core}/loaders/loader.js +0 -0
- /package/{core → dist/core}/loaders/object_loader.d.ts +0 -0
- /package/{core → dist/core}/loaders/object_loader.js +0 -0
- /package/{core → dist/core}/loaders/query_loader.d.ts +0 -0
- /package/{core → dist/core}/loaders/query_loader.js +0 -0
- /package/{core → dist/core}/loaders/raw_count_loader.d.ts +0 -0
- /package/{core → dist/core}/loaders/raw_count_loader.js +0 -0
- /package/{core → dist/core}/logger.d.ts +0 -0
- /package/{core → dist/core}/logger.js +0 -0
- /package/{core → dist/core}/privacy.d.ts +0 -0
- /package/{core → dist/core}/privacy.js +0 -0
- /package/{core → dist/core}/query/assoc_query.d.ts +0 -0
- /package/{core → dist/core}/query/assoc_query.js +0 -0
- /package/{core → dist/core}/query/custom_clause_query.d.ts +0 -0
- /package/{core → dist/core}/query/custom_clause_query.js +0 -0
- /package/{core → dist/core}/query/custom_query.d.ts +0 -0
- /package/{core → dist/core}/query/custom_query.js +0 -0
- /package/{core → dist/core}/query/index.d.ts +0 -0
- /package/{core → dist/core}/query/index.js +0 -0
- /package/{core → dist/core}/query/query.d.ts +0 -0
- /package/{core → dist/core}/query/query.js +0 -0
- /package/{core → dist/core}/query_impl.d.ts +0 -0
- /package/{core → dist/core}/query_impl.js +0 -0
- /package/{core → dist/core}/viewer.d.ts +0 -0
- /package/{core → dist/core}/viewer.js +0 -0
- /package/{graphql → dist/graphql}/builtins/connection.d.ts +0 -0
- /package/{graphql → dist/graphql}/builtins/connection.js +0 -0
- /package/{graphql → dist/graphql}/builtins/edge.d.ts +0 -0
- /package/{graphql → dist/graphql}/builtins/edge.js +0 -0
- /package/{graphql → dist/graphql}/builtins/node.d.ts +0 -0
- /package/{graphql → dist/graphql}/builtins/node.js +0 -0
- /package/{graphql → dist/graphql}/graphql.d.ts +0 -0
- /package/{graphql → dist/graphql}/graphql.js +0 -0
- /package/{graphql → dist/graphql}/graphql_field_helpers.d.ts +0 -0
- /package/{graphql → dist/graphql}/graphql_field_helpers.js +0 -0
- /package/{graphql → dist/graphql}/index.d.ts +0 -0
- /package/{graphql → dist/graphql}/index.js +0 -0
- /package/{graphql → dist/graphql}/mutations/union.d.ts +0 -0
- /package/{graphql → dist/graphql}/mutations/union.js +0 -0
- /package/{graphql → dist/graphql}/node_resolver.d.ts +0 -0
- /package/{graphql → dist/graphql}/node_resolver.js +0 -0
- /package/{graphql → dist/graphql}/query/connection_type.d.ts +0 -0
- /package/{graphql → dist/graphql}/query/connection_type.js +0 -0
- /package/{graphql → dist/graphql}/query/edge_connection.d.ts +0 -0
- /package/{graphql → dist/graphql}/query/edge_connection.js +0 -0
- /package/{graphql → dist/graphql}/query/page_info.d.ts +0 -0
- /package/{graphql → dist/graphql}/query/page_info.js +0 -0
- /package/{graphql → dist/graphql}/query/shared_edge_connection.d.ts +0 -0
- /package/{graphql → dist/graphql}/query/shared_edge_connection.js +0 -0
- /package/{graphql → dist/graphql}/scalars/orderby_direction.d.ts +0 -0
- /package/{graphql → dist/graphql}/scalars/orderby_direction.js +0 -0
- /package/{graphql → dist/graphql}/scalars/time.d.ts +0 -0
- /package/{graphql → dist/graphql}/scalars/time.js +0 -0
- /package/{imports → dist/imports}/dataz/example1/_auth.d.ts +0 -0
- /package/{imports → dist/imports}/dataz/example1/_auth.js +0 -0
- /package/{imports → dist/imports}/dataz/example1/_viewer.d.ts +0 -0
- /package/{imports → dist/imports}/dataz/example1/_viewer.js +0 -0
- /package/{imports → dist/imports}/index.d.ts +0 -0
- /package/{imports → dist/imports}/index.js +0 -0
- /package/{index.d.ts → dist/index.d.ts} +0 -0
- /package/{index.js → dist/index.js} +0 -0
- /package/{parse_schema → dist/parse_schema}/parse.d.ts +0 -0
- /package/{parse_schema → dist/parse_schema}/parse.js +0 -0
- /package/{schema → dist/schema}/base_schema.d.ts +0 -0
- /package/{schema → dist/schema}/base_schema.js +0 -0
- /package/{schema → dist/schema}/field.d.ts +0 -0
- /package/{schema → dist/schema}/field.js +0 -0
- /package/{schema → dist/schema}/index.d.ts +0 -0
- /package/{schema → dist/schema}/index.js +0 -0
- /package/{schema → dist/schema}/json_field.d.ts +0 -0
- /package/{schema → dist/schema}/json_field.js +0 -0
- /package/{schema → dist/schema}/schema.d.ts +0 -0
- /package/{schema → dist/schema}/schema.js +0 -0
- /package/{schema → dist/schema}/struct_field.d.ts +0 -0
- /package/{schema → dist/schema}/struct_field.js +0 -0
- /package/{schema → dist/schema}/union_field.d.ts +0 -0
- /package/{schema → dist/schema}/union_field.js +0 -0
- /package/{scripts → dist/scripts}/custom_compiler.d.ts +0 -0
- /package/{scripts → dist/scripts}/custom_graphql.d.ts +0 -0
- /package/{scripts → dist/scripts}/migrate_v0.1.d.ts +0 -0
- /package/{scripts → dist/scripts}/migrate_v0.1.js +0 -0
- /package/{scripts → dist/scripts}/move_types.d.ts +0 -0
- /package/{scripts → dist/scripts}/move_types.js +0 -0
- /package/{scripts → dist/scripts}/read_schema.d.ts +0 -0
- /package/{scripts → dist/scripts}/read_schema.js +0 -0
- /package/{testutils → dist/testutils}/action/complex_schemas.d.ts +0 -0
- /package/{testutils → dist/testutils}/action/complex_schemas.js +0 -0
- /package/{testutils → dist/testutils}/builder.d.ts +0 -0
- /package/{testutils → dist/testutils}/builder.js +0 -0
- /package/{testutils → dist/testutils}/context/test_context.d.ts +0 -0
- /package/{testutils → dist/testutils}/context/test_context.js +0 -0
- /package/{testutils → dist/testutils}/db/fixture.d.ts +0 -0
- /package/{testutils → dist/testutils}/db/fixture.js +0 -0
- /package/{testutils → dist/testutils}/db/temp_db.d.ts +0 -0
- /package/{testutils → dist/testutils}/db/temp_db.js +0 -0
- /package/{testutils → dist/testutils}/db/value.d.ts +0 -0
- /package/{testutils → dist/testutils}/db/value.js +0 -0
- /package/{testutils → dist/testutils}/db_mock.d.ts +0 -0
- /package/{testutils → dist/testutils}/db_mock.js +0 -0
- /package/{testutils → dist/testutils}/db_time_zone.d.ts +0 -0
- /package/{testutils → dist/testutils}/db_time_zone.js +0 -0
- /package/{testutils → dist/testutils}/ent-graphql-tests/index.d.ts +0 -0
- /package/{testutils → dist/testutils}/ent-graphql-tests/index.js +0 -0
- /package/{testutils → dist/testutils}/fake_comms.d.ts +0 -0
- /package/{testutils → dist/testutils}/fake_comms.js +0 -0
- /package/{testutils → dist/testutils}/fake_data/const.d.ts +0 -0
- /package/{testutils → dist/testutils}/fake_data/const.js +0 -0
- /package/{testutils → dist/testutils}/fake_data/events_query.d.ts +0 -0
- /package/{testutils → dist/testutils}/fake_data/events_query.js +0 -0
- /package/{testutils → dist/testutils}/fake_data/fake_contact.d.ts +0 -0
- /package/{testutils → dist/testutils}/fake_data/fake_contact.js +0 -0
- /package/{testutils → dist/testutils}/fake_data/fake_event.d.ts +0 -0
- /package/{testutils → dist/testutils}/fake_data/fake_event.js +0 -0
- /package/{testutils → dist/testutils}/fake_data/fake_tag.d.ts +0 -0
- /package/{testutils → dist/testutils}/fake_data/fake_tag.js +0 -0
- /package/{testutils → dist/testutils}/fake_data/fake_user.d.ts +0 -0
- /package/{testutils → dist/testutils}/fake_data/fake_user.js +0 -0
- /package/{testutils → dist/testutils}/fake_data/index.d.ts +0 -0
- /package/{testutils → dist/testutils}/fake_data/index.js +0 -0
- /package/{testutils → dist/testutils}/fake_data/internal.d.ts +0 -0
- /package/{testutils → dist/testutils}/fake_data/internal.js +0 -0
- /package/{testutils → dist/testutils}/fake_data/tag_query.d.ts +0 -0
- /package/{testutils → dist/testutils}/fake_data/tag_query.js +0 -0
- /package/{testutils → dist/testutils}/fake_data/test_helpers.d.ts +0 -0
- /package/{testutils → dist/testutils}/fake_data/test_helpers.js +0 -0
- /package/{testutils → dist/testutils}/fake_data/user_query.d.ts +0 -0
- /package/{testutils → dist/testutils}/fake_data/user_query.js +0 -0
- /package/{testutils → dist/testutils}/fake_log.d.ts +0 -0
- /package/{testutils → dist/testutils}/fake_log.js +0 -0
- /package/{testutils → dist/testutils}/mock_date.d.ts +0 -0
- /package/{testutils → dist/testutils}/mock_date.js +0 -0
- /package/{testutils → dist/testutils}/mock_log.d.ts +0 -0
- /package/{testutils → dist/testutils}/mock_log.js +0 -0
- /package/{testutils → dist/testutils}/parse_sql.d.ts +0 -0
- /package/{testutils → dist/testutils}/parse_sql.js +0 -0
- /package/{testutils → dist/testutils}/test_edge_global_schema.d.ts +0 -0
- /package/{testutils → dist/testutils}/test_edge_global_schema.js +0 -0
- /package/{testutils → dist/testutils}/write.d.ts +0 -0
- /package/{testutils → dist/testutils}/write.js +0 -0
- /package/{tsc → dist/tsc}/ast.d.ts +0 -0
- /package/{tsc → dist/tsc}/ast.js +0 -0
- /package/{tsc → dist/tsc}/compilerOptions.d.ts +0 -0
- /package/{tsc → dist/tsc}/compilerOptions.js +0 -0
- /package/{tsc → dist/tsc}/move_generated.d.ts +0 -0
- /package/{tsc → dist/tsc}/move_generated.js +0 -0
- /package/{tsc → dist/tsc}/transform.d.ts +0 -0
- /package/{tsc → dist/tsc}/transform.js +0 -0
- /package/{tsc → dist/tsc}/transform_action.d.ts +0 -0
- /package/{tsc → dist/tsc}/transform_action.js +0 -0
- /package/{tsc → dist/tsc}/transform_ent.d.ts +0 -0
- /package/{tsc → dist/tsc}/transform_ent.js +0 -0
- /package/{tsc → dist/tsc}/transform_schema.d.ts +0 -0
- /package/{tsc → dist/tsc}/transform_schema.js +0 -0
|
@@ -0,0 +1,660 @@
|
|
|
1
|
+
import { isPromise } from "util/types";
|
|
2
|
+
import {
|
|
3
|
+
Allow,
|
|
4
|
+
Context,
|
|
5
|
+
Deny,
|
|
6
|
+
Ent,
|
|
7
|
+
ID,
|
|
8
|
+
LoadEntOptions,
|
|
9
|
+
PrivacyError,
|
|
10
|
+
PrivacyPolicy,
|
|
11
|
+
PrivacyPolicyRule,
|
|
12
|
+
PrivacyResult,
|
|
13
|
+
Skip,
|
|
14
|
+
Viewer,
|
|
15
|
+
EdgeQueryableDataOptionsConfigureLoader,
|
|
16
|
+
} from "./base";
|
|
17
|
+
import { AssocEdge, loadEdgeForID2, loadEnt } from "./ent";
|
|
18
|
+
|
|
19
|
+
// copied from ./base
|
|
20
|
+
enum privacyResult {
|
|
21
|
+
// using http status codes similar to golang for the lols
|
|
22
|
+
Allow = 200,
|
|
23
|
+
Deny = 401,
|
|
24
|
+
Skip = 307,
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export class EntPrivacyError extends Error implements PrivacyError {
|
|
28
|
+
privacyPolicy: PrivacyPolicy;
|
|
29
|
+
privacyRule: PrivacyPolicyRule;
|
|
30
|
+
ent?: Ent;
|
|
31
|
+
|
|
32
|
+
constructor(
|
|
33
|
+
privacyPolicy: PrivacyPolicy,
|
|
34
|
+
rule: PrivacyPolicyRule,
|
|
35
|
+
ent?: Ent,
|
|
36
|
+
) {
|
|
37
|
+
let msg = `ent ${ent?.id} is not visible for privacy reasons`;
|
|
38
|
+
|
|
39
|
+
if (typeof ent === "object") {
|
|
40
|
+
ent.constructor.name;
|
|
41
|
+
msg = `ent ${ent?.id} of type ${ent.constructor.name} is not visible for privacy reasons`;
|
|
42
|
+
}
|
|
43
|
+
super(msg);
|
|
44
|
+
this.privacyPolicy = privacyPolicy;
|
|
45
|
+
this.privacyRule = rule;
|
|
46
|
+
this.ent = ent;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
class EntInvalidPrivacyPolicyError extends Error implements PrivacyError {
|
|
51
|
+
privacyPolicy: PrivacyPolicy;
|
|
52
|
+
ent?: Ent;
|
|
53
|
+
|
|
54
|
+
constructor(privacyPolicy: PrivacyPolicy, ent?: Ent) {
|
|
55
|
+
let msg = `ent ${ent?.id} is not visible because privacy policy is not properly configured`;
|
|
56
|
+
|
|
57
|
+
if (typeof ent === "object") {
|
|
58
|
+
ent.constructor.name;
|
|
59
|
+
msg = `ent ${ent?.id} of type ${ent.constructor.name} is not visible because privacy policy is not properly configured`;
|
|
60
|
+
}
|
|
61
|
+
super(msg);
|
|
62
|
+
this.privacyPolicy = privacyPolicy;
|
|
63
|
+
this.ent = ent;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export const AlwaysAllowRule = {
|
|
68
|
+
async apply(_v: Viewer, _ent?: Ent): Promise<PrivacyResult> {
|
|
69
|
+
return Allow();
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
export const AlwaysDenyRule = {
|
|
74
|
+
async apply(_v: Viewer, _ent?: Ent): Promise<PrivacyResult> {
|
|
75
|
+
return Deny();
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
export const DenyIfLoggedOutRule = {
|
|
80
|
+
async apply(v: Viewer, _ent?: Ent): Promise<PrivacyResult> {
|
|
81
|
+
if (v.viewerID === null || v.viewerID == undefined) {
|
|
82
|
+
return Deny();
|
|
83
|
+
}
|
|
84
|
+
return Skip();
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
export const DenyIfLoggedInRule = {
|
|
89
|
+
async apply(v: Viewer, _ent?: Ent): Promise<PrivacyResult> {
|
|
90
|
+
if (v.viewerID === null || v.viewerID == undefined) {
|
|
91
|
+
return Skip();
|
|
92
|
+
}
|
|
93
|
+
return Deny();
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export const AllowIfHasIdentity = {
|
|
98
|
+
async apply(v: Viewer, _ent?: Ent): Promise<PrivacyResult> {
|
|
99
|
+
if (v.viewerID === null || v.viewerID == undefined) {
|
|
100
|
+
return Skip();
|
|
101
|
+
}
|
|
102
|
+
return Allow();
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
export const AllowIfViewerRule = {
|
|
107
|
+
async apply(v: Viewer, ent?: Ent): Promise<PrivacyResult> {
|
|
108
|
+
if (v.viewerID && v.viewerID === ent?.id) {
|
|
109
|
+
return Allow();
|
|
110
|
+
}
|
|
111
|
+
return Skip();
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
export class AllowIfViewerEqualsRule {
|
|
116
|
+
constructor(private id: any) {}
|
|
117
|
+
|
|
118
|
+
async apply(v: Viewer, _ent?: Ent): Promise<PrivacyResult> {
|
|
119
|
+
return v.viewerID === this.id ? Allow() : Skip();
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export class DenyIfViewerEqualsRule {
|
|
124
|
+
constructor(private id: ID) {}
|
|
125
|
+
|
|
126
|
+
async apply(v: Viewer, _ent?: Ent): Promise<PrivacyResult> {
|
|
127
|
+
return v.viewerID === this.id ? Deny() : Skip();
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
interface FuncRule {
|
|
132
|
+
(v: Viewer, ent?: Ent): boolean | Promise<boolean>;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export class AllowIfFuncRule implements PrivacyPolicyRule {
|
|
136
|
+
constructor(private fn: FuncRule) {}
|
|
137
|
+
|
|
138
|
+
async apply(v: Viewer, ent?: Ent): Promise<PrivacyResult> {
|
|
139
|
+
let result = this.fn(v, ent);
|
|
140
|
+
if (isPromise(result)) {
|
|
141
|
+
result = await result;
|
|
142
|
+
}
|
|
143
|
+
if (result) {
|
|
144
|
+
return Allow();
|
|
145
|
+
}
|
|
146
|
+
return Skip();
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export class DenyIfFuncRule implements PrivacyPolicyRule {
|
|
151
|
+
constructor(private fn: FuncRule) {}
|
|
152
|
+
|
|
153
|
+
async apply(v: Viewer, ent?: Ent): Promise<PrivacyResult> {
|
|
154
|
+
let result = this.fn(v, ent);
|
|
155
|
+
if (isPromise(result)) {
|
|
156
|
+
result = await result;
|
|
157
|
+
}
|
|
158
|
+
if (result) {
|
|
159
|
+
return Deny();
|
|
160
|
+
}
|
|
161
|
+
return Skip();
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* @deprecated use AllowIfViewerIsEntPropertyRule
|
|
167
|
+
*/
|
|
168
|
+
export class AllowIfViewerIsRule implements PrivacyPolicyRule {
|
|
169
|
+
constructor(private property: string) {}
|
|
170
|
+
|
|
171
|
+
async apply(v: Viewer, ent?: Ent): Promise<PrivacyResult> {
|
|
172
|
+
let result: undefined;
|
|
173
|
+
if (ent) {
|
|
174
|
+
result = ent[this.property];
|
|
175
|
+
}
|
|
176
|
+
if (result === v.viewerID) {
|
|
177
|
+
return Allow();
|
|
178
|
+
}
|
|
179
|
+
return Skip();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export class AllowIfViewerIsEntPropertyRule<T extends Ent>
|
|
184
|
+
implements PrivacyPolicyRule
|
|
185
|
+
{
|
|
186
|
+
constructor(private property: keyof T) {}
|
|
187
|
+
|
|
188
|
+
async apply(v: Viewer, ent?: T): Promise<PrivacyResult> {
|
|
189
|
+
const result: any = ent && ent[this.property];
|
|
190
|
+
if (result === v.viewerID) {
|
|
191
|
+
return Allow();
|
|
192
|
+
}
|
|
193
|
+
return Skip();
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export class AllowIfEntPropertyIsRule<T extends Ent>
|
|
198
|
+
implements PrivacyPolicyRule
|
|
199
|
+
{
|
|
200
|
+
constructor(private property: keyof T, private val: any) {}
|
|
201
|
+
|
|
202
|
+
async apply(v: Viewer, ent?: T): Promise<PrivacyResult> {
|
|
203
|
+
const result: any = ent && ent[this.property];
|
|
204
|
+
if (result === this.val) {
|
|
205
|
+
return Allow();
|
|
206
|
+
}
|
|
207
|
+
return Skip();
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export class DenyIfEntPropertyIsRule<T extends Ent>
|
|
212
|
+
implements PrivacyPolicyRule
|
|
213
|
+
{
|
|
214
|
+
constructor(private property: keyof T, private val: any) {}
|
|
215
|
+
|
|
216
|
+
async apply(v: Viewer, ent?: T): Promise<PrivacyResult> {
|
|
217
|
+
const result: any = ent && ent[this.property];
|
|
218
|
+
if (result === this.val) {
|
|
219
|
+
return Deny();
|
|
220
|
+
}
|
|
221
|
+
return Skip();
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export class AllowIfEntIsVisibleRule<
|
|
226
|
+
TEnt extends Ent<TViewer>,
|
|
227
|
+
TViewer extends Viewer,
|
|
228
|
+
> implements PrivacyPolicyRule
|
|
229
|
+
{
|
|
230
|
+
constructor(private id: ID, private options: LoadEntOptions<TEnt, TViewer>) {}
|
|
231
|
+
|
|
232
|
+
async apply(v: TViewer, _ent?: Ent): Promise<PrivacyResult> {
|
|
233
|
+
const visible = await loadEnt(v, this.id, this.options);
|
|
234
|
+
if (visible === null) {
|
|
235
|
+
return Skip();
|
|
236
|
+
}
|
|
237
|
+
return Allow();
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export class AllowIfEntIsNotVisibleRule<
|
|
242
|
+
TEnt extends Ent<TViewer>,
|
|
243
|
+
TViewer extends Viewer,
|
|
244
|
+
> implements PrivacyPolicyRule
|
|
245
|
+
{
|
|
246
|
+
constructor(private id: ID, private options: LoadEntOptions<TEnt, TViewer>) {}
|
|
247
|
+
|
|
248
|
+
async apply(v: TViewer, _ent?: Ent): Promise<PrivacyResult> {
|
|
249
|
+
const visible = await loadEnt(v, this.id, this.options);
|
|
250
|
+
if (visible === null) {
|
|
251
|
+
return Allow();
|
|
252
|
+
}
|
|
253
|
+
return Skip();
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export class AllowIfEntIsVisiblePolicy<
|
|
258
|
+
TEnt extends Ent<TViewer>,
|
|
259
|
+
TViewer extends Viewer,
|
|
260
|
+
> implements PrivacyPolicy<TEnt, TViewer>
|
|
261
|
+
{
|
|
262
|
+
constructor(private id: ID, private options: LoadEntOptions<TEnt, TViewer>) {}
|
|
263
|
+
|
|
264
|
+
rules = [new AllowIfEntIsVisibleRule(this.id, this.options), AlwaysDenyRule];
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export class DenyIfEntIsVisiblePolicy<
|
|
268
|
+
TEnt extends Ent<TViewer>,
|
|
269
|
+
TViewer extends Viewer,
|
|
270
|
+
> implements PrivacyPolicy<TEnt, TViewer>
|
|
271
|
+
{
|
|
272
|
+
constructor(private id: ID, private options: LoadEntOptions<TEnt, TViewer>) {}
|
|
273
|
+
|
|
274
|
+
rules = [new DenyIfEntIsVisibleRule(this.id, this.options), AlwaysAllowRule];
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export class DenyIfEntIsVisibleRule<
|
|
278
|
+
TEnt extends Ent<TViewer>,
|
|
279
|
+
TViewer extends Viewer,
|
|
280
|
+
> implements PrivacyPolicyRule<TEnt, TViewer>
|
|
281
|
+
{
|
|
282
|
+
constructor(private id: ID, private options: LoadEntOptions<TEnt, TViewer>) {}
|
|
283
|
+
|
|
284
|
+
async apply(v: TViewer, _ent?: Ent): Promise<PrivacyResult> {
|
|
285
|
+
const visible = await loadEnt(v, this.id, this.options);
|
|
286
|
+
if (visible === null) {
|
|
287
|
+
return Skip();
|
|
288
|
+
}
|
|
289
|
+
return Deny();
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
export class DenyIfEntIsNotVisibleRule<
|
|
294
|
+
TEnt extends Ent<TViewer>,
|
|
295
|
+
TViewer extends Viewer,
|
|
296
|
+
> implements PrivacyPolicyRule
|
|
297
|
+
{
|
|
298
|
+
constructor(private id: ID, private options: LoadEntOptions<TEnt, TViewer>) {}
|
|
299
|
+
|
|
300
|
+
async apply(v: TViewer, _ent?: Ent): Promise<PrivacyResult> {
|
|
301
|
+
const visible = await loadEnt(v, this.id, this.options);
|
|
302
|
+
if (visible === null) {
|
|
303
|
+
return Deny();
|
|
304
|
+
}
|
|
305
|
+
return Skip();
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
async function allowIfEdgeExistsRule(
|
|
310
|
+
id1: ID | null | undefined,
|
|
311
|
+
id2: ID | null | undefined,
|
|
312
|
+
edgeType: string,
|
|
313
|
+
context?: Context,
|
|
314
|
+
options?: EdgeQueryableDataOptionsConfigureLoader,
|
|
315
|
+
): Promise<PrivacyResult> {
|
|
316
|
+
if (id1 && id2) {
|
|
317
|
+
const edge = await loadEdgeForID2({
|
|
318
|
+
id1,
|
|
319
|
+
edgeType,
|
|
320
|
+
id2,
|
|
321
|
+
context,
|
|
322
|
+
ctr: AssocEdge,
|
|
323
|
+
queryOptions: options,
|
|
324
|
+
});
|
|
325
|
+
if (edge) {
|
|
326
|
+
return Allow();
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
return Skip();
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
export class AllowIfEdgeExistsRule implements PrivacyPolicyRule {
|
|
333
|
+
constructor(
|
|
334
|
+
private id1: ID,
|
|
335
|
+
private id2: ID,
|
|
336
|
+
private edgeType: string,
|
|
337
|
+
private options?: EdgeQueryableDataOptionsConfigureLoader,
|
|
338
|
+
) {}
|
|
339
|
+
|
|
340
|
+
async apply(v: Viewer, _ent?: Ent): Promise<PrivacyResult> {
|
|
341
|
+
return allowIfEdgeExistsRule(
|
|
342
|
+
this.id1,
|
|
343
|
+
this.id2,
|
|
344
|
+
this.edgeType,
|
|
345
|
+
v.context,
|
|
346
|
+
this.options,
|
|
347
|
+
);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
export class AllowIfViewerInboundEdgeExistsRule implements PrivacyPolicyRule {
|
|
352
|
+
constructor(
|
|
353
|
+
private edgeType: string,
|
|
354
|
+
private options?: EdgeQueryableDataOptionsConfigureLoader,
|
|
355
|
+
) {}
|
|
356
|
+
|
|
357
|
+
async apply(v: Viewer, ent?: Ent): Promise<PrivacyResult> {
|
|
358
|
+
return allowIfEdgeExistsRule(
|
|
359
|
+
v.viewerID,
|
|
360
|
+
ent?.id,
|
|
361
|
+
this.edgeType,
|
|
362
|
+
v.context,
|
|
363
|
+
this.options,
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
export class AllowIfViewerOutboundEdgeExistsRule implements PrivacyPolicyRule {
|
|
369
|
+
constructor(
|
|
370
|
+
private edgeType: string,
|
|
371
|
+
private options?: EdgeQueryableDataOptionsConfigureLoader,
|
|
372
|
+
) {}
|
|
373
|
+
|
|
374
|
+
async apply(v: Viewer, ent?: Ent): Promise<PrivacyResult> {
|
|
375
|
+
return allowIfEdgeExistsRule(
|
|
376
|
+
ent?.id,
|
|
377
|
+
v.viewerID,
|
|
378
|
+
this.edgeType,
|
|
379
|
+
v.context,
|
|
380
|
+
this.options,
|
|
381
|
+
);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
async function denyIfEdgeExistsRule(
|
|
386
|
+
id1: ID | null | undefined,
|
|
387
|
+
id2: ID | null | undefined,
|
|
388
|
+
edgeType: string,
|
|
389
|
+
context?: Context,
|
|
390
|
+
options?: EdgeQueryableDataOptionsConfigureLoader,
|
|
391
|
+
): Promise<PrivacyResult> {
|
|
392
|
+
// edge doesn't exist if no viewer
|
|
393
|
+
if (id1 && id2) {
|
|
394
|
+
const edge = await loadEdgeForID2({
|
|
395
|
+
id1,
|
|
396
|
+
edgeType,
|
|
397
|
+
id2,
|
|
398
|
+
context,
|
|
399
|
+
ctr: AssocEdge,
|
|
400
|
+
queryOptions: options,
|
|
401
|
+
});
|
|
402
|
+
if (edge) {
|
|
403
|
+
return Deny();
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
return Skip();
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
async function denyIfEdgeDoesNotExistRule(
|
|
410
|
+
id1: ID | null | undefined,
|
|
411
|
+
id2: ID | null | undefined,
|
|
412
|
+
edgeType: string,
|
|
413
|
+
context?: Context,
|
|
414
|
+
options?: EdgeQueryableDataOptionsConfigureLoader,
|
|
415
|
+
): Promise<PrivacyResult> {
|
|
416
|
+
// edge doesn't exist if no viewer
|
|
417
|
+
if (!id1 || !id2) {
|
|
418
|
+
return Deny();
|
|
419
|
+
}
|
|
420
|
+
const edge = await loadEdgeForID2({
|
|
421
|
+
id1,
|
|
422
|
+
edgeType,
|
|
423
|
+
id2,
|
|
424
|
+
context,
|
|
425
|
+
ctr: AssocEdge,
|
|
426
|
+
queryOptions: options,
|
|
427
|
+
});
|
|
428
|
+
if (!edge) {
|
|
429
|
+
return Deny();
|
|
430
|
+
}
|
|
431
|
+
return Skip();
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
export class DenyIfEdgeExistsRule implements PrivacyPolicyRule {
|
|
435
|
+
constructor(
|
|
436
|
+
private id1: ID,
|
|
437
|
+
private id2: ID,
|
|
438
|
+
private edgeType: string,
|
|
439
|
+
private options?: EdgeQueryableDataOptionsConfigureLoader,
|
|
440
|
+
) {}
|
|
441
|
+
|
|
442
|
+
async apply(v: Viewer, _ent?: Ent): Promise<PrivacyResult> {
|
|
443
|
+
return denyIfEdgeExistsRule(
|
|
444
|
+
this.id1,
|
|
445
|
+
this.id2,
|
|
446
|
+
this.edgeType,
|
|
447
|
+
v.context,
|
|
448
|
+
this.options,
|
|
449
|
+
);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
export class DenyIfViewerInboundEdgeExistsRule implements PrivacyPolicyRule {
|
|
454
|
+
constructor(
|
|
455
|
+
private edgeType: string,
|
|
456
|
+
private options?: EdgeQueryableDataOptionsConfigureLoader,
|
|
457
|
+
) {}
|
|
458
|
+
|
|
459
|
+
async apply(v: Viewer, ent?: Ent): Promise<PrivacyResult> {
|
|
460
|
+
return denyIfEdgeExistsRule(
|
|
461
|
+
v.viewerID,
|
|
462
|
+
ent?.id,
|
|
463
|
+
this.edgeType,
|
|
464
|
+
v.context,
|
|
465
|
+
this.options,
|
|
466
|
+
);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
export class DenyIfViewerOutboundEdgeExistsRule implements PrivacyPolicyRule {
|
|
471
|
+
constructor(
|
|
472
|
+
private edgeType: string,
|
|
473
|
+
private options?: EdgeQueryableDataOptionsConfigureLoader,
|
|
474
|
+
) {}
|
|
475
|
+
|
|
476
|
+
async apply(v: Viewer, ent?: Ent): Promise<PrivacyResult> {
|
|
477
|
+
return denyIfEdgeExistsRule(
|
|
478
|
+
ent?.id,
|
|
479
|
+
v.viewerID,
|
|
480
|
+
this.edgeType,
|
|
481
|
+
v.context,
|
|
482
|
+
this.options,
|
|
483
|
+
);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
export class DenyIfEdgeDoesNotExistRule implements PrivacyPolicyRule {
|
|
488
|
+
constructor(
|
|
489
|
+
private id1: ID,
|
|
490
|
+
private id2: ID,
|
|
491
|
+
private edgeType: string,
|
|
492
|
+
private options?: EdgeQueryableDataOptionsConfigureLoader,
|
|
493
|
+
) {}
|
|
494
|
+
|
|
495
|
+
async apply(v: Viewer, _ent?: Ent): Promise<PrivacyResult> {
|
|
496
|
+
return denyIfEdgeDoesNotExistRule(
|
|
497
|
+
this.id1,
|
|
498
|
+
this.id2,
|
|
499
|
+
this.edgeType,
|
|
500
|
+
v.context,
|
|
501
|
+
this.options,
|
|
502
|
+
);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
export class DenyIfViewerInboundEdgeDoesNotExistRule
|
|
507
|
+
implements PrivacyPolicyRule
|
|
508
|
+
{
|
|
509
|
+
constructor(
|
|
510
|
+
private edgeType: string,
|
|
511
|
+
private options?: EdgeQueryableDataOptionsConfigureLoader,
|
|
512
|
+
) {}
|
|
513
|
+
|
|
514
|
+
async apply(v: Viewer, ent?: Ent): Promise<PrivacyResult> {
|
|
515
|
+
return denyIfEdgeDoesNotExistRule(
|
|
516
|
+
v.viewerID,
|
|
517
|
+
ent?.id,
|
|
518
|
+
this.edgeType,
|
|
519
|
+
v.context,
|
|
520
|
+
this.options,
|
|
521
|
+
);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
export class DenyIfViewerOutboundEdgeDoesNotExistRule
|
|
526
|
+
implements PrivacyPolicyRule
|
|
527
|
+
{
|
|
528
|
+
constructor(
|
|
529
|
+
private edgeType: string,
|
|
530
|
+
private options?: EdgeQueryableDataOptionsConfigureLoader,
|
|
531
|
+
) {}
|
|
532
|
+
|
|
533
|
+
async apply(v: Viewer, ent?: Ent): Promise<PrivacyResult> {
|
|
534
|
+
return denyIfEdgeDoesNotExistRule(
|
|
535
|
+
ent?.id,
|
|
536
|
+
v.viewerID,
|
|
537
|
+
this.edgeType,
|
|
538
|
+
v.context,
|
|
539
|
+
this.options,
|
|
540
|
+
);
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
// need a Deny version of this too
|
|
545
|
+
export class AllowIfConditionAppliesRule implements PrivacyPolicyRule {
|
|
546
|
+
constructor(private fn: FuncRule, private rule: PrivacyPolicyRule) {}
|
|
547
|
+
|
|
548
|
+
async apply(v: Viewer, ent?: Ent): Promise<PrivacyResult> {
|
|
549
|
+
const result = await this.fn(v, ent);
|
|
550
|
+
if (!result) {
|
|
551
|
+
return Skip();
|
|
552
|
+
}
|
|
553
|
+
const r = await this.rule.apply(v, ent);
|
|
554
|
+
return r.result === privacyResult.Allow ? Allow() : Skip();
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
interface DelayedFuncRule {
|
|
559
|
+
(v: Viewer, ent?: Ent):
|
|
560
|
+
| null
|
|
561
|
+
| PrivacyPolicyRule
|
|
562
|
+
| Promise<PrivacyPolicyRule | null>;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// use this when there's a computation needed to get the rule and then the privacy is applied on said rule
|
|
566
|
+
export class DelayedResultRule implements PrivacyPolicyRule {
|
|
567
|
+
constructor(private fn: DelayedFuncRule) {}
|
|
568
|
+
|
|
569
|
+
async apply(v: Viewer, ent?: Ent): Promise<PrivacyResult> {
|
|
570
|
+
let rule = this.fn(v, ent);
|
|
571
|
+
if (isPromise(rule)) {
|
|
572
|
+
rule = await rule;
|
|
573
|
+
}
|
|
574
|
+
if (!rule) {
|
|
575
|
+
return Skip();
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
return rule.apply(v, ent);
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
// TODO different variants
|
|
583
|
+
export class AllowIfSubPolicyAllowsRule implements PrivacyPolicyRule {
|
|
584
|
+
constructor(private policy: PrivacyPolicy) {}
|
|
585
|
+
|
|
586
|
+
async apply(v: Viewer, ent?: Ent): Promise<PrivacyResult> {
|
|
587
|
+
const result = await applyPrivacyPolicy(v, this.policy, ent);
|
|
588
|
+
if (result) {
|
|
589
|
+
return Allow();
|
|
590
|
+
}
|
|
591
|
+
return Skip();
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
export async function applyPrivacyPolicy(
|
|
596
|
+
v: Viewer,
|
|
597
|
+
policy: PrivacyPolicy,
|
|
598
|
+
ent: Ent | undefined,
|
|
599
|
+
): Promise<boolean> {
|
|
600
|
+
const err = await applyPrivacyPolicyImpl(v, policy, ent);
|
|
601
|
+
return err === null;
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
export async function applyPrivacyPolicyX(
|
|
605
|
+
v: Viewer,
|
|
606
|
+
policy: PrivacyPolicy,
|
|
607
|
+
ent: Ent | undefined,
|
|
608
|
+
throwErr?: () => Error,
|
|
609
|
+
): Promise<boolean> {
|
|
610
|
+
const err = await applyPrivacyPolicyImpl(v, policy, ent, throwErr);
|
|
611
|
+
if (err !== null) {
|
|
612
|
+
throw err;
|
|
613
|
+
}
|
|
614
|
+
return true;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
// this will throw an exception if fails or return error | null?
|
|
618
|
+
export async function applyPrivacyPolicyImpl(
|
|
619
|
+
v: Viewer,
|
|
620
|
+
policy: PrivacyPolicy,
|
|
621
|
+
ent: Ent | undefined,
|
|
622
|
+
throwErr?: () => Error,
|
|
623
|
+
): Promise<Error | null> {
|
|
624
|
+
for (const rule of policy.rules) {
|
|
625
|
+
const res = await rule.apply(v, ent);
|
|
626
|
+
if (res.result == privacyResult.Allow) {
|
|
627
|
+
return null;
|
|
628
|
+
} else if (res.result == privacyResult.Deny) {
|
|
629
|
+
// specific error throw that
|
|
630
|
+
if (res.error) {
|
|
631
|
+
return res.error;
|
|
632
|
+
}
|
|
633
|
+
if (res.getError) {
|
|
634
|
+
return res.getError(policy, rule, ent);
|
|
635
|
+
}
|
|
636
|
+
if (throwErr) {
|
|
637
|
+
return throwErr();
|
|
638
|
+
}
|
|
639
|
+
return new EntPrivacyError(policy, rule, ent);
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
return new EntInvalidPrivacyPolicyError(policy, ent);
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
export const AlwaysAllowPrivacyPolicy: PrivacyPolicy = {
|
|
647
|
+
rules: [AlwaysAllowRule],
|
|
648
|
+
};
|
|
649
|
+
|
|
650
|
+
export const AlwaysDenyPrivacyPolicy: PrivacyPolicy = {
|
|
651
|
+
rules: [AlwaysDenyRule],
|
|
652
|
+
};
|
|
653
|
+
|
|
654
|
+
export const AllowIfViewerPrivacyPolicy: PrivacyPolicy = {
|
|
655
|
+
rules: [AllowIfViewerRule, AlwaysDenyRule],
|
|
656
|
+
};
|
|
657
|
+
|
|
658
|
+
export const AllowIfViewerHasIdentityPrivacyPolicy: PrivacyPolicy = {
|
|
659
|
+
rules: [AllowIfHasIdentity, AlwaysDenyRule],
|
|
660
|
+
};
|