feat: add --reset-passwords and improve existing user candidate matching
This commit is contained in:
parent
fe0e04b3ba
commit
74526cf7a6
1 changed files with 28 additions and 4 deletions
|
|
@ -98,6 +98,12 @@ class Command(BaseCommand):
|
||||||
default=False,
|
default=False,
|
||||||
help="Si spécifié, met à jour la configuration des utilisateurs déjà existants au lieu de les ignorer.",
|
help="Si spécifié, met à jour la configuration des utilisateurs déjà existants au lieu de les ignorer.",
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--reset-passwords",
|
||||||
|
action="store_true",
|
||||||
|
default=False,
|
||||||
|
help="Régénère un nouveau mot de passe pour les utilisateurs existants lors d'une mise à jour.",
|
||||||
|
)
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
input_file = options["file"]
|
input_file = options["file"]
|
||||||
|
|
@ -108,8 +114,9 @@ class Command(BaseCommand):
|
||||||
desktop_url = options["desktop_url"]
|
desktop_url = options["desktop_url"]
|
||||||
mobile_url = options["mobile_url"]
|
mobile_url = options["mobile_url"]
|
||||||
install_url = options["install_url"]
|
install_url = options["install_url"]
|
||||||
dry_run = options["dry_run"]
|
|
||||||
update_existing = options["update_existing"]
|
update_existing = options["update_existing"]
|
||||||
|
reset_passwords = options.get("reset_passwords", False)
|
||||||
|
dry_run = options["dry_run"]
|
||||||
|
|
||||||
if dry_run:
|
if dry_run:
|
||||||
self.stdout.write(self.style.WARNING("=== MODE SIMULATION (DRY-RUN) : Aucune modification en base ==="))
|
self.stdout.write(self.style.WARNING("=== MODE SIMULATION (DRY-RUN) : Aucune modification en base ==="))
|
||||||
|
|
@ -126,12 +133,12 @@ class Command(BaseCommand):
|
||||||
source_config = UserConfig.objects.get(user=source_user)
|
source_config = UserConfig.objects.get(user=source_user)
|
||||||
except UserConfig.DoesNotExist:
|
except UserConfig.DoesNotExist:
|
||||||
raise CommandError(
|
raise CommandError(
|
||||||
f"L'utilisateur source '{source_user.username}' n'a pas de UserConfig associée à dupliquer."
|
f"L'utilisateur source '{source_user.username}' ne possède pas de configuration (UserConfig)."
|
||||||
)
|
)
|
||||||
|
|
||||||
self.stdout.write(
|
self.stdout.write(
|
||||||
self.style.SUCCESS(
|
self.style.SUCCESS(
|
||||||
f"Utilisateur source sélectionné : {source_user.username} ({source_user.get_full_name() or source_user.email})"
|
f"Utilisateur modèle source validé : {source_user.username} ({source_user.get_full_name() or source_user.email})"
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -203,6 +210,16 @@ class Command(BaseCommand):
|
||||||
existing_user = User.objects.filter(email__iexact=email).first()
|
existing_user = User.objects.filter(email__iexact=email).first()
|
||||||
if not existing_user and given_username:
|
if not existing_user and given_username:
|
||||||
existing_user = User.objects.filter(username__iexact=given_username).first()
|
existing_user = User.objects.filter(username__iexact=given_username).first()
|
||||||
|
if not existing_user and not given_username and first_name and last_name:
|
||||||
|
# Vérifier si un utilisateur existant porte déjà le même prénom et nom avec le login standard
|
||||||
|
candidate = generate_username(first_name, last_name, set())
|
||||||
|
match_user = User.objects.filter(username__iexact=candidate).first()
|
||||||
|
if (
|
||||||
|
match_user
|
||||||
|
and match_user.first_name.lower() == first_name.lower()
|
||||||
|
and match_user.last_name.lower() == last_name.lower()
|
||||||
|
):
|
||||||
|
existing_user = match_user
|
||||||
|
|
||||||
if existing_user and not update_existing:
|
if existing_user and not update_existing:
|
||||||
self.stdout.write(
|
self.stdout.write(
|
||||||
|
|
@ -226,7 +243,10 @@ class Command(BaseCommand):
|
||||||
# Générer ou réutiliser le login
|
# Générer ou réutiliser le login
|
||||||
if existing_user:
|
if existing_user:
|
||||||
username = existing_user.username
|
username = existing_user.username
|
||||||
password = "[CONSERVÉ]"
|
if reset_passwords:
|
||||||
|
password = generate_secure_password(20)
|
||||||
|
else:
|
||||||
|
password = "[CONSERVÉ]"
|
||||||
user_obj = existing_user
|
user_obj = existing_user
|
||||||
action_label = "Mis à jour"
|
action_label = "Mis à jour"
|
||||||
else:
|
else:
|
||||||
|
|
@ -265,6 +285,10 @@ class Command(BaseCommand):
|
||||||
user_obj.first_name = first_name
|
user_obj.first_name = first_name
|
||||||
if last_name:
|
if last_name:
|
||||||
user_obj.last_name = last_name
|
user_obj.last_name = last_name
|
||||||
|
if email:
|
||||||
|
user_obj.email = email
|
||||||
|
if reset_passwords and password and password != "[CONSERVÉ]":
|
||||||
|
user_obj.set_password(password)
|
||||||
user_obj.save()
|
user_obj.save()
|
||||||
count_updated += 1
|
count_updated += 1
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue