From add547e548cf5204f5a617f2a63265aa02dc04b5 Mon Sep 17 00:00:00 2001
From: Dominik George <dominik.george@teckids.org>
Date: Sun, 8 Nov 2020 11:52:35 +0100
Subject: [PATCH] Dynamically generate LDAPMatchingFields preference

---
 aleksis/apps/ldap/preferences.py    | 15 ---------------
 aleksis/apps/ldap/util/ldap_sync.py | 11 ++++++++++-
 2 files changed, 10 insertions(+), 16 deletions(-)

diff --git a/aleksis/apps/ldap/preferences.py b/aleksis/apps/ldap/preferences.py
index 53f6ed3..1fee285 100644
--- a/aleksis/apps/ldap/preferences.py
+++ b/aleksis/apps/ldap/preferences.py
@@ -35,21 +35,6 @@ class LDAPSyncCreateMissingPersons(BooleanPreference):
     verbose_name = _("Create missing persons for LDAP users")
 
 
-@site_preferences_registry.register
-class LDAPMatchingFields(ChoicePreference):
-    section = ldap
-    name = "matching_fields"
-    default = ""
-    required = False
-    verbose_name = _("LDAP sync matching fields")
-    choices = [
-        ("", "-----"),
-        ("match-email", _("Match only on email")),
-        ("match-name", _("Match only on name")),
-        ("match-email-name", _("Match on email and name")),
-    ]
-
-
 @site_preferences_registry.register
 class EnableLDAPGroupSync(BooleanPreference):
     section = ldap
diff --git a/aleksis/apps/ldap/util/ldap_sync.py b/aleksis/apps/ldap/util/ldap_sync.py
index e5c600a..57b890b 100644
--- a/aleksis/apps/ldap/util/ldap_sync.py
+++ b/aleksis/apps/ldap/util/ldap_sync.py
@@ -10,7 +10,7 @@ from django.db.models.fields.files import FileField
 from django.utils.text import slugify
 from django.utils.translation import gettext as _
 
-from dynamic_preferences.types import StringPreference
+from dynamic_preferences.types import MultipleChoicePreference, StringPreference
 from tqdm import tqdm
 
 from aleksis.core.registries import site_preferences_registry
@@ -101,6 +101,15 @@ def update_dynamic_preferences():
                 required = False
                 default = ""
 
+    @site_preferences_registry.register
+    class LDAPMatchingFields(MultipleChoicePreference):
+        section = section_ldap
+        name = "matching_fields"
+        default = []
+        required = False
+        verbose_name = _("LDAP sync matching fields")
+        choices = [("", "-----")} + [(field.name, field.name) for field in Perosn.syncable_fields()]
+
 
 def apply_templates(value, patterns, templates, separator="|"):
     """Regex-replace patterns in value in order."""
-- 
GitLab