From 18d5a168e61bc4cf509ec93ae4bddab4d80eae9f Mon Sep 17 00:00:00 2001
From: Emmanuel Viennet <emmanuel.viennet@gmail.com>
Date: Mon, 20 Jan 2025 19:25:47 +0100
Subject: [PATCH] =?UTF-8?q?Nouvelle=20commande:=20user-edit=20--dept.=20Fi?=
 =?UTF-8?q?x=20modif=20dept.=20si=20affect=C3=A9=20=C3=A0=20un=20dept=20in?=
 =?UTF-8?q?existant.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 app/views/users.py | 6 +++++-
 scodoc.py          | 6 ++++++
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/app/views/users.py b/app/views/users.py
index 8612d0a8..9e226220 100644
--- a/app/views/users.py
+++ b/app/views/users.py
@@ -273,7 +273,11 @@ def create_user_form(user_name=None, edit=0, all_roles=True):
 
     administrable_dept_acronyms = _get_administrable_depts()
     if edit:
-        if the_user.dept is None:  # seul le super admin peut le toucher
+        if (the_user.dept is None) or the_user.dept not in (
+            d.acronym for d in Departement.query.all()
+        ):
+            # user dans tous dept, ou dans un dept inconnu:
+            # seul le super admin peut le toucher
             edit_only_roles = not current_user.is_administrator()
         else:
             edit_only_roles = the_user.dept not in administrable_dept_acronyms
diff --git a/scodoc.py b/scodoc.py
index e47b7115..d8f114ef 100755
--- a/scodoc.py
+++ b/scodoc.py
@@ -471,6 +471,7 @@ def user_change_login(user_name, new_user_name):
     is_flag=True,
     help="interdit login via ScoDoc",
 )
+@click.option("--dept", "dept", help="acronyme du départemùent de rattachement")
 @click.option(
     "-v",
     "--verbose",
@@ -487,6 +488,7 @@ def user_edit(
     disable_scodoc_login=None,
     activate=None,
     deactivate=None,
+    dept=None,
     verbose=False,
 ):
     """Add or remove a role to the given user in the given dept"""
@@ -508,6 +510,10 @@ def user_edit(
         user.active = True
     if deactivate:
         user.active = False
+    if dept:
+        if dept == "@all":
+            dept = None
+        user.dept = dept
     db.session.add(user)
     db.session.commit()
     if verbose:
-- 
GitLab