Skip to content
Snippets Groups Projects
Commit d3e86d66 authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Merge branch '237-super-user-has-no-person' into 'master'

Resolve "(Super)User has no person"

Closes #237

See merge request AlekSIS!240
parents dd7a8710 a0bcfb01
No related branches found
No related tags found
1 merge request!240Resolve "(Super)User has no person"
Pipeline #1833 failed
......@@ -246,6 +246,22 @@ class Person(ExtensibleModel):
self.primary_group = self.member_of.filter(name__regex=pattern).first()
class DummyPerson(Person):
""" A dummy person that is not stored into the database.
Used to temporarily inject a Person object into a User.
"""
class Meta:
managed = False
proxy = True
is_dummy = True
def save(self, *args, **kwargs):
pass
class Group(ExtensibleModel):
"""Any kind of group of persons in a school, including, but not limited
classes, clubs, and the like.
......
......@@ -124,6 +124,7 @@ MIDDLEWARE = [
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"easyaudit.middleware.easyaudit.EasyAuditMiddleware",
"maintenance_mode.middleware.MaintenanceModeMiddleware",
"aleksis.core.util.middlewares.EnsurePersonMiddleware",
# 'django.middleware.cache.FetchFromCacheMiddleware'
]
......
......@@ -2,18 +2,28 @@
{% load i18n %}
{% if not user.person and not user.is_anonymous %}
{% if user.person.is_dummy or not user.person and not user.is_anonymous %}
<div class="alert error">
<div>
<i class="material-icons left">error</i>
<p>
{% blocktrans %}
Your user account is not linked to a person. This means you
cannot access any school-related information. Please contact
the managers of AlekSIS at your school.
{% endblocktrans %}
</p>
{% if user.person.is_dummy %}
<p>
{% blocktrans %}
Your administrator account is not linked to any person. Therefore,
a dummy person has been linked to your account.
{% endblocktrans %}
</p>
{% else %}
<p>
{% blocktrans %}
Your user account is not linked to a person. This means you
cannot access any school-related information. Please contact
the managers of AlekSIS at your school.
{% endblocktrans %}
</p>
{% endif %}
</div>
</div>
{% endif %}
......@@ -119,7 +119,13 @@ def has_person(obj: Union[HttpRequest, Model]) -> bool:
else:
return False
return getattr(obj, "person", None) is not None
person = getattr(obj, "person", None)
if person is None:
return False
elif getattr(person, "is_dummy", False):
return False
else:
return True
def celery_optional(orig: Callable) -> Callable:
......
from typing import Callable
from django.core.exceptions import PermissionDenied
from django.http import HttpRequest, HttpResponse
from django.utils.translation import gettext_lazy as _
from .core_helpers import has_person
from ..models import DummyPerson
class EnsurePersonMiddleware:
""" Middleware that ensures that the logged-in user is linked to a person.
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.
"""
def __init__(self, get_response: Callable):
self.get_response = get_response
def __call__(self, request: HttpRequest) -> HttpResponse:
if not has_person(request):
if request.user.is_superuser:
# Super-users get a dummy person linked
dummy_person = DummyPerson(first_name=request.user.first_name, last_name=request.user.last_name)
request.user.person = dummy_person
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