permissions.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from dataclasses import dataclass
  2. from fastapi import HTTPException, status
  3. from sqlalchemy import text
  4. from sqlalchemy.orm import Session
  5. @dataclass(frozen=True)
  6. class Entitlement:
  7. hauptbetrieb_id: str
  8. standort_id: str
  9. abteilung: str | None
  10. role: str
  11. priority: int
  12. @dataclass(frozen=True)
  13. class Principal:
  14. username: str
  15. name: str
  16. email: str
  17. role: str
  18. auth_source: str
  19. csrf: str
  20. entitlements: tuple[Entitlement, ...]
  21. RIGHTS_SQL = text(
  22. """
  23. SELECT BR.Hauptbetrieb_ID,
  24. BR.Standort_ID,
  25. BR.Abteilung,
  26. BR.Rolle,
  27. R.Prioritaet
  28. FROM dbo.Benutzer_Rechte AS BR
  29. INNER JOIN dbo.Rollen AS R ON R.Rolle = BR.Rolle AND R.Aktiv = 1
  30. WHERE LOWER(LTRIM(RTRIM(BR.Benutzer_ID))) = :username
  31. AND NULLIF(LTRIM(RTRIM(BR.Benutzer_ID)), '') IS NOT NULL
  32. ORDER BY R.Prioritaet DESC, BR.Rolle ASC
  33. """
  34. )
  35. def load_entitlements(db: Session, username: str) -> tuple[Entitlement, ...]:
  36. rows = db.execute(RIGHTS_SQL, {"username": username}).mappings().all()
  37. return tuple(
  38. Entitlement(
  39. hauptbetrieb_id=str(row["Hauptbetrieb_ID"]),
  40. standort_id=str(row["Standort_ID"]),
  41. abteilung=row["Abteilung"],
  42. role=row["Rolle"],
  43. priority=int(row["Prioritaet"]),
  44. )
  45. for row in rows
  46. )
  47. def highest_role(entitlements: tuple[Entitlement, ...]) -> str:
  48. if not entitlements:
  49. raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Keine Berechtigung für diese Anwendung")
  50. return sorted(entitlements, key=lambda item: (-item.priority, item.role))[0].role
  51. FORDERUNG_ACCESS_SQL = text(
  52. """
  53. SELECT TOP 1 1
  54. FROM dbo.Forderungen_Kopf AS F
  55. WHERE F.Client_DB = :client_db
  56. AND F.Beleg_Nr = :beleg_nr
  57. AND EXISTS (
  58. SELECT 1
  59. FROM dbo.Benutzer_Rechte AS BR
  60. INNER JOIN dbo.Rollen AS R ON R.Rolle = BR.Rolle AND R.Aktiv = 1
  61. WHERE LOWER(LTRIM(RTRIM(BR.Benutzer_ID))) = :auth_username
  62. AND NULLIF(LTRIM(RTRIM(BR.Benutzer_ID)), '') IS NOT NULL
  63. AND BR.Hauptbetrieb_ID = F.Hauptbetrieb_ID
  64. AND BR.Standort_ID = F.Standort_ID
  65. AND (BR.Abteilung IS NULL OR BR.Abteilung = F.Abteilung)
  66. )
  67. """
  68. )
  69. def ensure_forderung_access(db: Session, principal: Principal, client_db: str, beleg_nr: str) -> None:
  70. allowed = db.execute(
  71. FORDERUNG_ACCESS_SQL,
  72. {"client_db": client_db, "beleg_nr": beleg_nr, "auth_username": principal.username},
  73. ).first()
  74. if not allowed:
  75. raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Forderung nicht gefunden")
  76. def current_principal(request) -> Principal:
  77. principal = getattr(request.state, "principal", None)
  78. if principal is None:
  79. raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Nicht angemeldet")
  80. return principal