[WORK] Покрытие по зонам (сетка, % площади), роли и токены записи (viewer/editor/admin), аудит изменений, импорт проекта из CSV

This commit is contained in:
root committed 2026-11-06 18:44:13 +03:00
1 parent 2b1b91c9f5
commit 8abb809e24
5 files changed
+169

No files matched your search

+39
View File
@@ -6,6 +6,37 @@ import sys
import urllib.parse
from http.server import BaseHTTPRequestHandler
PROTECTED = ("/save", "/delete", "/override", "/import")
AUDIT = "/opt/work-hub/compat/audit.jsonl"
AUTH_FILE = "/opt/work-hub/compat/access.yaml"
def _tokens():
"""Читает токены и роли из access.yaml."""
try:
import yaml
d = yaml.safe_load(open(AUTH_FILE, encoding="utf-8")) or {}
except Exception:
return {}
out = {}
for role, info in (d.get("roles") or {}).items():
tok = (info or {}).get("token")
if tok:
out[tok] = {"role": role, "write": bool(info.get("can_write"))}
return out
def _audit(path, role, body):
import time
try:
with open(AUDIT, "a", encoding="utf-8") as f:
f.write(json.dumps({"at": time.strftime("%Y-%m-%dT%H:%M:%S"),
"path": path, "role": role,
"body": str(body)[:400]}, ensure_ascii=False) + "\n")
except Exception:
pass
CORS = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
@@ -47,6 +78,14 @@ def serve(routes, port, name, before=None):
self._send({}, 204)
def _handle(self, body=None):
u = urllib.parse.urlparse(self.path)
if u.path in PROTECTED and self.command == "POST":
tok = self.headers.get("X-Token") or ""
info = _tokens().get(tok)
if not info or not info.get("write"):
return self._send({"error": "нужен токен editor/admin",
"hint": "заголовок X-Token"}, 403)
_audit(u.path, info.get("role"), body)
if before is not None:
try:
before()