askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
loss_events.py · 3.6 KB · 80 lines
| 1 | """hook_loss_events -- who reported a loss, who approved it, and that they differ. |
| 2 | |
| 3 | Two layers, because the two questions are different: |
| 4 | |
| 5 | BEFORE_WRITE (the record API, MCP): the only place that knows who is |
| 6 | writing. It stamps `reported_by` from the signed-in user when a loss is |
| 7 | recorded, and `approved_by` / `approved_at` when it is approved, so neither |
| 8 | is ever typed; it refuses an edit to either stamp; and it refuses the |
| 9 | reporter's own approval. Same shape as hook_expenses: approving your own |
| 10 | loss is not an approval -- the person who reports a theft is the person a |
| 11 | second pair of eyes exists for. |
| 12 | |
| 13 | LOCKED_CHECK (loss_events.approval, every writer, inside the write lock): |
| 14 | an approved row names an approver and a reporter and they are not the same |
| 15 | person. The hook cannot hold this for a storage-level writer (an import, a |
| 16 | script, action_apply_count's own loss events) and a check that only the |
| 17 | HTTP path ran would be a rule with a back door. The check never asks who |
| 18 | is writing -- it states a fact about the row. |
| 19 | """ |
| 20 | |
| 21 | from datetime import datetime, timezone |
| 22 | |
| 23 | |
| 24 | def _text(value): |
| 25 | return str(value if value is not None else "").strip() |
| 26 | |
| 27 | |
| 28 | def BEFORE_WRITE(request): |
| 29 | action = request.get("action") |
| 30 | if action not in ("create", "update"): |
| 31 | return None # deleting an approved loss is the schema's locked_when (refused) |
| 32 | record = dict(request.get("record") or {}) |
| 33 | existing = request.get("existing") or {} |
| 34 | actor = _text((request.get("subject") or {}).get("user_id")) |
| 35 | |
| 36 | if action == "create": |
| 37 | if not actor: |
| 38 | return {"error": "A loss is reported by a signed-in person.", "status": 403} |
| 39 | if _text(record.get("status")) not in ("", "reported"): |
| 40 | return {"error": ("A loss is reported first, then approved by somebody other " |
| 41 | "than the person who reported it."), "status": 400} |
| 42 | record["reported_by"] = actor |
| 43 | record["approved_by"] = "" |
| 44 | record["approved_at"] = "" |
| 45 | return {"record": record} |
| 46 | |
| 47 | for stamp in ("reported_by", "approved_by", "approved_at"): |
| 48 | if _text(record.get(stamp)) != _text(existing.get(stamp)) and not ( |
| 49 | stamp != "reported_by" and _text(record.get("status")) == "approved" |
| 50 | and _text(existing.get("status")) != "approved"): |
| 51 | return {"error": f"{stamp} is stamped by the server, never typed.", "status": 400} |
| 52 | |
| 53 | if _text(record.get("status")) == "approved" and _text(existing.get("status")) != "approved": |
| 54 | reporter = _text(existing.get("reported_by")) |
| 55 | if not actor: |
| 56 | return {"error": "A loss is approved by a signed-in person.", "status": 403} |
| 57 | if actor == reporter: |
| 58 | return {"error": ("Losses are approved by somebody other than the person who " |
| 59 | "reported them. Approving your own report is not an approval."), |
| 60 | "status": 403} |
| 61 | record["approved_by"] = actor |
| 62 | record["approved_at"] = datetime.now(timezone.utc).isoformat() |
| 63 | return {"record": record} |
| 64 | return None |
| 65 | |
| 66 | |
| 67 | def LOCKED_CHECK(request): |
| 68 | if request.get("action") not in ("create", "update"): |
| 69 | return None |
| 70 | record = request.get("record") or {} |
| 71 | if _text(record.get("status")) != "approved": |
| 72 | return None |
| 73 | reporter, approver = _text(record.get("reported_by")), _text(record.get("approved_by")) |
| 74 | if not reporter or not approver: |
| 75 | return {"error": "An approved loss names who reported it and who approved it.", |
| 76 | "status": 409} |
| 77 | if reporter == approver: |
| 78 | return {"error": "A loss is approved by somebody other than the person who reported it.", |
| 79 | "status": 409} |
| 80 | return None |