mirror of
https://github.com/deunix-educ/PyQuest.git
synced 2026-11-25 08:19:44 +01:00
create_author: --admin option, --reset keeps existing rights
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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/)
|
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
|
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
|
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)
|
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)
|
validate-levels: $(VENV)/.installed ## Valide les niveaux (schéma, manifeste, traductions)
|
||||||
$(VENV_PY) scripts/validate_levels.py
|
$(VENV_PY) scripts/validate_levels.py
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ class Command(BaseCommand):
|
|||||||
def add_arguments(self, parser: CommandParser) -> None:
|
def add_arguments(self, parser: CommandParser) -> None:
|
||||||
parser.add_argument("username")
|
parser.add_argument("username")
|
||||||
parser.add_argument("--reset", action="store_true", help="Change le mot de passe d'un compte existant")
|
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:
|
def handle(self, *args: Any, **options: Any) -> None:
|
||||||
username: str = options["username"].strip().lower()
|
username: str = options["username"].strip().lower()
|
||||||
@@ -39,10 +40,13 @@ class Command(BaseCommand):
|
|||||||
raise CommandError(" ".join(error.messages)) from error
|
raise CommandError(" ".join(error.messages)) from error
|
||||||
|
|
||||||
user = existing or User(username=username)
|
user = existing or User(username=username)
|
||||||
user.is_staff = False # is_staff ouvre l'administration : réservé aux responsables
|
# is_staff ouvre l'administration : réservé aux responsables (option --admin).
|
||||||
user.is_superuser = False
|
# --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.is_active = True
|
||||||
user.set_password(password)
|
user.set_password(password)
|
||||||
user.save()
|
user.save()
|
||||||
access.grant(user)
|
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 ".")))
|
||||||
|
|||||||
@@ -178,6 +178,21 @@ class CreateAuthorCommandTest(TestCase):
|
|||||||
self.assertTrue(user.check_password(PASSWORD))
|
self.assertTrue(user.check_password(PASSWORD))
|
||||||
self.assertEqual(self.client.post(LOGIN, {"username": "ada", "password": PASSWORD}).status_code, 302)
|
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:
|
def test_rejects_weak_or_mismatched_passwords(self) -> None:
|
||||||
with self.assertRaises(CommandError):
|
with self.assertRaises(CommandError):
|
||||||
self.run_command("ada", passwords=("court", "court"))
|
self.run_command("ada", passwords=("court", "court"))
|
||||||
|
|||||||
Reference in New Issue
Block a user