سامانه صدور فاکتور و حسابداری
Mirrored from https://github.com/davoodmehraban89/Factor-easy.git
Synced Dec 3, 22:00 UTC
c8-workflow-task1
audit-remediation-wave3-4-20261002
audit-remediation-wave3-verify-20261002
audit-remediation-wave4-20261002
audit-remediation-wave4b-20261002
audit-remediation-wave5a-identity-20261002
audit-remediation-wave5b-offboarding-20261002
audit-remediation-wave5c-authorization-20261002
audit-security-intent-review-20261002
audit/finora-financial-core-20261115
audit/finora-master-prompt-phase0-20261016
audit/finora-stage3-legacy-source-readonly-20261023
audit/full-controls-20261030
audit/owner-live-state-reconcile-20261012
audit/owner-review-20261007
audit/phase1-live-permissions-readonly-20261013
audit/reconcile-live-state-20261006
audit/reconcile-state-20261002
audit/wp0-hardening-baseline-20261102
branding-login-favicon-20261002
c8-workflow-task1
c8-workflow-task2
c8-workflow-task3
chore/wave0-pages-gate-closeout
claude-code-plugins-setup
design/finora-c01-authoritative-source-draft-20261124
docs-wave4b-verified-20261002
docs/active-company-invoice-release-evidence-20261014
docs/agent-operating-model-20261114
docs/chart-taxpayer-release-evidence-20261004
docs/cloud-only-agent-orchestration-20261115
docs/confirm-finora-product-charter-20261115
docs/continuation-20261030
docs/d4a-verified
docs/external-work-review-20261121
docs/finora-audit-continuity-20261014
docs/finora-backup-merge-evidence-20261014
docs/finora-ci-reconciliation-20261122
docs/finora-codex-master-prompt
docs/finora-eight-phase-plan-20261011
docs/finora-f002-live-buyer-filter-verified-20261015
docs/finora-license-race-release-evidence-20261014
docs/finora-license-rollout-acceptance-20261015
docs/finora-phase0-repeat-and-pages-nav-verified-20261015
docs/finora-phase0-repeat-evidence-20261015
docs/finora-selectors-release-and-fixture-qa-20261015
docs/finora-stagea-pr240-ci-triage-20261123
docs/finora-test-license-live-pass-20261015
docs/finora-v2-m01-coverage-20261124
docs/handoff-charter-v2-20261124
docs/invoice-product-search-release-20261011
docs/invoice-workspace-release-20261007
docs/phase-status-20261031
docs/phase0-acceptance-release-evidence-20261012
docs/phase0-admin-provision-release-evidence-20261012
docs/phase0-core-bundle-owner-login-open-20261013
docs/phase0-duplicate-js-release-20261016
docs/phase0-f001-auth-rest-shared-timeout-20261013
docs/phase0-f001-owner-login-observed-20261013
docs/phase0-f001-second-device-fail-40s-20261013
docs/phase0-f003-release-verification-20261011
docs/phase0-f004-print-release-evidence-20261012
docs/phase0-f004-watermark-release-evidence-20261012
docs/phase0-f005-large-selector-release-20261012
docs/phase0-f005-release-evidence-20261012
docs/phase0-f006-dark-release-evidence-20261012
docs/phase0-f008-final-pr-closure-evidence-20261012
docs/phase0-f008-reconcile-stale-prs-20261012
docs/phase0-f107-production-evidence-20261012
docs/phase0-kavenegar-otp-preflight-release-20261012
docs/phase0-login-release-evidence-20261012
docs/phase0-login-rest-bootstrap-stage-release-20261012
docs/phase0-owner-app-assets-bundle-release-20261013
docs/phase0-owner-login-csp-release-20261012
docs/phase0-reconcile-postmerge-evidence-20261012
docs/phase0-site-probe-release-evidence-20261012
docs/phase0-startup-release-evidence-20261012
docs/phase3-treasury-release-evidence-20261014
docs/pilot-posted-correction-release-evidence-20261014
docs/pilot-purchase-correction-release-evidence-20261014
docs/print-persian-release-verification-20261011
docs/qa-ci-triage-218-20261122
docs/real-account-qa-20261030
docs/release-state-20261007
docs/seamless-amendment-release-20261014
docs/seamless-document-correction-release-20261014
docs/settings-login-release-20261025
docs/stripe-qa-deployment-evidence-20261011
docs/ux-f2-release-evidence-20261011
docs/ux-f2-release-verification-20261011
docs/wave0-closeout
docs/wave2-closeout
exec-phase1-architecture
exec-phase1-implementation
exec-phase1-qa
exec-phase1-security-release
exec-phase1-ux
feat/accounting-document-draft-approval-20261011
feat/accounting-document-release-candidate-20261011
feat/active-company-strict-isolation-20261014
feat/admin-provisioning-reconciled-20261011
feat/admin-secure-user-provisioning-20261011
feat/admin-user-provisioning-merged-20261011
feat/d4a-request-work-queue
feat/document-lifecycle-atf-20261118
feat/document-lifecycle-temp-permanent-atef
feat/draft-first-attachment-lifecycle-216
feat/finora-offline-encrypted-backup-20261014
feat/finora-phase-a-navigation-20261023
feat/finora-stripe-sandbox-edge-gate
feat/immutable-attachment-temporary-vouchers
feat/iran-accounting-design-audit
feat/journal-document-lifecycle-20261118
feat/journal-rpc-repository-adapter-20261117
feat/office-c7-fulltext-ocr-provenance
feat/password-recovery-admin-reset-20261025
feat/pilot-expense-income-inplace-amendment-20261014
feat/pilot-journal-invoice-correction-20261014
feat/pilot-purchase-correction-20261014
feat/pilot-treasury-unallocated-correction-20261014
feat/retain-document-editor-after-save-20261014
feat/seamless-financial-document-correction-20261014
feat/slice-b-tenant-rbac
feat/stripe-sandbox-invoice-probe
feature/accounting-core-p0
feature/accounting-product-guidance
feature/admin-identity-phone-auth
feature/commercial-rnd
feature/finora-phase1a-supabase-cli-20261016
feature/finora-stage2-manual-canary-20261017
feature/finora-stage3-commercial-payload-20261023
feature/finora-stage3-enforce-invoice-rpc-20261023
feature/finora-stage3-enforce-purchase-red-20261023
feature/invoice-workspace-cleanup
feature/phase0-kavenegar-otp-preflight-20261012
feature/ux-commerce-settings-r2
fix-shell-scroll-live-cache-20261002
fix-shell-scrollbar-explicit-ownership-20261002
fix-shell-scrollbar-ownership-20261002
fix-shell-scrollbar-right-edge-20261002
fix/about-bug-report-20261114
fix/active-company-invoice-scope-20261014
fix/active-company-isolation-reconcile-20261014
fix/admin-central-password-reset-20261028
fix/auth-network-diagnostics
fix/chart-taxpayer-reference-20261003
fix/combobox-enter-reliability
fix/combobox-live-search
fix/combobox-production-cache
fix/combobox-release-asset-v2
fix/company-edit-action-20261028
fix/company-edit-pilot-delete-20261014
fix/company-form-action-state-20261014
fix/company-form-action-state-rebased-20261014
fix/contact-operational-balance-20261111
fix/dark-theme-contrast
fix/financial-lifecycle-recovery-20261124
fix/finora-accounting-test-organization-readiness-20261015
fix/finora-b01-safe-org-switch-20261124
fix/finora-b02-sync-recovery-20261124
fix/finora-b03-conflict-review-20261124
fix/finora-org-license-bootstrap-race-20261014
fix/finora-stage3-posted-source-immutable-20261023
fix/finora-v2-b03-r2-qa-20261124
fix/finora-v2-b03-r3-readonly-20261124
fix/g01-asof-financial-position-20261128
fix/invoice-product-search-rehydrate-20261011
fix/invoice-search-cache-bust
fix/issue229-linked-draft-status-20261122
fix/login-assets-retry-20261014
fix/login-inert-startup-20261110
fix/login-member-access-20261101
fix/login-resilience-20261017
fix/login-short-viewport-20261028
fix/login-startup-readiness-20261024
fix/navigation-utilities-placement-20261105
fix/office-c7-source-ref-binding
fix/office-c7-source-ref-binding-v2
fix/owner-login-network-20261026
fix/password-recovery-invalid-cancel-20261026
fix/payment-amount-format-20261111
fix/phase0-admin-provision-seat-bootstrap-20261012
fix/phase0-auth-bundled-assets-for-unstable-isp-20261013
fix/phase0-auth-handler-binding-stalled-session-20261012
fix/phase0-login-context-stage-visibility-20261012
fix/phase0-login-single-local-bundle-20261013
fix/phase0-login-site-probe-clarity-20261012
fix/phase0-login-ui-ready-20261012
fix/phase0-modal-dashboard-drill-20261011
fix/phase0-pages-blocking-core-bundles-20261013
fix/phase0-print-no-draft-watermark-20261012
fix/phase0-startup-parallel-asset-load-20261012
fix/phase3-behavioral-verification
fix/preserve-pending-edits-20261121
fix/print-persian-digit-consistency-20261011
fix/recovery-login-startup-20261027
fix/report-opening-balance-release-20261130
fix/settings-checkbox-persistence-20261024
fix/taxpayer-arithmetic-20261004
fix/ux-keyboard-search-f2-20261011
fix/wave2-request-input-integrity
hardening/c01-source-draft-20261129
hardening/global-draft-conversion-20261113
hardening/wp1-ledger-integrity-20261102
integration/finora-execution-20261124
integration/issue229-ui-qa-20261122
integration/wp1-ledger-core-20261115
integration/wp2-manual-journal-rpc-20261116
main
ops-heartbeat-v2-20261002
ops-progress-heartbeat-rule-20261002
ops/wave4-production-20261003
plan/financial-core-phase1-approval-20261016
qa/attachment-allocator-two-session-20261122
qa/draft-first-save-red-evidence-20261122
qa/finora-autopost-retry-identity-red-20261122
qa/finora-identity-rpc-security-tripwire-20261122
qa/finora-issue223-manual-kardex-browser-red-20261123
qa/finora-issue223-stock-real-jwt-20261123
qa/finora-issue238-inventory-count-browser-red-20261123
qa/finora-issue238-save-browser-red-20261122
qa/finora-login-cors-header-preflight-20261025
qa/finora-phase0-saved-session-repeat-20261015
qa/finora-phase5-posted-insert-jwt-red-20261123
qa/finora-stage3-ui-jwt-autopost-20261023
qa/finora-v2-g01-report-reconciliation-20261125
qa/free-supabase-local-wp2-20261116
qa/issue229-approval-boundary-jwt-20261122
qa/issue229-sale-save-linked-draft-red-20261122
qa/phase0-acceptance-start-20261012
qa/phase0-f005-account-project-contract-stress-20261012
qa/phase0-f005-selector-stress-20261012
qa/phase0-f006-dark-contrast-20261012
qa/post-wave4-verification-20261003
qa/posted-source-immutable-jwt-20261122
qa/pr219-wp1-disposable-dburl-proof-20261123
qa/purchase-source-jwt-20261122
qa/real-settings-acceptance-20261025
qa/recovery-mail-acceptance-20261026
qa/source-autopost-jwt-atomicity-20261122
qa/wave0-negative-gate-proof
qa/wp2-auth-tenant-e2e-20261117
qa/wp2-authorized-journal-write-20261122
qa/wp2-financial-paths-reconciliation-20261122
qa/wp3-financial-tenant-isolation-20261117
remediation/wave0-liveqa-wave1
remediation/wave2-request-integrity
staging/finora-stage22-free-20261022
state-scroll-verified-20261002
state-wave4b-production-20261002
state-wave5a-production-20261002
test/finora-accounting-boot-readiness-20261015
test/finora-f0-02-production-selectors-20261015
test/finora-pages-commerce-event-wiring-20261015
test/finora-pages-invoice-selectors-readonly-20261015
test/finora-stage3-jwt-source-post-20261023
test/phase0-invoice-fixture-bootstrap-20261016
test/phase0-invoice-persistence-20261011
test/phase0-pages-delayed-dispatcher-20261016
ux-scroll-ownership-20261002
work/finora-stage3-enforce-purchase-green-20261023
work/finora-stage3-enforce-sale-green-20261023
work/finora-stage3-payload-green-20261023
| 1 | -- ELI-2: reusable organization roles and effective-permission union. |
| 2 | |
| 3 | create table if not exists public.organization_roles ( |
| 4 | id uuid primary key default gen_random_uuid(), |
| 5 | organization_id uuid not null references public.organizations(id) on delete cascade, |
| 6 | role_key text not null check (role_key ~ '^[a-z][a-z0-9_]{1,63}$'), |
| 7 | title text not null check (char_length(trim(title)) between 1 and 120), |
| 8 | description text, |
| 9 | is_system boolean not null default false, |
| 10 | active boolean not null default true, |
| 11 | created_by uuid references public.profiles(id) on delete set null, |
| 12 | created_at timestamptz not null default now(), |
| 13 | updated_at timestamptz not null default now(), |
| 14 | unique (organization_id,role_key) |
| 15 | ); |
| 16 | |
| 17 | create table if not exists public.organization_role_permissions ( |
| 18 | id uuid primary key default gen_random_uuid(), |
| 19 | organization_id uuid not null references public.organizations(id) on delete cascade, |
| 20 | role_id uuid not null references public.organization_roles(id) on delete cascade, |
| 21 | module_key text not null check (module_key = any(array[ |
| 22 | 'core','full_suite','accounting','commerce','treasury','contracting','inventory','assets', |
| 23 | 'hr_payroll','office_automation','requests_workflow','crm','transport','manufacturing', |
| 24 | 'maintenance','group_consolidation','analytics' |
| 25 | ]::text[])), |
| 26 | capabilities text[] not null default array['read']::text[], |
| 27 | scope_type text not null default 'own' check (scope_type in ('own','unit','branch','company','organization')), |
| 28 | scope_id text, |
| 29 | confidentiality_level smallint not null default 0 check (confidentiality_level between 0 and 3), |
| 30 | created_at timestamptz not null default now(), |
| 31 | updated_at timestamptz not null default now(), |
| 32 | check (capabilities <@ array['read','create','edit','delete','approve','register','refer','archive','configure']::text[]), |
| 33 | check (cardinality(capabilities)>0), |
| 34 | check ((scope_type in ('own','organization') and scope_id is null) or (scope_type in ('unit','branch','company') and nullif(trim(scope_id),'') is not null)), |
| 35 | unique nulls not distinct (organization_id,role_id,module_key,scope_type,scope_id) |
| 36 | ); |
| 37 | |
| 38 | create table if not exists public.organization_member_roles ( |
| 39 | id uuid primary key default gen_random_uuid(), |
| 40 | organization_id uuid not null references public.organizations(id) on delete cascade, |
| 41 | member_id uuid not null references public.organization_members(id) on delete cascade, |
| 42 | role_id uuid not null references public.organization_roles(id) on delete cascade, |
| 43 | assigned_by uuid references public.profiles(id) on delete set null, |
| 44 | active boolean not null default true, |
| 45 | created_at timestamptz not null default now(), |
| 46 | updated_at timestamptz not null default now(), |
| 47 | unique (organization_id,member_id,role_id) |
| 48 | ); |
| 49 | |
| 50 | create index if not exists organization_roles_org_idx on public.organization_roles(organization_id,active,role_key); |
| 51 | create index if not exists organization_role_permissions_role_idx on public.organization_role_permissions(role_id,module_key); |
| 52 | create index if not exists organization_member_roles_member_idx on public.organization_member_roles(member_id,active); |
| 53 | create index if not exists organization_member_roles_role_idx on public.organization_member_roles(role_id,active); |
| 54 | |
| 55 | alter table public.organization_roles enable row level security; |
| 56 | alter table public.organization_role_permissions enable row level security; |
| 57 | alter table public.organization_member_roles enable row level security; |
| 58 | |
| 59 | grant select,insert,update,delete on public.organization_roles to authenticated; |
| 60 | grant select,insert,update,delete on public.organization_role_permissions to authenticated; |
| 61 | grant select,insert,update,delete on public.organization_member_roles to authenticated; |
| 62 | |
| 63 | create policy organization_roles_select on public.organization_roles for select to authenticated |
| 64 | using (organization_id in (select private.current_organization_ids())); |
| 65 | create policy organization_roles_mutate_insert on public.organization_roles for insert to authenticated |
| 66 | with check (private.is_org_owner(organization_id) or private.has_org_capability(organization_id,'core','configure')); |
| 67 | create policy organization_roles_mutate_update on public.organization_roles for update to authenticated |
| 68 | using (private.is_org_owner(organization_id) or private.has_org_capability(organization_id,'core','configure')) |
| 69 | with check (private.is_org_owner(organization_id) or private.has_org_capability(organization_id,'core','configure')); |
| 70 | create policy organization_roles_mutate_delete on public.organization_roles for delete to authenticated |
| 71 | using (private.is_org_owner(organization_id) or private.has_org_capability(organization_id,'core','configure')); |
| 72 | |
| 73 | create policy organization_role_permissions_select on public.organization_role_permissions for select to authenticated |
| 74 | using (organization_id in (select private.current_organization_ids())); |
| 75 | create policy organization_role_permissions_mutate_insert on public.organization_role_permissions for insert to authenticated |
| 76 | with check (private.is_org_owner(organization_id) or private.has_org_capability(organization_id,'core','configure')); |
| 77 | create policy organization_role_permissions_mutate_update on public.organization_role_permissions for update to authenticated |
| 78 | using (private.is_org_owner(organization_id) or private.has_org_capability(organization_id,'core','configure')) |
| 79 | with check (private.is_org_owner(organization_id) or private.has_org_capability(organization_id,'core','configure')); |
| 80 | create policy organization_role_permissions_mutate_delete on public.organization_role_permissions for delete to authenticated |
| 81 | using (private.is_org_owner(organization_id) or private.has_org_capability(organization_id,'core','configure')); |
| 82 | |
| 83 | create policy organization_member_roles_select on public.organization_member_roles for select to authenticated |
| 84 | using (organization_id in (select private.current_organization_ids())); |
| 85 | create policy organization_member_roles_mutate_insert on public.organization_member_roles for insert to authenticated |
| 86 | with check (private.is_org_owner(organization_id) or private.has_org_capability(organization_id,'core','configure')); |
| 87 | create policy organization_member_roles_mutate_update on public.organization_member_roles for update to authenticated |
| 88 | using (private.is_org_owner(organization_id) or private.has_org_capability(organization_id,'core','configure')) |
| 89 | with check (private.is_org_owner(organization_id) or private.has_org_capability(organization_id,'core','configure')); |
| 90 | create policy organization_member_roles_mutate_delete on public.organization_member_roles for delete to authenticated |
| 91 | using (private.is_org_owner(organization_id) or private.has_org_capability(organization_id,'core','configure')); |
| 92 | |
| 93 | create or replace function private.protect_system_organization_role() |
| 94 | returns trigger language plpgsql set search_path='' |
| 95 | as $$ |
| 96 | begin |
| 97 | if tg_op='DELETE' and old.is_system then |
| 98 | raise exception 'system role cannot be deleted' using errcode='42501'; |
| 99 | end if; |
| 100 | if tg_op='UPDATE' and old.is_system and (new.role_key<>old.role_key or not new.is_system) then |
| 101 | raise exception 'system role identity is immutable' using errcode='42501'; |
| 102 | end if; |
| 103 | if tg_op='DELETE' then return old; else return new; end if; |
| 104 | end $$; |
| 105 | |
| 106 | revoke all on function private.protect_system_organization_role() from public,anon,authenticated; |
| 107 | drop trigger if exists trg_protect_system_organization_role on public.organization_roles; |
| 108 | create trigger trg_protect_system_organization_role |
| 109 | before update or delete on public.organization_roles |
| 110 | for each row execute function private.protect_system_organization_role(); |
| 111 | |
| 112 | create or replace function private.role_permission_allowed( |
| 113 | p_organization_id uuid, |
| 114 | p_module_key text |
| 115 | ) |
| 116 | returns boolean |
| 117 | language sql stable security definer set search_path='' |
| 118 | as $$ |
| 119 | select case |
| 120 | when p_module_key='core' then true |
| 121 | when p_module_key='full_suite' then private.organization_has_module_entitlement(p_organization_id,'full_suite',false) |
| 122 | else private.organization_has_module_entitlement(p_organization_id,p_module_key,false) |
| 123 | end |
| 124 | $$; |
| 125 | |
| 126 | revoke all on function private.role_permission_allowed(uuid,text) from public,anon; |
| 127 | grant execute on function private.role_permission_allowed(uuid,text) to authenticated; |
| 128 | |
| 129 | create or replace function public.organization_upsert_role( |
| 130 | p_organization_id uuid, |
| 131 | p_role_key text, |
| 132 | p_title text, |
| 133 | p_description text default null, |
| 134 | p_active boolean default true |
| 135 | ) |
| 136 | returns uuid |
| 137 | language plpgsql security invoker set search_path='' |
| 138 | as $$ |
| 139 | declare v_id uuid; |
| 140 | begin |
| 141 | if not (private.is_org_owner(p_organization_id) or private.has_org_capability(p_organization_id,'core','configure')) then |
| 142 | raise exception 'forbidden' using errcode='42501'; |
| 143 | end if; |
| 144 | if p_role_key is null or p_role_key !~ '^[a-z][a-z0-9_]{1,63}$' then raise exception 'invalid role key'; end if; |
| 145 | if nullif(trim(p_title),'') is null then raise exception 'role title required'; end if; |
| 146 | |
| 147 | insert into public.organization_roles(organization_id,role_key,title,description,active,created_by) |
| 148 | values(p_organization_id,p_role_key,trim(p_title),nullif(trim(p_description),''),coalesce(p_active,true),(select auth.uid())) |
| 149 | on conflict(organization_id,role_key) do update |
| 150 | set title=excluded.title, |
| 151 | description=excluded.description, |
| 152 | active=excluded.active, |
| 153 | updated_at=now() |
| 154 | returning id into v_id; |
| 155 | return v_id; |
| 156 | end $$; |
| 157 | |
| 158 | revoke all on function public.organization_upsert_role(uuid,text,text,text,boolean) from public,anon; |
| 159 | grant execute on function public.organization_upsert_role(uuid,text,text,text,boolean) to authenticated; |
| 160 | |
| 161 | create or replace function public.organization_set_role_permission( |
| 162 | p_organization_id uuid, |
| 163 | p_role_id uuid, |
| 164 | p_module_key text, |
| 165 | p_capabilities text[], |
| 166 | p_scope_type text default 'organization', |
| 167 | p_scope_id text default null, |
| 168 | p_confidentiality_level smallint default 0 |
| 169 | ) |
| 170 | returns uuid |
| 171 | language plpgsql security invoker set search_path='' |
| 172 | as $$ |
| 173 | declare v_id uuid; |
| 174 | begin |
| 175 | if not (private.is_org_owner(p_organization_id) or private.has_org_capability(p_organization_id,'core','configure')) then |
| 176 | raise exception 'forbidden' using errcode='42501'; |
| 177 | end if; |
| 178 | if not exists(select 1 from public.organization_roles r where r.id=p_role_id and r.organization_id=p_organization_id and r.active) then |
| 179 | raise exception 'role not found'; |
| 180 | end if; |
| 181 | if not private.role_permission_allowed(p_organization_id,p_module_key) then |
| 182 | raise exception 'module not licensed' using errcode='42501'; |
| 183 | end if; |
| 184 | |
| 185 | insert into public.organization_role_permissions( |
| 186 | organization_id,role_id,module_key,capabilities,scope_type,scope_id,confidentiality_level |
| 187 | ) |
| 188 | values( |
| 189 | p_organization_id,p_role_id,p_module_key,p_capabilities,p_scope_type,p_scope_id,p_confidentiality_level |
| 190 | ) |
| 191 | on conflict(organization_id,role_id,module_key,scope_type,scope_id) do update |
| 192 | set capabilities=excluded.capabilities, |
| 193 | confidentiality_level=excluded.confidentiality_level, |
| 194 | updated_at=now() |
| 195 | returning id into v_id; |
| 196 | return v_id; |
| 197 | end $$; |
| 198 | |
| 199 | revoke all on function public.organization_set_role_permission(uuid,uuid,text,text[],text,text,smallint) from public,anon; |
| 200 | grant execute on function public.organization_set_role_permission(uuid,uuid,text,text[],text,text,smallint) to authenticated; |
| 201 | |
| 202 | create or replace function public.organization_assign_member_role( |
| 203 | p_organization_id uuid, |
| 204 | p_member_id uuid, |
| 205 | p_role_id uuid, |
| 206 | p_active boolean default true |
| 207 | ) |
| 208 | returns uuid |
| 209 | language plpgsql security invoker set search_path='' |
| 210 | as $$ |
| 211 | declare v_id uuid; |
| 212 | begin |
| 213 | if not (private.is_org_owner(p_organization_id) or private.has_org_capability(p_organization_id,'core','configure')) then |
| 214 | raise exception 'forbidden' using errcode='42501'; |
| 215 | end if; |
| 216 | if not exists(select 1 from public.organization_members m where m.id=p_member_id and m.organization_id=p_organization_id and not m.is_owner) then |
| 217 | raise exception 'member not found or owner role is implicit'; |
| 218 | end if; |
| 219 | if not exists(select 1 from public.organization_roles r where r.id=p_role_id and r.organization_id=p_organization_id and r.active) then |
| 220 | raise exception 'role not found'; |
| 221 | end if; |
| 222 | |
| 223 | insert into public.organization_member_roles(organization_id,member_id,role_id,assigned_by,active) |
| 224 | values(p_organization_id,p_member_id,p_role_id,(select auth.uid()),coalesce(p_active,true)) |
| 225 | on conflict(organization_id,member_id,role_id) do update |
| 226 | set active=excluded.active,assigned_by=(select auth.uid()),updated_at=now() |
| 227 | returning id into v_id; |
| 228 | return v_id; |
| 229 | end $$; |
| 230 | |
| 231 | revoke all on function public.organization_assign_member_role(uuid,uuid,uuid,boolean) from public,anon; |
| 232 | grant execute on function public.organization_assign_member_role(uuid,uuid,uuid,boolean) to authenticated; |
| 233 | |
| 234 | -- Role grants augment direct member grants. Commercial entitlement remains a separate gate. |
| 235 | create or replace function private.has_org_capability(p_organization_id uuid,p_module_key text,p_capability text) |
| 236 | returns boolean |
| 237 | language sql stable security definer set search_path='' |
| 238 | as $$ |
| 239 | select exists( |
| 240 | select 1 |
| 241 | from public.organization_members m |
| 242 | where m.organization_id=p_organization_id |
| 243 | and m.user_id=(select auth.uid()) |
| 244 | and m.status='active' |
| 245 | and ( |
| 246 | (p_module_key='core' and p_capability='read') |
| 247 | or m.is_owner |
| 248 | or exists( |
| 249 | select 1 from public.member_module_permissions p |
| 250 | where p.organization_id=p_organization_id and p.member_id=m.id |
| 251 | and (p.module_key=p_module_key or p.module_key='full_suite') |
| 252 | and p_capability=any(p.capabilities) |
| 253 | ) |
| 254 | or exists( |
| 255 | select 1 |
| 256 | from public.organization_member_roles mr |
| 257 | join public.organization_roles r on r.id=mr.role_id and r.organization_id=mr.organization_id and r.active |
| 258 | join public.organization_role_permissions rp on rp.role_id=r.id and rp.organization_id=r.organization_id |
| 259 | where mr.organization_id=p_organization_id and mr.member_id=m.id and mr.active |
| 260 | and (rp.module_key=p_module_key or rp.module_key='full_suite') |
| 261 | and p_capability=any(rp.capabilities) |
| 262 | ) |
| 263 | ) |
| 264 | ) |
| 265 | $$; |
| 266 | |
| 267 | create or replace function private.record_in_member_scope(p_organization_id uuid,p_module_key text,p_owner_id uuid,p_data jsonb,p_capability text) |
| 268 | returns boolean |
| 269 | language sql stable security definer set search_path='' |
| 270 | as $$ |
| 271 | select exists( |
| 272 | select 1 from public.organization_members m |
| 273 | where m.organization_id=p_organization_id and m.user_id=(select auth.uid()) and m.status='active' |
| 274 | and ( |
| 275 | (p_module_key='core' and p_capability='read') |
| 276 | or m.is_owner |
| 277 | or exists( |
| 278 | select 1 from ( |
| 279 | select p.scope_type,p.scope_id,p.module_key,p.capabilities |
| 280 | from public.member_module_permissions p |
| 281 | where p.organization_id=p_organization_id and p.member_id=m.id |
| 282 | union all |
| 283 | select rp.scope_type,rp.scope_id,rp.module_key,rp.capabilities |
| 284 | from public.organization_member_roles mr |
| 285 | join public.organization_roles r on r.id=mr.role_id and r.organization_id=mr.organization_id and r.active |
| 286 | join public.organization_role_permissions rp on rp.role_id=r.id and rp.organization_id=r.organization_id |
| 287 | where mr.organization_id=p_organization_id and mr.member_id=m.id and mr.active |
| 288 | ) g |
| 289 | where (g.module_key=p_module_key or g.module_key='full_suite') |
| 290 | and p_capability=any(g.capabilities) |
| 291 | and ( |
| 292 | g.scope_type='organization' |
| 293 | or (g.scope_type='own' and p_owner_id=(select auth.uid())) |
| 294 | or (g.scope_type='company' and g.scope_id=coalesce(p_data->>'companyId',p_data->>'company_id')) |
| 295 | or (g.scope_type='branch' and g.scope_id=coalesce(p_data->>'branchId',p_data->>'branch_id')) |
| 296 | or (g.scope_type='unit' and g.scope_id=coalesce(p_data->>'unitId',p_data->>'unit_id')) |
| 297 | ) |
| 298 | ) |
| 299 | ) |
| 300 | ) |
| 301 | $$; |
| 302 | |
| 303 | create or replace function private.record_confidentiality_allowed(p_organization_id uuid,p_module_key text,p_data jsonb) |
| 304 | returns boolean |
| 305 | language sql stable security definer set search_path='' |
| 306 | as $$ |
| 307 | select case when p_module_key='core' then p_organization_id in (select private.current_organization_ids()) else exists( |
| 308 | select 1 from public.organization_members m |
| 309 | where m.organization_id=p_organization_id and m.user_id=(select auth.uid()) and m.status='active' |
| 310 | and ( |
| 311 | m.is_owner |
| 312 | or coalesce(( |
| 313 | select max(x.confidentiality_level) |
| 314 | from ( |
| 315 | select p.confidentiality_level,p.module_key |
| 316 | from public.member_module_permissions p |
| 317 | where p.organization_id=p_organization_id and p.member_id=m.id |
| 318 | union all |
| 319 | select rp.confidentiality_level,rp.module_key |
| 320 | from public.organization_member_roles mr |
| 321 | join public.organization_roles r on r.id=mr.role_id and r.organization_id=mr.organization_id and r.active |
| 322 | join public.organization_role_permissions rp on rp.role_id=r.id and rp.organization_id=r.organization_id |
| 323 | where mr.organization_id=p_organization_id and mr.member_id=m.id and mr.active |
| 324 | ) x |
| 325 | where x.module_key=p_module_key or x.module_key='full_suite' |
| 326 | ),-1) >= ( |
| 327 | case lower(coalesce(p_data->>'confidentiality','normal')) |
| 328 | when 'محرمانه' then 1 when 'confidential' then 1 |
| 329 | when 'خیلی محرمانه' then 2 when 'very_confidential' then 2 |
| 330 | when 'سری' then 3 when 'secret' then 3 else 0 end |
| 331 | ) |
| 332 | ) |
| 333 | ) end |
| 334 | $$; |
| 335 | |
| 336 | -- Seed reusable starter roles. They grant nothing unless assigned. |
| 337 | insert into public.organization_roles(organization_id,role_key,title,description,is_system,active) |
| 338 | select o.id,x.role_key,x.title,x.description,true,true |
| 339 | from public.organizations o |
| 340 | cross join (values |
| 341 | ('system_admin','مدیر سیستم سازمان','مدیریت ساختار و کاربران سازمان'), |
| 342 | ('finance_manager','مدیر مالی','مدیریت و تأیید عملیات مالی'), |
| 343 | ('accountant','حسابدار','ثبت و ویرایش عملیات حسابداری'), |
| 344 | ('petty_cash','تنخواهگردان','عملیات پایه خزانه و تنخواه'), |
| 345 | ('secretariat','کاربر دبیرخانه','ثبت، ارجاع و بایگانی مکاتبات'), |
| 346 | ('viewer','مشاهدهگر','نقش پایه مشاهده') |
| 347 | ) as x(role_key,title,description) |
| 348 | on conflict(organization_id,role_key) do nothing; |
| 349 | |
| 350 | insert into public.organization_role_permissions(organization_id,role_id,module_key,capabilities,scope_type,scope_id,confidentiality_level) |
| 351 | select r.organization_id,r.id,'core',array['read','configure']::text[],'organization',null,3 |
| 352 | from public.organization_roles r where r.role_key='system_admin' |
| 353 | on conflict(organization_id,role_id,module_key,scope_type,scope_id) do nothing; |
| 354 | |
| 355 | insert into public.organization_role_permissions(organization_id,role_id,module_key,capabilities,scope_type,scope_id,confidentiality_level) |
| 356 | select r.organization_id,r.id,'accounting',array['read','create','edit','approve']::text[],'organization',null,1 |
| 357 | from public.organization_roles r |
| 358 | join public.licenses l on l.organization_id=r.organization_id |
| 359 | where r.role_key='finance_manager' and ('full_suite'=any(l.modules) or 'accounting'=any(l.modules)) |
| 360 | on conflict(organization_id,role_id,module_key,scope_type,scope_id) do nothing; |
| 361 | |
| 362 | insert into public.organization_role_permissions(organization_id,role_id,module_key,capabilities,scope_type,scope_id,confidentiality_level) |
| 363 | select r.organization_id,r.id,'treasury',array['read','create','edit','approve']::text[],'organization',null,1 |
| 364 | from public.organization_roles r |
| 365 | join public.licenses l on l.organization_id=r.organization_id |
| 366 | where r.role_key='finance_manager' and ('full_suite'=any(l.modules) or 'treasury'=any(l.modules)) |
| 367 | on conflict(organization_id,role_id,module_key,scope_type,scope_id) do nothing; |
| 368 | |
| 369 | insert into public.organization_role_permissions(organization_id,role_id,module_key,capabilities,scope_type,scope_id,confidentiality_level) |
| 370 | select r.organization_id,r.id,'accounting',array['read','create','edit']::text[],'organization',null,0 |
| 371 | from public.organization_roles r |
| 372 | join public.licenses l on l.organization_id=r.organization_id |
| 373 | where r.role_key='accountant' and ('full_suite'=any(l.modules) or 'accounting'=any(l.modules)) |
| 374 | on conflict(organization_id,role_id,module_key,scope_type,scope_id) do nothing; |
| 375 | |
| 376 | insert into public.organization_role_permissions(organization_id,role_id,module_key,capabilities,scope_type,scope_id,confidentiality_level) |
| 377 | select r.organization_id,r.id,'treasury',array['read','create']::text[],'organization',null,0 |
| 378 | from public.organization_roles r |
| 379 | join public.licenses l on l.organization_id=r.organization_id |
| 380 | where r.role_key='petty_cash' and ('full_suite'=any(l.modules) or 'treasury'=any(l.modules)) |
| 381 | on conflict(organization_id,role_id,module_key,scope_type,scope_id) do nothing; |
| 382 | |
| 383 | insert into public.organization_role_permissions(organization_id,role_id,module_key,capabilities,scope_type,scope_id,confidentiality_level) |
| 384 | select r.organization_id,r.id,'office_automation',array['read','create','register','refer','archive']::text[],'organization',null,1 |
| 385 | from public.organization_roles r |
| 386 | join public.licenses l on l.organization_id=r.organization_id |
| 387 | where r.role_key='secretariat' and ('full_suite'=any(l.modules) or 'office_automation'=any(l.modules)) |
| 388 | on conflict(organization_id,role_id,module_key,scope_type,scope_id) do nothing; |
| 389 | |
| 390 | insert into public.organization_role_permissions(organization_id,role_id,module_key,capabilities,scope_type,scope_id,confidentiality_level) |
| 391 | select r.organization_id,r.id,'core',array['read']::text[],'organization',null,0 |
| 392 | from public.organization_roles r where r.role_key='viewer' |
| 393 | on conflict(organization_id,role_id,module_key,scope_type,scope_id) do nothing; |
| 394 | |
| 395 | create or replace function private.organization_role_audit_capture() |
| 396 | returns trigger |
| 397 | language plpgsql security definer set search_path='' |
| 398 | as $$ |
| 399 | declare v_org uuid; v_id text; |
| 400 | begin |
| 401 | v_org:=case when tg_op='DELETE' then old.organization_id else new.organization_id end; |
| 402 | v_id:=case when tg_op='DELETE' then old.id::text else new.id::text end; |
| 403 | insert into public.organization_audit(organization_id,actor_user_id,action,entity_type,entity_id,before_data,after_data) |
| 404 | values( |
| 405 | v_org,(select auth.uid()),lower(tg_table_name||'_'||tg_op),tg_table_name,v_id, |
| 406 | case when tg_op in ('UPDATE','DELETE') then to_jsonb(old) else null end, |
| 407 | case when tg_op in ('INSERT','UPDATE') then to_jsonb(new) else null end |
| 408 | ); |
| 409 | if tg_op='DELETE' then return old; else return new; end if; |
| 410 | end $$; |
| 411 | |
| 412 | revoke all on function private.organization_role_audit_capture() from public,anon,authenticated; |
| 413 | |
| 414 | drop trigger if exists organization_roles_audit on public.organization_roles; |
| 415 | create trigger organization_roles_audit after insert or update or delete on public.organization_roles |
| 416 | for each row execute function private.organization_role_audit_capture(); |
| 417 | |
| 418 | drop trigger if exists organization_role_permissions_audit on public.organization_role_permissions; |
| 419 | create trigger organization_role_permissions_audit after insert or update or delete on public.organization_role_permissions |
| 420 | for each row execute function private.organization_role_audit_capture(); |
| 421 | |
| 422 | drop trigger if exists organization_member_roles_audit on public.organization_member_roles; |
| 423 | create trigger organization_member_roles_audit after insert or update or delete on public.organization_member_roles |
| 424 | for each row execute function private.organization_role_audit_capture(); |