Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

dbbasic-object-server/object_permission_store.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 signup: the person chooses the password and name on the confirm page 83c7aa6 2 weeks, 5 days ago History
object_permission_store.py · 4.2 KB · 129 lines
1"""File-backed permission policy storage."""
2
3from __future__ import annotations
4
5import json
6from pathlib import Path
7from typing import Any
8
9import object_permissions
10from object_versions import DEFAULT_DATA_DIR
11
12PERMISSIONS_DIR = "permissions"
13POLICY_FILE = "policy.json"
14DEFAULT_POLICY = object_permissions.PermissionPolicy(access_mode="role_based")
15
16
17def policy_path(base_dir: Path | str = DEFAULT_DATA_DIR) -> Path:
18 """Return the permission policy file path."""
19 root = Path(base_dir) / PERMISSIONS_DIR
20 path = root / POLICY_FILE
21 resolved_root = root.resolve(strict=False)
22 resolved_path = path.resolve(strict=False)
23
24 try:
25 resolved_path.relative_to(resolved_root)
26 except ValueError as exc:
27 raise ValueError("Permission policy path escapes permissions directory") from exc
28
29 return path
30
31
32def load_policy(base_dir: Path | str = DEFAULT_DATA_DIR) -> object_permissions.PermissionPolicy:
33 """Load the persisted policy, or return the conservative default."""
34 path = policy_path(base_dir)
35 if not path.exists():
36 return DEFAULT_POLICY
37
38 try:
39 payload = json.loads(path.read_text())
40 except json.JSONDecodeError as exc:
41 raise ValueError("Permission policy file contains invalid JSON") from exc
42
43 return object_permissions.policy_from_dict(payload)
44
45
46def save_policy(
47 policy: object_permissions.PermissionPolicy,
48 base_dir: Path | str = DEFAULT_DATA_DIR,
49) -> Path:
50 """Atomically save the permission policy."""
51 path = policy_path(base_dir)
52 path.parent.mkdir(parents=True, exist_ok=True)
53 temp_path = path.with_name(f".{path.name}.tmp")
54 payload = object_permissions.policy_to_dict(policy)
55
56 temp_path.write_text(_json_dump(payload))
57 temp_path.replace(path)
58 return path
59
60
61def replace_policy(
62 payload: dict[str, Any],
63 base_dir: Path | str = DEFAULT_DATA_DIR,
64) -> object_permissions.PermissionPolicy:
65 """Validate and persist a JSON-compatible policy payload."""
66 policy = object_permissions.policy_from_dict(payload)
67 save_policy(policy, base_dir=base_dir)
68 return policy
69
70
71def starter_policy_payload() -> dict[str, Any]:
72 """Return the documented starter policy for a fresh deployment.
73
74 Role-based, with the smallest grants that keep a public staging site
75 working once enforcement is on: anonymous visitors can run the public
76 pages and read the probe demo records; signed-in users can run objects
77 and write probe records. Admin-role subjects bypass rules entirely.
78 Deployments should edit this per app rather than widen it in place.
79 """
80 return {
81 "access_mode": "role_based",
82 "rules": [
83 {
84 "effect": "allow",
85 "principal": "public",
86 "actions": ["execute"],
87 "object_id": "site_home",
88 "reason": "public home page",
89 },
90 {
91 "effect": "allow",
92 "principal": "public",
93 "actions": ["execute"],
94 "object_id": "system_dashboard",
95 "reason": "public staging dashboard",
96 },
97 {
98 "effect": "allow",
99 "principal": "public",
100 "actions": ["execute"],
101 "object_id": "system_write_probe",
102 "reason": "public write probe page",
103 },
104 {
105 "effect": "allow",
106 "principal": "public",
107 "actions": ["read"],
108 "collection": "dbbasic_probe",
109 "reason": "probe records are public demo data",
110 },
111 {
112 "effect": "allow",
113 "principal": "registered",
114 "actions": ["read", "execute"],
115 "reason": "signed-in users can read and run objects",
116 },
117 {
118 "effect": "allow",
119 "principal": "registered",
120 "actions": ["create", "update", "delete"],
121 "collection": "dbbasic_probe",
122 "reason": "signed-in users can write probe records",
123 },
124 ],
125 }
126
127
128def _json_dump(payload: dict[str, Any]) -> str:
129 return json.dumps(payload, indent=2, sort_keys=True) + "\n"