Date-range scoping for viewer tokens
This commit is contained in:
1 parent
c820d6f6db
commit
4ef2dfb352
9 files changed
+69
-6
No files matched your search
+8
-2
@@ -179,8 +179,10 @@ def db_flagged_items():
|
||||
"""
|
||||
if not DB_OK: return jsonify([])
|
||||
from flask import session as _session
|
||||
scope = _session.get("viewer_scope", {})
|
||||
role_filt = scope.get("role", "") if isinstance(scope, dict) else ""
|
||||
scope = _session.get("viewer_scope", {})
|
||||
role_filt = scope.get("role", "") if isinstance(scope, dict) else ""
|
||||
date_from = scope.get("valid_from", "") if isinstance(scope, dict) else ""
|
||||
date_to = scope.get("valid_to", "") if isinstance(scope, dict) else ""
|
||||
# user may be a list of emails (current) or a legacy single string
|
||||
raw_user = scope.get("user", "") if isinstance(scope, dict) else ""
|
||||
if isinstance(raw_user, list):
|
||||
@@ -197,6 +199,10 @@ def db_flagged_items():
|
||||
continue
|
||||
if user_filt and (row.get("account_id", "") or "").lower() not in user_filt:
|
||||
continue
|
||||
if date_from and (row.get("modified") or "") < date_from:
|
||||
continue
|
||||
if date_to and (row.get("modified") or "") > date_to:
|
||||
continue
|
||||
row["special_category"] = _json.loads(row.get("special_category") or "[]") if isinstance(row.get("special_category"), str) else row.get("special_category", [])
|
||||
row["exif"] = _json.loads(row.get("exif_json") or "{}") if isinstance(row.get("exif_json"), str) else row.get("exif", {})
|
||||
row.pop("exif_json", None)
|
||||
|
||||
@@ -97,12 +97,27 @@ def create_token():
|
||||
return jsonify({"error": "scope.role must be '', 'student', or 'staff'"}), 400
|
||||
if user_emails and not all("@" in e for e in user_emails):
|
||||
return jsonify({"error": "scope.user entries must be valid email addresses"}), 400
|
||||
valid_from = str(raw_scope.get("valid_from", "")).strip()
|
||||
valid_to = str(raw_scope.get("valid_to", "")).strip()
|
||||
from datetime import datetime as _dt
|
||||
for _d, _lbl in ((valid_from, "valid_from"), (valid_to, "valid_to")):
|
||||
if _d:
|
||||
try:
|
||||
_dt.strptime(_d, "%Y-%m-%d")
|
||||
except ValueError:
|
||||
return jsonify({"error": f"scope.{_lbl} must be YYYY-MM-DD"}), 400
|
||||
if valid_from and valid_to and valid_from > valid_to:
|
||||
return jsonify({"error": "scope.valid_from must be ≤ scope.valid_to"}), 400
|
||||
if user_emails:
|
||||
scope = {"user": user_emails, "display_name": display_name or user_emails[0]}
|
||||
elif role:
|
||||
scope = {"role": role}
|
||||
else:
|
||||
scope = {}
|
||||
if valid_from:
|
||||
scope["valid_from"] = valid_from
|
||||
if valid_to:
|
||||
scope["valid_to"] = valid_to
|
||||
entry = create_viewer_token(label=label, expires_days=expires_days, scope=scope)
|
||||
return jsonify(entry), 201
|
||||
|
||||
|
||||
Reference in new issue
Block a user