From 66ee610bbddde44a965793f7e66f7c3b98b38996 Mon Sep 17 00:00:00 2001 From: denis Date: Fri, 16 Oct 2026 22:45:57 +0200 Subject: [PATCH] create_author: --admin option, --reset keeps existing rights Co-Authored-By: Claude Sonnet 5.5 --- Makefile | 4 ++-- .../authors/management/commands/create_author.py | 10 +++++++--- backend/authors/tests.py | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 25ba3cc..f4f879c 100644 --- a/Makefile +++ b/Makefile @@ -60,8 +60,8 @@ dev-nginx: $(VENV)/.installed build ## Pile locale réelle : nginx + Django (éd dev-admin: $(VENV)/.installed ## Crée le compte administrateur de l'essai local (gère les auteurs sur /api/admin/) cd backend && DJANGO_SETTINGS_MODULE=pyquest_api.settings.dev PYQUEST_DB_PATH=$(CURDIR)/.local/stack/db.sqlite3 ../$(VENV_PY) manage.py createsuperuser -dev-author: $(VENV)/.installed ## Crée directement un compte auteur local (NAME=x, RESET=--reset) ; demande le mot de passe - cd backend && DJANGO_SETTINGS_MODULE=pyquest_api.settings.dev PYQUEST_DB_PATH=$(CURDIR)/.local/stack/db.sqlite3 ../$(VENV_PY) manage.py create_author $(or $(NAME),$(USER)) $(RESET) +dev-author: $(VENV)/.installed ## Compte local direct : NAME=x, RESET=--reset (mot de passe), ADMIN=--admin (accès à /api/admin/) ; mot de passe demandé + cd backend && DJANGO_SETTINGS_MODULE=pyquest_api.settings.dev PYQUEST_DB_PATH=$(CURDIR)/.local/stack/db.sqlite3 ../$(VENV_PY) manage.py create_author $(or $(NAME),$(USER)) $(RESET) $(ADMIN) validate-levels: $(VENV)/.installed ## Valide les niveaux (schéma, manifeste, traductions) $(VENV_PY) scripts/validate_levels.py diff --git a/backend/authors/management/commands/create_author.py b/backend/authors/management/commands/create_author.py index e8964d8..244bc72 100644 --- a/backend/authors/management/commands/create_author.py +++ b/backend/authors/management/commands/create_author.py @@ -21,6 +21,7 @@ class Command(BaseCommand): def add_arguments(self, parser: CommandParser) -> None: parser.add_argument("username") parser.add_argument("--reset", action="store_true", help="Change le mot de passe d'un compte existant") + parser.add_argument("--admin", action="store_true", help="Compte administrateur : accès à l'administration (/api/admin/) en plus de l'éditeur") def handle(self, *args: Any, **options: Any) -> None: username: str = options["username"].strip().lower() @@ -39,10 +40,13 @@ class Command(BaseCommand): raise CommandError(" ".join(error.messages)) from error user = existing or User(username=username) - user.is_staff = False # is_staff ouvre l'administration : réservé aux responsables - user.is_superuser = False + # is_staff ouvre l'administration : réservé aux responsables (option --admin). + # --admin élève ; changer seulement un mot de passe (--reset) ne retire jamais de droits existants. + if options["admin"] or not existing: + user.is_staff = bool(options["admin"]) + user.is_superuser = bool(options["admin"]) user.is_active = True user.set_password(password) user.save() access.grant(user) - self.stdout.write(self.style.SUCCESS(f"Compte auteur {username} {'mis à jour' if existing else 'créé'}.")) + self.stdout.write(self.style.SUCCESS(f"Compte auteur {username} {'mis à jour' if existing else 'créé'}" + (" (administrateur)." if options["admin"] else "."))) diff --git a/backend/authors/tests.py b/backend/authors/tests.py index db396e5..6595176 100644 --- a/backend/authors/tests.py +++ b/backend/authors/tests.py @@ -178,6 +178,21 @@ class CreateAuthorCommandTest(TestCase): self.assertTrue(user.check_password(PASSWORD)) self.assertEqual(self.client.post(LOGIN, {"username": "ada", "password": PASSWORD}).status_code, 302) + def test_admin_option_creates_an_administrator_who_can_also_edit(self) -> None: + self.run_command("chef", "--admin") + user = User.objects.get(username="chef") + self.assertTrue(user.is_staff and user.is_superuser and access.is_author(user)) + # Un simple changement de mot de passe (--reset) ne retire pas les droits d'administration. + self.run_command("chef", "--reset") + user.refresh_from_db() + self.assertTrue(user.is_staff and user.is_superuser) + + def test_admin_option_promotes_an_existing_author(self) -> None: + make_user("denis") + self.run_command("denis", "--reset", "--admin") + user = User.objects.get(username="denis") + self.assertTrue(user.is_staff and user.is_superuser) + def test_rejects_weak_or_mismatched_passwords(self) -> None: with self.assertRaises(CommandError): self.run_command("ada", passwords=("court", "court"))