Mirrored from https://github.com/davoodmehraban89/Aram.git
Synced Dec 4, 03:00 UTC
arena/094f314c-aram
add-index-html-10623938925177053894
arena/094f314c-aram
chatgpt-phase1b2-autopilot
codex/adr-future-connectors-pi-v1
codex/aram-account-note-source-trace-20261031
codex/aram-actions-node24-maintenance-20261031
codex/aram-analyst-review-workbook-20261031
codex/aram-bs-candidate-statement-20261031
codex/aram-cash-flow-direct-candidate-20261031
codex/aram-clean-reference-year-integrity-20261030
codex/aram-cross-statement-controls-20261031
codex/aram-equity-changes-candidate-20261031
codex/aram-five-statement-cli-20261031
codex/aram-five-statement-integration-20261031
codex/aram-five-statement-orchestrator-20261031
codex/aram-gate3-coverage-target-guard-20261030
codex/aram-gate3-hidden-xlsx-guard-20261030
codex/aram-gate3-integration-validation-20261030
codex/aram-gate3-mapping-change-preview-20261030
codex/aram-gate3-stale-note-guard-20261030
codex/aram-income-statement-candidate-20261031
codex/aram-manifest-referential-integrity-20261031
codex/aram-mapping-source-parser-20261101
codex/aram-multidimensional-mapping-20261101
codex/aram-multistatement-mapping-20261031
codex/aram-pipeline-manifest-schema-20261031
codex/aram-readonly-mapping-coverage-20261031
codex/aram-reconciliation-review-gate-20261031
codex/aram-same-flush-fiscal-lock-20261030
codex/aram-synthetic-windows-demo-20261031
codex/aram-trial-balance-diagnostic-20261031
codex/aram-windows-background-detail-search-20261031
codex/aram-windows-detail-review-20261031
codex/aram-windows-five-statement-pilot-20261031
codex/aram-windows-statement-figures-20261031
codex/gate-2a-pilot-workbook
codex/gate-2a-pilot-workbook-6864754244772568979
codex/gate3-authenticated-principal-boundary-20261030
codex/gate3-authored-draft-staging-20261030
codex/gate3-carry-forward-preview-20261023
codex/gate3-draft-staging-audit-20261030
codex/gate3-mapping-batch-freshness-20261030
codex/gate3-mapping-batch-review-20261030
codex/gate3-mapping-core
codex/gate3-mapping-coverage-20261023
codex/gate3-mapping-excel-20261023
codex/gate3-note-mapping-review-20261023
codex/gate3-signed-year-scope-20261030
codex/gate3-unapproved-proposal-storage-20261030
codex/payvast-mapping-discovery-20261023
codex/phase-2a-backend-foundation
codex/phase-2b-post-merge-record
codex/phase-2b-source-registration
codex/phase-2b-source-registration-12955190277775369594
codex/post-gate2a-control-reconciliation
codex/post-pr18-acceptance-record
codex/project-control-foundation
codex/t1-repo-hygiene-20261023
codex/t2-ci-hardening-20261023
codex/t3-explicit-migrations-20261023
codex/t6a-readonly-diagnostics-20261023
codex/v1-01-safety-gate-hardening
codex/v1-year-isolation-followup-20261023
codex/windows-native-pilot-shell-20261030
docs/aram-master-control-plan-20261111
docs/aram-roadmap-dashboard-20261030
docs/financial-statement-reconciliation-1402-1404-20261110
feat/gate3-governed-mapping-20261101
feat/gate3-package9-1-security-20261103
feat/gate3-package9-isolated-20261103
fix/password-unicode-validation-20261111
governance/aram-5-1-3-controls-20261030
governance/role-based-operating-model
jules-1168663913887856558-73b8a885
jules-gate-2b-mapping-workflow-foundation-4137724387867065330
phase-1b1-audit-report-7365389436990903239
phase-2a-backend-foundation-10237597358138521261
phase1b2-postmerge-metadata-fix
refactor/drg-parent-year-id-15088443704684163304
source-data-cleanup
| 1 | """HTTP API for Phase 7 administration: users, audit, reopen/lock, backup. |
| 2 | |
| 3 | Sensitive actions are gated by local role (``require_role``) and attributed to a |
| 4 | username in the hash-chained audit log. Offline desktop: no runtime network. |
| 5 | """ |
| 6 | |
| 7 | from __future__ import annotations |
| 8 | |
| 9 | from fastapi import APIRouter, Depends, HTTPException |
| 10 | from pydantic import BaseModel, Field |
| 11 | from sqlalchemy import select |
| 12 | from sqlalchemy.orm import Session |
| 13 | |
| 14 | from aram_backend.config import get_settings |
| 15 | from aram_backend.database import get_session |
| 16 | from aram_backend.models import AuditEntry, UserRole |
| 17 | from aram_backend.services.audit import verify_chain |
| 18 | from aram_backend.services.backup import BackupError, create_backup, verify_backup |
| 19 | from aram_backend.services.reopen import ( |
| 20 | ReopenError, |
| 21 | approve_reopen, |
| 22 | list_reopen_requests, |
| 23 | lock_year, |
| 24 | reopen_year, |
| 25 | request_reopen, |
| 26 | ) |
| 27 | from aram_backend.services.users import ( |
| 28 | AuthorizationError, |
| 29 | UserError, |
| 30 | authenticate, |
| 31 | create_user, |
| 32 | list_users, |
| 33 | require_role, |
| 34 | set_active, |
| 35 | set_role, |
| 36 | ) |
| 37 | |
| 38 | router = APIRouter(prefix="/api/v1", tags=["admin"]) |
| 39 | |
| 40 | _ADMIN = {UserRole.ADMIN} |
| 41 | _REVIEW = {UserRole.ADMIN, UserRole.REVIEWER} |
| 42 | |
| 43 | |
| 44 | def _guard(session: Session, actor: str, allowed: set[UserRole]) -> None: |
| 45 | try: |
| 46 | require_role(session, username=actor, allowed=allowed) |
| 47 | except AuthorizationError as exc: |
| 48 | raise HTTPException(status_code=403, detail=str(exc)) from exc |
| 49 | |
| 50 | |
| 51 | # --------------------------------------------------------------------- users |
| 52 | class UserIn(BaseModel): |
| 53 | username: str = Field(min_length=1, max_length=64) |
| 54 | password: str = Field(min_length=8, max_length=256) |
| 55 | role: UserRole = UserRole.PREPARER |
| 56 | display_name: str = Field(default="", max_length=128) |
| 57 | actor: str = Field(default="", max_length=64) |
| 58 | |
| 59 | |
| 60 | @router.post("/users") |
| 61 | def create_user_endpoint(body: UserIn, session: Session = Depends(get_session)) -> dict: |
| 62 | # Bootstrap: the very first user is created without an existing admin and is |
| 63 | # forced to the admin role (offline desktop install). Afterwards, only an |
| 64 | # admin may create users. |
| 65 | bootstrap = not list_users(session) |
| 66 | if not bootstrap: |
| 67 | _guard(session, body.actor, _ADMIN) |
| 68 | role = UserRole.ADMIN if bootstrap else body.role |
| 69 | try: |
| 70 | user = create_user( |
| 71 | session, |
| 72 | username=body.username, |
| 73 | password=body.password, |
| 74 | role=role, |
| 75 | display_name=body.display_name, |
| 76 | actor=body.actor, |
| 77 | ) |
| 78 | except UserError as exc: |
| 79 | raise HTTPException(status_code=422, detail=str(exc)) from exc |
| 80 | session.commit() |
| 81 | return {"username": user.username, "role": user.role.value} |
| 82 | |
| 83 | |
| 84 | @router.get("/users") |
| 85 | def list_users_endpoint(actor: str = "", session: Session = Depends(get_session)) -> dict: |
| 86 | _guard(session, actor, _ADMIN) |
| 87 | return { |
| 88 | "users": [ |
| 89 | { |
| 90 | "username": u.username, |
| 91 | "display_name": u.display_name, |
| 92 | "role": u.role.value, |
| 93 | "active": u.active, |
| 94 | } |
| 95 | for u in list_users(session) |
| 96 | ] |
| 97 | } |
| 98 | |
| 99 | |
| 100 | class LoginIn(BaseModel): |
| 101 | username: str = Field(min_length=1, max_length=64) |
| 102 | password: str = Field(min_length=1, max_length=256) |
| 103 | |
| 104 | |
| 105 | @router.post("/auth/login") |
| 106 | def login(body: LoginIn, session: Session = Depends(get_session)) -> dict: |
| 107 | try: |
| 108 | user = authenticate(session, username=body.username, password=body.password) |
| 109 | except UserError as exc: |
| 110 | raise HTTPException(status_code=401, detail=str(exc)) from exc |
| 111 | return {"username": user.username, "display_name": user.display_name, "role": user.role.value} |
| 112 | |
| 113 | |
| 114 | class RoleIn(BaseModel): |
| 115 | role: UserRole |
| 116 | actor: str = Field(default="", max_length=64) |
| 117 | |
| 118 | |
| 119 | @router.post("/users/{username}/role") |
| 120 | def set_role_endpoint(username: str, body: RoleIn, session: Session = Depends(get_session)) -> dict: |
| 121 | _guard(session, body.actor, _ADMIN) |
| 122 | try: |
| 123 | user = set_role(session, username=username, role=body.role, actor=body.actor) |
| 124 | except UserError as exc: |
| 125 | raise HTTPException(status_code=422, detail=str(exc)) from exc |
| 126 | session.commit() |
| 127 | return {"username": user.username, "role": user.role.value} |
| 128 | |
| 129 | |
| 130 | class ActiveIn(BaseModel): |
| 131 | active: bool |
| 132 | actor: str = Field(default="", max_length=64) |
| 133 | |
| 134 | |
| 135 | @router.post("/users/{username}/active") |
| 136 | def set_active_endpoint( |
| 137 | username: str, body: ActiveIn, session: Session = Depends(get_session) |
| 138 | ) -> dict: |
| 139 | _guard(session, body.actor, _ADMIN) |
| 140 | try: |
| 141 | user = set_active(session, username=username, active=body.active, actor=body.actor) |
| 142 | except UserError as exc: |
| 143 | raise HTTPException(status_code=422, detail=str(exc)) from exc |
| 144 | session.commit() |
| 145 | return {"username": user.username, "active": user.active} |
| 146 | |
| 147 | |
| 148 | # --------------------------------------------------------------------- audit |
| 149 | @router.get("/audit") |
| 150 | def list_audit(year_id: int | None = None, session: Session = Depends(get_session)) -> dict: |
| 151 | stmt = select(AuditEntry).order_by(AuditEntry.id.asc()) |
| 152 | if year_id is not None: |
| 153 | stmt = stmt.where(AuditEntry.year_id == year_id) |
| 154 | entries = session.scalars(stmt).all() |
| 155 | return { |
| 156 | "chain_valid": verify_chain(session), |
| 157 | "entries": [ |
| 158 | { |
| 159 | "id": e.id, |
| 160 | "year_id": e.year_id, |
| 161 | "actor": e.actor, |
| 162 | "action": e.action, |
| 163 | "chain_hash": e.chain_hash, |
| 164 | "created_at": e.created_at.isoformat(), |
| 165 | } |
| 166 | for e in entries |
| 167 | ], |
| 168 | } |
| 169 | |
| 170 | |
| 171 | @router.get("/audit/verify") |
| 172 | def verify_audit(session: Session = Depends(get_session)) -> dict: |
| 173 | return {"chain_valid": verify_chain(session)} |
| 174 | |
| 175 | |
| 176 | # --------------------------------------------------------------- reopen/lock |
| 177 | class ReopenRequestIn(BaseModel): |
| 178 | reason: str = Field(min_length=1, max_length=2000) |
| 179 | actor: str = Field(default="", max_length=64) |
| 180 | |
| 181 | |
| 182 | @router.post("/years/{year_id}/reopen/request") |
| 183 | def reopen_request( |
| 184 | year_id: int, body: ReopenRequestIn, session: Session = Depends(get_session) |
| 185 | ) -> dict: |
| 186 | _guard(session, body.actor, _REVIEW) |
| 187 | try: |
| 188 | request = request_reopen(session, year_id, reason=body.reason, requested_by=body.actor) |
| 189 | except ReopenError as exc: |
| 190 | raise HTTPException(status_code=422, detail=str(exc)) from exc |
| 191 | session.commit() |
| 192 | return {"request_id": request.id, "status": request.status} |
| 193 | |
| 194 | |
| 195 | class ActorIn(BaseModel): |
| 196 | actor: str = Field(default="", max_length=64) |
| 197 | |
| 198 | |
| 199 | @router.post("/reopen/{request_id}/approve") |
| 200 | def reopen_approve(request_id: int, body: ActorIn, session: Session = Depends(get_session)) -> dict: |
| 201 | _guard(session, body.actor, _REVIEW) |
| 202 | try: |
| 203 | request = approve_reopen(session, request_id, approved_by=body.actor) |
| 204 | except ReopenError as exc: |
| 205 | raise HTTPException(status_code=422, detail=str(exc)) from exc |
| 206 | session.commit() |
| 207 | return {"request_id": request.id, "status": request.status} |
| 208 | |
| 209 | |
| 210 | @router.post("/years/{year_id}/reopen") |
| 211 | def reopen(year_id: int, body: ActorIn, session: Session = Depends(get_session)) -> dict: |
| 212 | _guard(session, body.actor, _REVIEW) |
| 213 | try: |
| 214 | year = reopen_year(session, year_id, actor=body.actor) |
| 215 | except ReopenError as exc: |
| 216 | raise HTTPException(status_code=422, detail=str(exc)) from exc |
| 217 | session.commit() |
| 218 | return {"year_id": year.id, "status": year.status.value} |
| 219 | |
| 220 | |
| 221 | @router.post("/years/{year_id}/lock") |
| 222 | def lock(year_id: int, body: ActorIn, session: Session = Depends(get_session)) -> dict: |
| 223 | _guard(session, body.actor, _REVIEW) |
| 224 | try: |
| 225 | year = lock_year(session, year_id, actor=body.actor) |
| 226 | except ReopenError as exc: |
| 227 | raise HTTPException(status_code=422, detail=str(exc)) from exc |
| 228 | session.commit() |
| 229 | return {"year_id": year.id, "status": year.status.value} |
| 230 | |
| 231 | |
| 232 | @router.get("/years/{year_id}/reopen/requests") |
| 233 | def reopen_requests(year_id: int, session: Session = Depends(get_session)) -> dict: |
| 234 | return { |
| 235 | "requests": [ |
| 236 | { |
| 237 | "id": r.id, |
| 238 | "reason": r.reason, |
| 239 | "requested_by": r.requested_by, |
| 240 | "approved_by": r.approved_by, |
| 241 | "status": r.status, |
| 242 | } |
| 243 | for r in list_reopen_requests(session, year_id) |
| 244 | ] |
| 245 | } |
| 246 | |
| 247 | |
| 248 | # -------------------------------------------------------------------- backup |
| 249 | class BackupIn(BaseModel): |
| 250 | dest: str = Field(min_length=1, max_length=512) |
| 251 | actor: str = Field(default="", max_length=64) |
| 252 | |
| 253 | |
| 254 | @router.post("/backup") |
| 255 | def backup(body: BackupIn, session: Session = Depends(get_session)) -> dict: |
| 256 | _guard(session, body.actor, _ADMIN) |
| 257 | try: |
| 258 | manifest = create_backup(session, body.dest, store_root=get_settings().source_store_root) |
| 259 | except BackupError as exc: |
| 260 | raise HTTPException(status_code=422, detail=str(exc)) from exc |
| 261 | return {"dest": body.dest, "manifest": manifest} |
| 262 | |
| 263 | |
| 264 | class VerifyBackupIn(BaseModel): |
| 265 | dest: str = Field(min_length=1, max_length=512) |
| 266 | actor: str = Field(default="", max_length=64) |
| 267 | |
| 268 | |
| 269 | @router.post("/backup/verify") |
| 270 | def backup_verify(body: VerifyBackupIn, session: Session = Depends(get_session)) -> dict: |
| 271 | _guard(session, body.actor, _ADMIN) |
| 272 | try: |
| 273 | return verify_backup(body.dest) |
| 274 | except BackupError as exc: |
| 275 | raise HTTPException(status_code=422, detail=str(exc)) from exc |