From 0fdae3bd709dedbc9693ad6d30fc92e9a9c6f3b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Gastinel?= <264859180+gastinelt@users.noreply.github.com> Date: Wed, 4 Nov 2026 20:08:08 +0100 Subject: [PATCH] =?UTF-8?q?forum=20:=20envoie=20les=20r=C3=B4les=20au=20Hu?= =?UTF-8?q?b=20(POST=20/oauth/roles)=20=C3=A0=20la=20connexion=20et=20?= =?UTF-8?q?=C3=A0=20chaque=20changement=20de=20r=C3=B4le,=20pour=20la=20pa?= =?UTF-8?q?ge=20du=20compte=20Gastinel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- forum/connexion.py | 16 ++++++++++++++++ forum/forum.py | 14 ++++++++++++++ forum/panneau.py | 1 + forum/server.py | 1 + 4 files changed, 32 insertions(+) diff --git a/forum/connexion.py b/forum/connexion.py index bda8729..2fa5940 100644 --- a/forum/connexion.py +++ b/forum/connexion.py @@ -191,3 +191,19 @@ def est_locale(ip): if getattr(a, "ipv4_mapped", None): a = a.ipv4_mapped return any(a.version == n.version and a in n for n in LOCALES) + + +def pousser_roles(r, hub_id, roles_forum): + """POST {hub}/oauth/roles : le Hub affiche ces rôles sur la page du compte. Sans secret client, rien n'est envoyé.""" + if not r.client_secret or not r.hub_url: + return + valides = ("adherent", "moderateur", "proprietaire_station", "proprietaire_adjoint", "proprietaire") + corps = json.dumps({"id": hub_id, "roles": sorted(set(x for x in roles_forum if x in valides))}).encode("utf-8") + jeton = base64.b64encode(("%s:%s" % (r.client_id, r.client_secret)).encode("utf-8")).decode("ascii") + req = urllib.request.Request(r.hub_url + "/oauth/roles", data=corps, method="POST", + headers={"Content-Type": "application/json", "Authorization": "Basic " + jeton}) + try: + with _OUVREUR.open(req, timeout=DELAI): + pass + except (urllib.error.URLError, OSError): + pass # le Hub rattrapera à la prochaine connexion ou au prochain changement de rôle diff --git a/forum/forum.py b/forum/forum.py index 0bfffdd..f1507a8 100644 --- a/forum/forum.py +++ b/forum/forum.py @@ -6,6 +6,7 @@ Le sujet fixe « Bulletin de la semaine du Vercors » est toujours épinglé en import hashlib import os import secrets +import threading import db import photos @@ -32,6 +33,16 @@ class Forum: self.stations = stations or {} # id -> nom affiché (pour le titre des sujets de station) self.dossier_photos = dossier_photos self.proprietaire_id = proprietaire_id or None # id Gastinel de Théo (FORUM_PROPRIETAIRE_ID) + self.apres_roles = None # fonction(hub_id, roles) : envoie les rôles au Hub (branchée par server.py) + + def _pousser_roles(self, hub_id): + if not self.apres_roles: + return + c = self.con.execute("SELECT id FROM users WHERE hub_id=?", (hub_id,)).fetchone() + if c is None: + return + rs = [r[0] for r in self.con.execute("SELECT role FROM user_roles WHERE user_id=?", (c[0],))] + threading.Thread(target=self.apres_roles, args=(hub_id, rs), daemon=True).start() # --- connexion Gastinel et sessions ---------------------------------- @staticmethod @@ -43,6 +54,7 @@ class Forum: u = self.utilisateur(hub_id, pseudo) if self.proprietaire_id and hub_id == self.proprietaire_id: u = self.fixer_proprietaire(hub_id, pseudo) + self._pousser_roles(hub_id) return u def ouvrir_session(self, user, jours=SESSION_JOURS, portee=""): @@ -167,6 +179,7 @@ class Forum: self.con.execute("INSERT OR IGNORE INTO user_roles(user_id, role, station, given_by) VALUES (?, ?, ?, ?)", (cible["id"], role, station, acteur["id"])) self.con.commit() + self._pousser_roles(hub_id) def retirer_role(self, acteur, hub_id, role, station=""): if not roles.peut_donner(self.roles_de(acteur), role): @@ -175,6 +188,7 @@ class Forum: self.con.execute("DELETE FROM user_roles WHERE user_id=? AND role=? AND station=?", (cible["id"], role, station if role == roles.STATION else "")) self.con.commit() + self._pousser_roles(hub_id) # --- lecture ------------------------------------------------------- def _visible(self, user, t): diff --git a/forum/panneau.py b/forum/panneau.py index 56749e0..aa4f978 100644 --- a/forum/panneau.py +++ b/forum/panneau.py @@ -362,6 +362,7 @@ def main(): reglages.redirect_uri = os.environ.get("FORUM_PANNEAU_REDIRECT_URI") or "http://localhost:%d/connexion/retour" % a.port f = Forum(a.db, stations=server.charger_stations() or {"geve": "Gêve"}, dossier_photos=a.photos, proprietaire_id=os.environ.get("FORUM_PROPRIETAIRE_ID")) + f.apres_roles = lambda h, rs: connexion.pousser_roles(reglages, h, rs) # rôles affichés sur la page du compte Gastinel s = creer_serveur(f, a.port, a.hote, reglages) print("Panneau : http://%s:%d (réseau local seulement ; connexion Gastinel %s, retour %s)" % ( a.hote, a.port, "configurée" if reglages.configuree else "NON configurée : FORUM_HUB_URL manquant", reglages.redirect_uri)) diff --git a/forum/server.py b/forum/server.py index d42f5f2..ee8014b 100644 --- a/forum/server.py +++ b/forum/server.py @@ -642,6 +642,7 @@ def main(): sys.exit("--seed refusé : comptes d'essai seulement avec FORUM_MODE_TEST=1") f = Forum(a.db, stations=charger_stations() or {"geve": "Gêve"}, dossier_photos=a.photos, proprietaire_id=os.environ.get("FORUM_PROPRIETAIRE_ID")) + f.apres_roles = lambda h, rs: connexion.pousser_roles(reglages, h, rs) if a.seed: semer(f) s = creer_serveur(f, a.port, a.hote, reglages=reglages)