mirror of
https://github.com/ae-utbm/sith.git
synced 2024-12-22 15:51:19 +00:00
custom auth backend
This commit is contained in:
parent
4f233538e0
commit
81e163812e
29
core/auth_backends.py
Normal file
29
core/auth_backends.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
from django.contrib.auth.backends import ModelBackend
|
||||||
|
from django.contrib.auth.models import Permission
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from core.models import User
|
||||||
|
|
||||||
|
|
||||||
|
class SithModelBackend(ModelBackend):
|
||||||
|
"""Custom auth backend for the Sith.
|
||||||
|
|
||||||
|
In fact, it's the exact same backend as `django.contrib.auth.backend.ModelBackend`,
|
||||||
|
with the exception that group permissions are fetched slightly differently.
|
||||||
|
Indeed, django tries by default to fetch the permissions associated
|
||||||
|
with all the `django.contrib.auth.models.Group` of a user ;
|
||||||
|
however, our User model overrides that, so the actual linked group model
|
||||||
|
is [core.models.Group][].
|
||||||
|
Instead of having the relation `auth_perm --> auth_group <-- core_user`,
|
||||||
|
we have `auth_perm --> auth_group <-- core_group <-- core_user`.
|
||||||
|
|
||||||
|
Thus, this backend make the small tweaks necessary to make
|
||||||
|
our custom models interact with the django auth.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def _get_group_permissions(self, user_obj: User):
|
||||||
|
return Permission.objects.filter(group__group__users=user_obj)
|
@ -290,6 +290,7 @@ STORAGES = {
|
|||||||
# Auth configuration
|
# Auth configuration
|
||||||
AUTH_USER_MODEL = "core.User"
|
AUTH_USER_MODEL = "core.User"
|
||||||
AUTH_ANONYMOUS_MODEL = "core.models.AnonymousUser"
|
AUTH_ANONYMOUS_MODEL = "core.models.AnonymousUser"
|
||||||
|
AUTHENTICATION_BACKENDS = ["core.auth_backends.SithModelBackend"]
|
||||||
LOGIN_URL = "/login"
|
LOGIN_URL = "/login"
|
||||||
LOGOUT_URL = "/logout"
|
LOGOUT_URL = "/logout"
|
||||||
LOGIN_REDIRECT_URL = "/"
|
LOGIN_REDIRECT_URL = "/"
|
||||||
|
Loading…
Reference in New Issue
Block a user