@fayz-ai/db 0.13.0 → 0.14.0
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/canonical-schema.json +106 -23
- package/dist/index.cjs +4 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -4
- package/dist/index.js.map +1 -1
- package/dist/schema/spine.d.ts +42 -37
- package/dist/schema/spine.d.ts.map +1 -1
- package/migrations/000_baseline.sql +29667 -0
- package/migrations/001_the_tenant_has_apps.sql +314 -0
- package/migrations/002_the_spine_drops_the_vendor_and_the_dead.sql +129 -0
- package/migrations/003_the_role_rpcs_finish_and_the_mirrors_go.sql +135 -0
- package/migrations/004_the_projections_get_a_successor.sql +312 -0
- package/migrations/005_the_archetypes_anchor_to_the_unit.sql +90 -0
- package/migrations/006_the_helpers_read_the_source.sql +153 -0
- package/migrations/007_the_compat_machinery_leaves_with_the_tables.sql +77 -0
- package/migrations/008_the_switchers_see_across_accounts.sql +150 -0
- package/migrations/009_the_compat_aliases_nobody_adopted.sql +57 -0
- package/migrations/010_the_new_tenant_is_born_with_its_trade.sql +201 -0
- package/migrations/011_leadcontrol_joins_the_family.sql +116 -0
- package/migrations/012_chefcontrol_wears_its_own_mark.sql +28 -0
- package/migrations/013_the_workspace_switch_reaches_the_other_half.sql +156 -0
- package/migrations/014_the_chooser_can_read_what_it_offers.sql +91 -0
- package/migrations/015_the_unit_bindings_belong_to_one_workspace.sql +86 -0
- package/migrations/016_the_rule_stops_asking_every_row.sql +543 -0
- package/migrations/017_the_pin_is_checked_against_the_right_workspace.sql +139 -0
- package/migrations/018_the_bootstrap_says_what_you_reach.sql +114 -0
- package/migrations/019_distributing_one_row_stops_costing_the_table.sql +245 -0
- package/migrations/020_the_agenda_asks_where_someone_works.sql +81 -0
- package/migrations/022_the_line_says_which_document_it_belongs_to.sql +109 -0
- package/package.json +1 -1
- package/migrations/0000_legacy_quarantine.sql +0 -86
- package/migrations/0000b_straggler_sweep.sql +0 -37
- package/migrations/000_core_v1_convert.sql +0 -343
- package/migrations/001_core.sql +0 -372
- package/migrations/002_rls_user_tenant_ids.sql +0 -64
- package/migrations/003_invitations.sql +0 -17
- package/migrations/004_archetypes.sql +0 -343
- package/migrations/005_locations_archetype.sql +0 -18
- package/migrations/006_archetype_rls.sql +0 -74
- package/migrations/007_archetype_grants.sql +0 -4
- package/migrations/008_grants.sql +0 -5
- package/migrations/009_anon_hardening.sql +0 -19
- package/migrations/010_migration_ledger.sql +0 -46
- package/migrations/011_anon_write_revoke.sql +0 -12
- package/migrations/012_rename_migration_ledger.sql +0 -61
- package/migrations/013_sequences_rls.sql +0 -22
- package/migrations/014_plan_entitlements.sql +0 -28
- package/migrations/015_agent_guard.sql +0 -108
- package/migrations/016_agent_guard_actor.sql +0 -102
- package/migrations/017_core_addresses.sql +0 -115
- package/migrations/018_global_search.sql +0 -482
- package/migrations/019_tenant_members_person_id.sql +0 -20
- package/migrations/020_fayz_projects.sql +0 -63
- package/migrations/021_tenant_roles.sql +0 -74
- package/migrations/022_invite_acceptance.sql +0 -91
- package/migrations/023_team_visibility.sql +0 -75
- package/migrations/024_analytics_engine.sql +0 -317
- package/migrations/025_created_by.sql +0 -75
- package/migrations/026_audit_trail.sql +0 -73
- package/migrations/027_domain_events.sql +0 -266
- package/migrations/028_tenant_scoped_token.sql +0 -127
- package/migrations/029_connections.sql +0 -186
- package/migrations/030_effect_idempotency.sql +0 -159
- package/migrations/031_sync_run_message.sql +0 -39
- package/migrations/032_connection_secrets.sql +0 -227
- package/migrations/033_sync_schedule.sql +0 -651
- package/migrations/034_custom_fields.sql +0 -55
- package/migrations/035_field_registry.sql +0 -148
- package/migrations/036_analytics_run_batch.sql +0 -84
- package/migrations/037_sync_tick_one_at_a_time.sql +0 -256
- package/migrations/038_onboarding_responses.sql +0 -103
- package/migrations/039_unit_tree.sql +0 -270
- package/migrations/040_resource_grants.sql +0 -474
- package/migrations/041_scoped_columns.sql +0 -192
- package/migrations/042_unit_scope_policies.sql +0 -145
- package/migrations/043_view_invoker.sql +0 -90
- package/migrations/044_unit_member_facts.sql +0 -47
- package/migrations/045_unit_entry.sql +0 -236
- package/migrations/046_membership_visible_to_members.sql +0 -85
- package/migrations/047_tasks.sql +0 -266
- package/migrations/048_every_login_is_a_person.sql +0 -190
- package/migrations/049_bookable_people.sql +0 -126
- package/migrations/050_authz_core.sql +0 -230
- package/migrations/051_authz_helpers.sql +0 -330
- package/migrations/052_authz_fanout.sql +0 -253
- package/migrations/053_authz_catalog_templates.sql +0 -402
- package/migrations/060_tenancy_seam.sql +0 -132
- package/migrations/061_create_tenant.sql +0 -125
- package/migrations/062_invitations.sql +0 -329
- package/migrations/063_legacy_tenancy_retired.sql +0 -568
- package/migrations/064_agent_guard_authz.sql +0 -118
- package/migrations/064b_supersede_unit_scope.sql +0 -205
- package/migrations/065_scaffold_sweep.sql +0 -139
- package/migrations/066_shadow_report.sql +0 -122
- package/migrations/070_permission_catalog.sql +0 -205
- package/migrations/071_v1_permission_map.sql +0 -266
- package/migrations/072_role_rpcs.sql +0 -126
- package/migrations/073_legacy_rbac_retire.sql +0 -471
- package/migrations/075_company_legal_entities.sql +0 -169
- package/migrations/076_tenant_settings.sql +0 -251
- package/migrations/080_people_roles.sql +0 -269
- package/migrations/081_people_identity.sql +0 -271
- package/migrations/082_people_membership_link.sql +0 -142
- package/migrations/083_people_data_migration.sql +0 -75
- package/migrations/084_people_registrations_tokens.sql +0 -282
- package/migrations/085_catalog_products.sql +0 -333
- package/migrations/086_catalog_pricing.sql +0 -194
- package/migrations/087_catalog_packages.sql +0 -134
- package/migrations/090_orders_venda.sql +0 -166
- package/migrations/091_orders_scaffold.sql +0 -92
- package/migrations/095_config_field_rules.sql +0 -261
- package/migrations/096_audit_append_only.sql +0 -256
- package/migrations/097_sequences_authorized.sql +0 -111
- package/migrations/098_holidays.sql +0 -80
- package/migrations/099_access_grants.sql +0 -293
- package/migrations/100_migration_schema.sql +0 -217
- package/migrations/101_migration_allowlist.sql +0 -202
- package/migrations/102_migration_rpcs.sql +0 -933
- package/migrations/103_migration_reconcile_ponr.sql +0 -132
- package/migrations/105_policies_to_authenticated.sql +0 -71
- package/migrations/106_function_search_path.sql +0 -55
- package/migrations/107_core_table_prefix.sql +0 -361
- package/migrations/108_authz_write_guards.sql +0 -149
- package/migrations/109_catalog_migration_promoted_columns.sql +0 -126
- package/migrations/110_people_kind_vocabulary.sql +0 -120
- package/migrations/111_migration_excluded_is_a_decision.sql +0 -259
- package/migrations/111_replacing_a_view_keeps_its_dependents.sql +0 -132
- package/migrations/112_money_precision_standard.sql +0 -132
- package/migrations/113_addresses_units_companies.sql +0 -114
- package/migrations/114_migrations_ledger_immutable.sql +0 -65
- package/migrations/115_registration_honours_field_rules.sql +0 -172
- package/migrations/116_field_rules_check_needs_a_tenant.sql +0 -72
- package/migrations/117_authz_reconciliation_schedule.sql +0 -82
- package/migrations/118_migration_catalog_writers.sql +0 -450
- package/migrations/119_migration_config_writers.sql +0 -193
- package/migrations/120_migration_facet_writer.sql +0 -139
- package/migrations/121_migration_financial_writers.sql +0 -294
- package/migrations/122_migration_inventory_writers.sql +0 -245
- package/migrations/123_migration_order_item_writer.sql +0 -144
- package/migrations/124_migration_ledger_writers.sql +0 -170
- package/migrations/125_migration_authz_people_writers.sql +0 -205
- package/migrations/126_owner_scoping_is_configuration.sql +0 -247
- package/migrations/127_scaffold_audit_hook.sql +0 -98
- package/migrations/128_shadow_tables_enforced.sql +0 -93
- package/migrations/129_order_kind_terminal_states.sql +0 -57
- package/migrations/130_every_login_is_a_person.sql +0 -196
- package/migrations/131_migration_invoice_split.sql +0 -127
- package/migrations/132_audit_prune_scheduled.sql +0 -28
- package/migrations/133_unit_is_a_session_property.sql +0 -155
- package/migrations/134_field_rules_on_read.sql +0 -341
- package/migrations/135_distribution_polarity.sql +0 -158
- package/migrations/136_unit_id_means_locality.sql +0 -358
- package/migrations/137_vendor_named_spine_objects.sql +0 -126
- package/migrations/138_grants_inherit_down_the_tree.sql +0 -178
- package/migrations/139_distributable_is_a_scaffold_option.sql +0 -109
- package/migrations/140_masking_is_a_scaffold_option.sql +0 -85
- package/migrations/141_renamed_objects_keep_a_compat_name.sql +0 -192
- package/migrations/142_a_person_has_addresses.sql +0 -212
- package/migrations/143_a_professional_has_a_rate.sql +0 -75
- package/migrations/144_a_product_can_be_sold_in_sizes.sql +0 -57
- package/migrations/145_the_app_says_which_tenant_it_is_in.sql +0 -123
- package/migrations/146_products_stock_has_one_writer.sql +0 -67
- package/migrations/147_every_archetype_has_a_kind_registry.sql +0 -388
- package/migrations/148_the_legacy_archetype_stops_being_writable.sql +0 -154
- package/migrations/149_the_event_log_gets_a_consumer.sql +0 -331
- package/migrations/150_the_app_is_configured_in_the_database.sql +0 -285
- package/migrations/151_stock_balance_is_an_archetype.sql +0 -144
- package/migrations/152_a_file_is_an_archetype.sql +0 -222
- package/migrations/153_a_plugin_calls_a_service_through_one_door.sql +0 -199
- package/migrations/154_the_plan_is_a_ceiling.sql +0 -258
- package/migrations/155_a_tenant_can_leave.sql +0 -271
- package/migrations/156_the_service_door_has_a_server_side.sql +0 -335
- package/migrations/157_nobody_creates_objects_in_public.sql +0 -74
- package/migrations/158_the_registry_owns_the_status_vocabulary.sql +0 -153
- package/migrations/159_the_plan_catalogue_gets_its_first_rows.sql +0 -132
- package/migrations/160_the_vertical_decides_what_starts_on.sql +0 -246
- package/migrations/161_a_tenant_leaves_with_its_data.sql +0 -363
- package/migrations/162_every_name_has_one_creator_for_real.sql +0 -126
- package/migrations/163_a_species_nobody_declared_goes_to_quarantine.sql +0 -184
- package/migrations/164_the_sequence_table_joins_the_register.sql +0 -26
- package/migrations/165_a_view_is_not_a_way_in.sql +0 -90
- package/migrations/166_the_platform_billing_leftovers_go.sql +0 -72
- package/migrations/167_a_view_gets_back_what_its_table_grants.sql +0 -112
- package/migrations/168_the_grant_census_stops_crying_wolf.sql +0 -33
- package/migrations/169_the_census_asks_postgres_instead_of_guessing.sql +0 -45
|
@@ -1,270 +0,0 @@
|
|
|
1
|
-
-- ============================================================================
|
|
2
|
-
-- 039_unit_tree.sql — the org tree `locations` always was, finally readable.
|
|
3
|
-
--
|
|
4
|
-
-- WHAT A UNIT IS. A franchise brand is ONE tenant with N units: "Alem do Olhar"
|
|
5
|
-
-- has 15 of them. The unit is not a new noun — it is `public.locations`, which
|
|
6
|
-
-- 001_core created with `is_headquarters` and `kind DEFAULT 'branch'` and which
|
|
7
|
-
-- nothing has ever read for authorization. Two kinds and only two:
|
|
8
|
-
--
|
|
9
|
-
-- kind='region' groups units kind='branch' IS the unit
|
|
10
|
-
--
|
|
11
|
-
-- A ROOM IS NOT A UNIT. Sala, cadeira, mesa, consultório are *service
|
|
12
|
-
-- locations* — operational places that HAVE a unit — and they do not belong in
|
|
13
|
-
-- this table; mixing "where the chair is" with "who may see the money" is how
|
|
14
|
-
-- an authorization model stops being explainable. No room concept exists in the
|
|
15
|
-
-- code today (`kind='room'`/`'zone'` appears in prose documentation and in no
|
|
16
|
-
-- table and no screen), so there is nothing to migrate and the tree is exactly
|
|
17
|
-
-- two levels deep.
|
|
18
|
-
--
|
|
19
|
-
-- WHAT THIS FILE DOES NOT DO. Nothing here changes what any query returns. It
|
|
20
|
-
-- adds the tree, the bindings, the per-tenant switch and the five helper
|
|
21
|
-
-- functions; 042 is the file that turns them into a boundary, and even that one
|
|
22
|
-
-- is inert until a tenant sets `unit_scoping_enabled`.
|
|
23
|
-
--
|
|
24
|
-
-- IT ALSO REPAIRS `location_members`. That table has existed since 001_core
|
|
25
|
-
-- with a SELECT policy and NOTHING else: no INSERT, no UPDATE, no DELETE, so no
|
|
26
|
-
-- screen has ever been able to write a binding. It also has no index leading
|
|
27
|
-
-- with `user_id`, which would make `user_unit_ids()` seq-scan it on every
|
|
28
|
-
-- single query. Both are fixed here.
|
|
29
|
-
--
|
|
30
|
-
-- Additive and idempotent: safe to re-run, no-ops when already applied.
|
|
31
|
-
-- ============================================================================
|
|
32
|
-
|
|
33
|
-
-- ── The tree ────────────────────────────────────────────────────────────────
|
|
34
|
-
ALTER TABLE public.locations
|
|
35
|
-
ADD COLUMN IF NOT EXISTS parent_id uuid,
|
|
36
|
-
ADD COLUMN IF NOT EXISTS code text;
|
|
37
|
-
|
|
38
|
-
COMMENT ON COLUMN public.locations.parent_id IS
|
|
39
|
-
'Parent node of the org tree: a branch points at its region. Two levels only.';
|
|
40
|
-
COMMENT ON COLUMN public.locations.code IS
|
|
41
|
-
'The unit code the franchise already uses on its own paperwork (e.g. "RJ-042").';
|
|
42
|
-
|
|
43
|
-
-- A parent in ANOTHER tenant would be a cross-tenant edge inside the very
|
|
44
|
-
-- structure that decides visibility. The composite FK makes it unrepresentable
|
|
45
|
-
-- rather than merely discouraged — cheaper and more honest than a trigger.
|
|
46
|
-
DO $$
|
|
47
|
-
BEGIN
|
|
48
|
-
IF NOT EXISTS (
|
|
49
|
-
SELECT 1 FROM pg_constraint WHERE conname = 'locations_id_tenant_uniq'
|
|
50
|
-
) THEN
|
|
51
|
-
ALTER TABLE public.locations ADD CONSTRAINT locations_id_tenant_uniq UNIQUE (id, tenant_id);
|
|
52
|
-
END IF;
|
|
53
|
-
|
|
54
|
-
IF NOT EXISTS (
|
|
55
|
-
SELECT 1 FROM pg_constraint WHERE conname = 'locations_parent_same_tenant'
|
|
56
|
-
) THEN
|
|
57
|
-
ALTER TABLE public.locations
|
|
58
|
-
ADD CONSTRAINT locations_parent_same_tenant
|
|
59
|
-
FOREIGN KEY (parent_id, tenant_id)
|
|
60
|
-
REFERENCES public.locations (id, tenant_id) ON DELETE SET NULL;
|
|
61
|
-
END IF;
|
|
62
|
-
|
|
63
|
-
-- Self-parenting is the one cycle a CHECK can catch. Deeper cycles are
|
|
64
|
-
-- survivable by construction: user_unit_ids() walks with UNION (which dedupes
|
|
65
|
-
-- and therefore terminates) plus a depth guard. NOT VALID so no live table is
|
|
66
|
-
-- scanned; new and updated rows are checked from here on.
|
|
67
|
-
IF NOT EXISTS (
|
|
68
|
-
SELECT 1 FROM pg_constraint WHERE conname = 'locations_no_self_parent'
|
|
69
|
-
) THEN
|
|
70
|
-
ALTER TABLE public.locations
|
|
71
|
-
ADD CONSTRAINT locations_no_self_parent CHECK (parent_id IS DISTINCT FROM id) NOT VALID;
|
|
72
|
-
END IF;
|
|
73
|
-
END $$;
|
|
74
|
-
|
|
75
|
-
CREATE INDEX IF NOT EXISTS locations_parent_idx
|
|
76
|
-
ON public.locations (parent_id) WHERE parent_id IS NOT NULL;
|
|
77
|
-
|
|
78
|
-
-- ── The bindings ────────────────────────────────────────────────────────────
|
|
79
|
-
-- `tenant_id` denormalised so this table's own policies take the canonical
|
|
80
|
-
-- shape instead of joining back through `locations` on every check.
|
|
81
|
-
ALTER TABLE public.location_members
|
|
82
|
-
ADD COLUMN IF NOT EXISTS tenant_id uuid REFERENCES public.tenants(id) ON DELETE CASCADE;
|
|
83
|
-
|
|
84
|
-
UPDATE public.location_members lm
|
|
85
|
-
SET tenant_id = l.tenant_id
|
|
86
|
-
FROM public.locations l
|
|
87
|
-
WHERE l.id = lm.location_id
|
|
88
|
-
AND lm.tenant_id IS DISTINCT FROM l.tenant_id;
|
|
89
|
-
|
|
90
|
-
-- The column is derived, so deriving it is the trigger's job — a caller that
|
|
91
|
-
-- has to remember to pass it will eventually not, and a binding with a NULL
|
|
92
|
-
-- tenant is invisible to every policy below.
|
|
93
|
-
CREATE OR REPLACE FUNCTION public.handle_location_member_tenant()
|
|
94
|
-
RETURNS trigger
|
|
95
|
-
LANGUAGE plpgsql
|
|
96
|
-
SECURITY DEFINER
|
|
97
|
-
SET search_path = public, pg_temp
|
|
98
|
-
AS $$
|
|
99
|
-
BEGIN
|
|
100
|
-
SELECT l.tenant_id INTO NEW.tenant_id
|
|
101
|
-
FROM public.locations l WHERE l.id = NEW.location_id;
|
|
102
|
-
RETURN NEW;
|
|
103
|
-
END;
|
|
104
|
-
$$;
|
|
105
|
-
|
|
106
|
-
REVOKE ALL ON FUNCTION public.handle_location_member_tenant() FROM public, anon;
|
|
107
|
-
|
|
108
|
-
DROP TRIGGER IF EXISTS location_members_tenant ON public.location_members;
|
|
109
|
-
CREATE TRIGGER location_members_tenant
|
|
110
|
-
BEFORE INSERT OR UPDATE OF location_id ON public.location_members
|
|
111
|
-
FOR EACH ROW EXECUTE FUNCTION public.handle_location_member_tenant();
|
|
112
|
-
|
|
113
|
-
-- Without this index every RLS-evaluated call of user_unit_ids() seq-scans the
|
|
114
|
-
-- table. 001_core's only index is UNIQUE(location_id, user_id) — wrong leading
|
|
115
|
-
-- column for the one question this table is ever asked.
|
|
116
|
-
CREATE INDEX IF NOT EXISTS location_members_user_idx
|
|
117
|
-
ON public.location_members (user_id, location_id);
|
|
118
|
-
|
|
119
|
-
-- A binding with no role is the normal case: it says "you work here", and the
|
|
120
|
-
-- role you already have in the org applies. A role here only ever ADDS.
|
|
121
|
-
ALTER TABLE public.location_members ALTER COLUMN role DROP DEFAULT;
|
|
122
|
-
|
|
123
|
-
COMMENT ON COLUMN public.location_members.role IS
|
|
124
|
-
'Additional role INSIDE this unit. Unions with the org role, never subtracts. NULL = no extra role.';
|
|
125
|
-
|
|
126
|
-
-- ── The per-tenant switch ───────────────────────────────────────────────────
|
|
127
|
-
-- A column and not a key in `tenants.settings`, following the precedent shop
|
|
128
|
-
-- 0020 set with `storefront_strict_scope`: a security flag read inside a policy
|
|
129
|
-
-- has to be cheap and indexable, and a jsonb extraction is neither.
|
|
130
|
-
ALTER TABLE public.tenants
|
|
131
|
-
ADD COLUMN IF NOT EXISTS unit_scoping_enabled boolean NOT NULL DEFAULT false;
|
|
132
|
-
|
|
133
|
-
COMMENT ON COLUMN public.tenants.unit_scoping_enabled IS
|
|
134
|
-
'Off by default. While false, every unit predicate in 042 short-circuits and this tenant behaves exactly as before.';
|
|
135
|
-
|
|
136
|
-
-- ── Helpers ─────────────────────────────────────────────────────────────────
|
|
137
|
-
-- Every one of these is zero-argument on purpose. A helper that takes the row's
|
|
138
|
-
-- own column (is_tenant_admin(tenant_id) is the cautionary example) is
|
|
139
|
-
-- correlated: RLS injects the predicate into securityQuals, where the planner
|
|
140
|
-
-- never turns a sublink into a semijoin, so it becomes a SubPlan re-executed
|
|
141
|
-
-- once PER ROW. Zero-argument helpers wrapped in `(SELECT …)` become one
|
|
142
|
-
-- InitPlan per query, hashed, then a probe per row.
|
|
143
|
-
--
|
|
144
|
-
-- All are SECURITY DEFINER for the reason 023 spells out: they read the very
|
|
145
|
-
-- tables whose policies will call them, and invoker semantics would recurse.
|
|
146
|
-
|
|
147
|
-
-- The pool-level short circuit. On a pool where nobody uses units this returns
|
|
148
|
-
-- false once per query and the whole predicate in 042 costs one boolean test
|
|
149
|
-
-- per row — no hash build, because OR does not evaluate its later arms.
|
|
150
|
-
CREATE OR REPLACE FUNCTION public.unit_scoping_active()
|
|
151
|
-
RETURNS boolean
|
|
152
|
-
LANGUAGE sql STABLE SECURITY DEFINER
|
|
153
|
-
SET search_path = public, pg_temp
|
|
154
|
-
AS $$
|
|
155
|
-
SELECT EXISTS (SELECT 1 FROM public.tenants WHERE unit_scoping_enabled);
|
|
156
|
-
$$;
|
|
157
|
-
|
|
158
|
-
CREATE OR REPLACE FUNCTION public.unit_scoped_tenants()
|
|
159
|
-
RETURNS SETOF uuid
|
|
160
|
-
LANGUAGE sql STABLE SECURITY DEFINER
|
|
161
|
-
SET search_path = public, pg_temp
|
|
162
|
-
AS $$
|
|
163
|
-
SELECT id FROM public.tenants WHERE unit_scoping_enabled;
|
|
164
|
-
$$;
|
|
165
|
-
|
|
166
|
-
-- The tenants where the caller may see everything regardless of unit. Mirrors
|
|
167
|
-
-- is_tenant_admin's role set exactly (001_core:87) — divergence here would mean
|
|
168
|
-
-- the settings screens and the row predicate disagree about who is an admin.
|
|
169
|
-
CREATE OR REPLACE FUNCTION public.user_admin_tenant_ids()
|
|
170
|
-
RETURNS SETOF uuid
|
|
171
|
-
LANGUAGE sql STABLE SECURITY DEFINER
|
|
172
|
-
SET search_path = public, pg_temp
|
|
173
|
-
AS $$
|
|
174
|
-
SELECT tm.tenant_id
|
|
175
|
-
FROM public.tenant_members tm
|
|
176
|
-
WHERE tm.user_id = auth.uid()
|
|
177
|
-
AND tm.role IN ('owner', 'admin');
|
|
178
|
-
$$;
|
|
179
|
-
|
|
180
|
-
-- The units the caller reaches. A binding to a region reaches its branches;
|
|
181
|
-
-- reach flows DOWN the tree and never up — a unit manager is not a regional.
|
|
182
|
-
--
|
|
183
|
-
-- Deliberately NOT tenant-aware: a user can belong to tenant A with units on
|
|
184
|
-
-- and tenant B with units off, and one answer cannot serve both. The mode check
|
|
185
|
-
-- lives in the predicate (042), where it is free.
|
|
186
|
-
CREATE OR REPLACE FUNCTION public.user_unit_ids()
|
|
187
|
-
RETURNS SETOF uuid
|
|
188
|
-
LANGUAGE sql STABLE SECURITY DEFINER
|
|
189
|
-
SET search_path = public, pg_temp
|
|
190
|
-
AS $$
|
|
191
|
-
WITH RECURSIVE seed AS (
|
|
192
|
-
SELECT lm.location_id AS id, 0 AS depth
|
|
193
|
-
FROM public.location_members lm
|
|
194
|
-
WHERE lm.user_id = auth.uid()
|
|
195
|
-
),
|
|
196
|
-
tree AS (
|
|
197
|
-
SELECT id, depth FROM seed
|
|
198
|
-
UNION -- UNION, not UNION ALL: dedupes, and
|
|
199
|
-
SELECT l.id, t.depth + 1 -- therefore terminates on a cycle
|
|
200
|
-
FROM public.locations l
|
|
201
|
-
JOIN tree t ON l.parent_id = t.id
|
|
202
|
-
WHERE t.depth < 8 -- a policy must never be able to hang
|
|
203
|
-
)
|
|
204
|
-
SELECT id FROM tree;
|
|
205
|
-
$$;
|
|
206
|
-
|
|
207
|
-
-- The unit the browser says it is looking at, read from a request header the
|
|
208
|
-
-- same way shop 0020 reads `x-fayz-store`. It NARROWS a write's default stamp;
|
|
209
|
-
-- it never grants anything — 042 validates the stamped unit against
|
|
210
|
-
-- user_unit_ids() in WITH CHECK. Swallows every parse failure for the reason
|
|
211
|
-
-- 028 states: an RLS-adjacent function is not a place to raise.
|
|
212
|
-
CREATE OR REPLACE FUNCTION public.requested_unit()
|
|
213
|
-
RETURNS uuid
|
|
214
|
-
LANGUAGE plpgsql STABLE
|
|
215
|
-
SET search_path = public, pg_temp
|
|
216
|
-
AS $$
|
|
217
|
-
DECLARE v text;
|
|
218
|
-
BEGIN
|
|
219
|
-
v := nullif(current_setting('request.headers', true), '')::json ->> 'x-fayz-unit';
|
|
220
|
-
RETURN nullif(v, '')::uuid;
|
|
221
|
-
EXCEPTION WHEN OTHERS THEN
|
|
222
|
-
RETURN NULL;
|
|
223
|
-
END;
|
|
224
|
-
$$;
|
|
225
|
-
|
|
226
|
-
GRANT EXECUTE ON FUNCTION public.unit_scoping_active() TO authenticated, service_role;
|
|
227
|
-
GRANT EXECUTE ON FUNCTION public.unit_scoped_tenants() TO authenticated, service_role;
|
|
228
|
-
GRANT EXECUTE ON FUNCTION public.user_admin_tenant_ids() TO authenticated, service_role;
|
|
229
|
-
GRANT EXECUTE ON FUNCTION public.user_unit_ids() TO authenticated, service_role;
|
|
230
|
-
GRANT EXECUTE ON FUNCTION public.requested_unit() TO authenticated, service_role;
|
|
231
|
-
|
|
232
|
-
-- ── Policies on the tree itself ─────────────────────────────────────────────
|
|
233
|
-
-- `locations` and `location_members` are in 002's core_tables exclusion list,
|
|
234
|
-
-- so they carry curated policies and no sweep will overwrite these.
|
|
235
|
-
--
|
|
236
|
-
-- RLS is re-enabled rather than assumed: 001_core enables it, but a pool built
|
|
237
|
-
-- by hand (the ecommerce pool has tables the ledger never heard of) may not have
|
|
238
|
-
-- run 001, and a policy on a table without RLS is decoration.
|
|
239
|
-
ALTER TABLE public.locations ENABLE ROW LEVEL SECURITY;
|
|
240
|
-
ALTER TABLE public.location_members ENABLE ROW LEVEL SECURITY;
|
|
241
|
-
--
|
|
242
|
-
-- The tree stays readable tenant-wide even under unit scoping, on purpose: the
|
|
243
|
-
-- unit switcher, the "Unidade: Ipanema" label on a record and the reports
|
|
244
|
-
-- breakdown all need to name a unit the reader may not have data in. A unit's
|
|
245
|
-
-- NAME is not its data.
|
|
246
|
-
DROP POLICY IF EXISTS "locations_select" ON public.locations;
|
|
247
|
-
CREATE POLICY "locations_select" ON public.locations FOR SELECT TO authenticated
|
|
248
|
-
USING (tenant_id IN (SELECT public.user_tenant_ids()));
|
|
249
|
-
|
|
250
|
-
DROP POLICY IF EXISTS "loc_members_select" ON public.location_members;
|
|
251
|
-
CREATE POLICY "loc_members_select" ON public.location_members FOR SELECT TO authenticated
|
|
252
|
-
USING (tenant_id IN (SELECT public.user_tenant_ids()));
|
|
253
|
-
|
|
254
|
-
-- The missing three quarters of the table. Only a tenant admin hands out
|
|
255
|
-
-- access to a unit — the same bar `tenant_role_overrides` and `invitations`
|
|
256
|
-
-- already set for who may change who can do what.
|
|
257
|
-
DROP POLICY IF EXISTS "loc_members_insert" ON public.location_members;
|
|
258
|
-
CREATE POLICY "loc_members_insert" ON public.location_members FOR INSERT TO authenticated
|
|
259
|
-
WITH CHECK (tenant_id IN (SELECT public.user_admin_tenant_ids()));
|
|
260
|
-
|
|
261
|
-
DROP POLICY IF EXISTS "loc_members_update" ON public.location_members;
|
|
262
|
-
CREATE POLICY "loc_members_update" ON public.location_members FOR UPDATE TO authenticated
|
|
263
|
-
USING (tenant_id IN (SELECT public.user_admin_tenant_ids()));
|
|
264
|
-
|
|
265
|
-
DROP POLICY IF EXISTS "loc_members_delete" ON public.location_members;
|
|
266
|
-
CREATE POLICY "loc_members_delete" ON public.location_members FOR DELETE TO authenticated
|
|
267
|
-
USING (tenant_id IN (SELECT public.user_admin_tenant_ids()));
|
|
268
|
-
|
|
269
|
-
GRANT SELECT, INSERT, UPDATE, DELETE ON public.location_members TO authenticated;
|
|
270
|
-
GRANT ALL ON public.location_members TO service_role;
|