Django - Read the current base user authentication class

I use my own authentication server with Django to automatically create and enter users from the old system. My class Backendis this:

from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.models import User
from sfi.models import Employee
import base64, hashlib

class SFIUserBackend(ModelBackend):
    def authenticate(self, username=None, password=None):
        if not username or not password:
            return

        digest = base64.standard_b64encode(hashlib.md5(password).digest())
        user = None
        try:
            employee = Employee.objects.get(login=username, passwd=digest)
            user, created = User.objects.get_or_create(username=username)
            if created:
                # setting attributes
                user.first_name = employee.names[:30]
                user.last_name = employee.surnames[:30]
                user.is_staff = True
                user.save()
        except Employee.DoesNotExist:
            pass

        return user

So far this is working fine. However, I need to read the base class of the current registered user in the template.

Usage request.user.backendsays that it userdoes not have an attribute backend ... and I cannot read it from the session (using request.session._auth_user_backend) because the Django template system complains that "variables and attributes may not start with underscores."

I use django.contrib.auth.views.loginso that users can log in. What am I missing?

+3
2

backend user, django.contrib.auth.authenticate(username='foo',password='bar').

, , AUTHENTICATION_BACKENDS, settings.py, , .

" ", backend, , , , . , SFIUserBackend.authenticate , django.contrib.auth.authenticate?

.

+3

.

0

Source: https://habr.com/ru/post/1792716/


All Articles