forum : envoie les rôles au Hub (POST /oauth/roles) à la connexion et à chaque changement de rôle, pour la page du compte Gastinel
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Cette révision appartient à :
1 Parent
6551843f28
révision
0fdae3bd70
4 fichiers modifiés
+32
Aucun fichier ne correspond à votre recherche
@@ -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
|
||||
@@ -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):
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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)
|
||||
|
||||
Référencer dans un nouveau ticket
Bloquer un utilisateur