auth.py 1.37 KB
Newer Older
1
from django import http
2
from django.shortcuts import redirect
Enrico Zini's avatar
Enrico Zini committed
3
import backend.models as bmodels
Enrico Zini's avatar
Enrico Zini committed
4
from django.contrib.auth.backends import RemoteUserBackend
Enrico Zini's avatar
Enrico Zini committed
5

Enrico Zini's avatar
Enrico Zini committed
6
class NMUserBackend(RemoteUserBackend):
Enrico Zini's avatar
Enrico Zini committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
    """
    RemoteUserBackend customised to create User objects from Person
    """

    # Copied from RemoteUserBackend and tweaked to validate against Person
    def authenticate(self, remote_user):
        """
        The username passed as ``remote_user`` is considered trusted.  This
        method simply returns the ``User`` object with the given username,
        creating a new ``User`` object if ``create_unknown_user`` is ``True``.

        Returns None if ``create_unknown_user`` is ``False`` and a ``User``
        object with the given username is not found in the database.
        """
        if not remote_user:
            return
        username = self.clean_username(remote_user)

        # Get the Person for this username: Person is authoritative over User
26 27 28
        # Allow user@alioth without -guest, for cases like retired DDs who are
        # DMs (Edward Betts <edward> is an example)
        if username.endswith("@debian.org") or username.endswith("@users.alioth.debian.org"):
29
            try:
30
                return bmodels.Person.objects.get(username=username)
Enrico Zini's avatar
Enrico Zini committed
31 32
            except bmodels.Person.DoesNotExist:
                return None
33
        else:
Enrico Zini's avatar
Enrico Zini committed
34
            return None