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

Link/Create persons if a matching one is found for a user

Closes #332
parent 6563144f
No related branches found
No related tags found
1 merge request!414Resolve "Allow automatic linking of persons to account by e-mail address"
Pipeline #5017 passed
......@@ -5,6 +5,7 @@ from django.utils.translation import gettext_lazy as _
from dynamic_preferences.preferences import Section
from dynamic_preferences.types import (
BooleanPreference,
ChoicePreference,
FilePreference,
MultipleChoicePreference,
......@@ -170,6 +171,24 @@ class PrimaryGroupField(ChoicePreference):
return Person.syncable_fields_choices()
@site_preferences_registry.register
class AutoCreatePerson(BooleanPreference):
section = account
name = "auto_create_person"
default = False
required = False
verbose_name = _("Automatically create new persons for new users")
@site_preferences_registry.register
class AutoLinkPerson(BooleanPreference):
section = account
name = "auto_link_person"
default = False
required = False
verbose_name = _("Automatically link existing persons to new users by their e-mail address")
@site_preferences_registry.register
class SchoolName(StringPreference):
section = school
......
......@@ -2,8 +2,8 @@ from typing import Callable
from django.http import HttpRequest, HttpResponse
from ..models import DummyPerson
from .core_helpers import has_person
from ..models import DummyPerson, Person
from .core_helpers import get_site_preferences, has_person
class EnsurePersonMiddleware:
......@@ -12,6 +12,9 @@ class EnsurePersonMiddleware:
It is needed to inject a dummy person to a superuser that would otherwise
not have an associated person, in order they can get their account set up
without external help.
In addition, if configured in preferences, it auto-creates or links persons
to regular users if they match.
"""
def __init__(self, get_response: Callable):
......@@ -25,6 +28,22 @@ class EnsurePersonMiddleware:
first_name=request.user.first_name, last_name=request.user.last_name
)
request.user.person = dummy_person
else:
prefs = get_site_preferences()
if prefs.get("account__auto_link_person", False):
person, created = Person.objects.get_or_create(
email=request.user.email,
defaults={
"first_name": request.user.first_name,
"last_name": request.user.last_name,
},
)
if created and not prefs.get("account__auto_create_person"):
return
person.user = request.user
person.save()
response = self.get_response(request)
return response
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