Skip to content
Snippets Groups Projects
Verified Commit f90827d1 authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

Allow combining matching fields for OR

parent e6445cd4
No related branches found
No related tags found
1 merge request!94Resolve "Allow OR matching in addition to AND matching"
Pipeline #65987 passed with warnings
...@@ -9,6 +9,11 @@ and this project adheres to `Semantic Versioning`_. ...@@ -9,6 +9,11 @@ and this project adheres to `Semantic Versioning`_.
Unreleased Unreleased
---------- ----------
Added
~~~~~
* Person field matching can now be matched disjunctive
Fixed Fixed
~~~~~ ~~~~~
......
...@@ -19,7 +19,7 @@ Licence ...@@ -19,7 +19,7 @@ Licence
:: ::
Copyright © 2020, 2021 Dominik George <dominik.george@teckids.org> Copyright © 2020, 2021, 2022 Dominik George <dominik.george@teckids.org>
Copyright © 2020 Tom Teichler <tom.teichler@teckids.org> Copyright © 2020 Tom Teichler <tom.teichler@teckids.org>
Licenced under the EUPL, version 1.2 or later, by Teckids e.V. (Bonn, Germany). Licenced under the EUPL, version 1.2 or later, by Teckids e.V. (Bonn, Germany).
......
...@@ -15,7 +15,7 @@ class LDAPConfig(AppConfig): ...@@ -15,7 +15,7 @@ class LDAPConfig(AppConfig):
} }
licence = "EUPL-1.2+" licence = "EUPL-1.2+"
copyright_info = ( copyright_info = (
([2020, 2021], "Dominik George", "dominik.george@teckids.org"), ([2020, 2021, 2022], "Dominik George", "dominik.george@teckids.org"),
([2020], "Tom Teichler", "tom.teichler@teckids.org"), ([2020], "Tom Teichler", "tom.teichler@teckids.org"),
) )
......
...@@ -150,6 +150,16 @@ class LDAPGroupSyncOwnerAttrType(ChoicePreference): ...@@ -150,6 +150,16 @@ class LDAPGroupSyncOwnerAttrType(ChoicePreference):
row = "ldap_group_sync_owner_attr" row = "ldap_group_sync_owner_attr"
@site_preferences_registry.register
class LDAPMatchingMode(ChoicePreference):
section = ldap
name = "matching_mode"
default = "AND"
required = True
verbose_name = _("LDAP sync matching mode")
choices = [("AND", _("All fields must match"), "OR", _("Any one field must match"))]
@site_preferences_registry.register @site_preferences_registry.register
class EnableLDAPPasswordChange(BooleanPreference): class EnableLDAPPasswordChange(BooleanPreference):
section = ldap section = ldap
......
...@@ -7,7 +7,7 @@ from django.apps import apps ...@@ -7,7 +7,7 @@ from django.apps import apps
from django.conf import settings from django.conf import settings
from django.core.files import File from django.core.files import File
from django.db import DataError, IntegrityError, transaction from django.db import DataError, IntegrityError, transaction
from django.db.models import fields from django.db.models import Q, fields
from django.db.models.fields.files import FileField from django.db.models.fields.files import FileField
from django.utils.text import slugify from django.utils.text import slugify
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
...@@ -270,11 +270,24 @@ def ldap_sync_from_user(user, dn, attrs): ...@@ -270,11 +270,24 @@ def ldap_sync_from_user(user, dn, attrs):
if missing_key not in matches: if missing_key not in matches:
defaults[missing_key] = getattr(user, missing_key) defaults[missing_key] = getattr(user, missing_key)
if get_site_preferences()["ldap__create_missing_persons"]: q = Q()
person, created = Person.objects.get_or_create(**matches, defaults=defaults) matching_mode = get_site_preferences()["ldap__matching_mode"]
else: for field, value in matches.items():
person = Person.objects.get(**matches) add_q = Q(**{field: value})
if matching_mode == "AND":
q = q & add_q
elif matching_mode == "OR":
q = q | add_q
else:
raise ValueError(f"Invalid setting for matching mode: {matching_mode}")
try:
person = Person.objects.get(q)
created = False created = False
except Person.DoesNotExist:
if get_site_preferences()["ldap__create_missing_persons"]:
person = Person.objects.create(**matches, **defaults)
created = True
user.save() user.save()
person.user = user person.user = user
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment