Authentication login with legacy database in Django

1.4k Views Asked by At

I am creating a project in Django and I have a HelpDesk database that I would like to use in the project. In the database I have the table log_user where the logins information is stored, but I can not find a way to authenticate other than the Django default table.

Views.py

from django.shortcuts import render
from django.http import HttpResponse
from .forms import Person
from django.contrib.auth import authenticate, login

def user_login(request):
    if request.method == 'POST':
        form = Person(request.POST)
        if form.is_valid():
            username = request.POST['username']
            password = request.POST['password']
            user = authenticate(request, username=username, password=password)
            if user is not None:
                if user.is_active:
                    login(request, user)
                    return HttpResponse('Authenticated sucessfully')
                else:
                    return HttpResponse('Disable account')
            else:
                return HttpResponse('Invalid Login')
    else:
        form = Person()
        return render(request, 'login2.html', {'form': form})

forms.py

from django import forms
from .models import Loginteste

class Person(forms.ModelForm):
    class Meta:
        model = Loginteste
        fields = ['username', 'password']

models.py

from django.db import models

class Loginteste(models.Model):
    username = models.CharField(max_length=50, blank=True, null=True)
    password = models.CharField(max_length=50, blank=True, null=True)

    class Meta:
        managed = True
        db_table = 'loginteste'

auth_backend.py

from .models import Loginteste

class MyBackend:
    def authenticate(self, request, username=None, password=None):
        user = Loginteste.object.get(username=username)
        if user.check_password(password):
            return (request, user)

I tried with the default table "User" and the login function worked, but I can not do it with an already existing table (log_user in database HelpDesk).

1

There are 1 best solutions below

4
Ralf On

I think I understand your problem now.

As far as I know, Django is really dependant on the User model; it can be a custom user model, but there has to be some sort of User model.

When you build your own authentication backend, it still has to return User instances (or your custom User model instances).

For example:

File auth_backends.py

from django.contrib.auth.models import User
from .models import Loginteste

class MyAuthBackend:
    def authenticate(self, request, username=None, password=None):
        if username is None or password is None:
            # nothing to do
            return None

        # get 'User' object
        try:
            usr = User.objects.get(username=username)
        except User.DoesNotExist:
            return None

        # get 'Loginteste' object
        try:
            loginteste = Loginteste.objects.get(username=username)
        except Loginteste.DoesNotExist:
            return None

        # authenticate user
        if not loginteste.check_password(password):
            # incorrect password
            return None

        return usr

    def get_user(self, pk):
        try:
            return User.objects.get(pk=pk)
        except User.DoesNotExist:
            pass

        return None

File settings.py

AUTHENTICATION_BACKENDS = [
    'project_config.auth_backends.MyAuthBackend',
    # 'django.contrib.auth.backends.ModelBackend',
]

The only relatively simple option you might have is to use the model Loginteste as your custom user model. Everything else involves overriding a lot of Djangos code to make it work.