Django:调用从RemoteUserBackend继承的类的authenticate方法

时间:2016-06-01 21:55:13

标签: python django authentication

我目前无法自定义Djangos身份验证系统。 在我的方案中,没有数据库,通过http请求进行身份验证。没有可供阅读或写入的表格。

这个thread在这里与我正在处理的情况相同,但是我不明白他的例子中是如何创建用户的。我还从django手册中读到this有关REMOTE_USER身份验证的信息。回到用户在此处发布此代码的第一个链接。它包含后端和用户对象

后端 - 从RemoteUserBackend继承

from django.contrib.auth.backends import RemoteUserBackend

class MyRemoteUserBackend (RemoteUserBackend):
    # Create a User object if not already in the database?
    create_unknown_user = False

    def get_user (self, user_id):
        user = somehow_create_an_instance_of (MyUser, user_id)    ---->A
        return user

    def authenticate (self, **credentials):
        check_credentials ()
        user = somehow_create_an_instance_of (MyUser, credentials) ---->B
        return user

然后是用户:

from django.contrib.auth.models import User

class MyUser (User):

    def save (self):
        """saving to DB disabled"""
        pass

    objects = None # we cannot really use this w/o local DB

    username = ""  # and all the other properties likewise.
                   # They're defined as model.CharField or similar,
                   # and we can't allow that

    def get_group_permissions (self):
        """If you don't make your own permissions module,
           the default also will use the DB. Throw it away"""
        return [] # likewise with the other permission defs

    def get_and_delete_messages (self):
        """Messages are stored in the DB. Darn!"""
        return []

现在我的问题是我想在我看来做这样的事情

def my_view(request):
    username = request.POST['username']
    password = request.POST['password']
    user = authenticate(username=username, password=password)
    if user is not None:
        if user.is_active:
            login(request, user)

现在我如何在上面的代码片段中调用来自authenticate的{​​{1}}方法,以及MyRemoteUserBackend代码段的含义对此有什么建议

0 个答案:

没有答案