Django源码分析之权限系统_擒贼先擒王

乍见

Django内置的权限系统已经很完善了,加上django-guardian提供的功能,基本上能满足大部分的权限需求。暂且不说django-guardian,我们先来看下Django内置的权限系统:django.contrib.auth 包。

相识

一般权限系统分为全局权限和对象权限。Django只提供了一个对象权限的框架,具体实现由第三方库django-gardian完成。我们只看全局权限。

先来看auth包暴露出哪些接口。

django.contrib.auth.__init__.py

def load_backend(path):
    return import_string(path)()

def _get_backends(return_tuples=False):
    backends = []
    for backend_path in settings.AUTHENTICATION_BACKENDS:
        backend = load_backend(backend_path)
        backends.append((backend, backend_path) if return_tuples else backend)
    if not backends:
        raise ImproperlyConfigured(
            ‘No authentication backends have been defined. Does ‘
            ‘AUTHENTICATION_BACKENDS contain anything?‘
        )
    return backends

def get_backends():
    return _get_backends(return_tuples=False)

前三个方法都是为了加载backends。一个backend其实就是一个class,必须实现authenticate和get_user两个方法。每当我们这样验证用户时

authenticate(username=‘username‘, password=‘password‘)

django就会去调用这些backend class,用其提供的方法去验证用户权限。那django是如何知道要调用哪些backend class呢?答案就在settings.py中,默认为

AUTHENTICATION_BACKENDS = [‘django.contrib.auth.backends.ModelBackend‘]

那Django是如何调用这些backend class的呢?

def authenticate(**credentials):
    """
    If the given credentials are valid, return a User object.
    """
    for backend, backend_path in _get_backends(return_tuples=True):
        try:
            inspect.getcallargs(backend.authenticate, **credentials)
        except TypeError:
            # This backend doesn‘t accept these credentials as arguments. Try the next one.
            continue

        try:
            user = backend.authenticate(**credentials)
        except PermissionDenied:
            # This backend says to stop in our tracks - this user should not be allowed in at all.
            return None
        if user is None:
            continue
        # Annotate the user object with the path of the backend.
        user.backend = backend_path
        return user

    # The credentials supplied are invalid to all backends, fire signal
    user_login_failed.send(sender=__name__,
            credentials=_clean_credentials(credentials))

由此可见,Django会在第一个验证正确的backend class调用完成后停止,或者碰到PermissionDenied异常也会停止,所以backend class的顺序也很重要。可以添加自定义的backend class。

def login(request, user):
    """
    Persist a user id and a backend in the request. This way a user doesn‘t
    have to reauthenticate on every request. Note that data set during
    the anonymous session is retained when the user logs in.
    """
    session_auth_hash = ‘‘
    if user is None:
        user = request.user
    if hasattr(user, ‘get_session_auth_hash‘):
        session_auth_hash = user.get_session_auth_hash()

    if SESSION_KEY in request.session:
        if _get_user_session_key(request) != user.pk or (
                session_auth_hash and
                request.session.get(HASH_SESSION_KEY) != session_auth_hash):
            # To avoid reusing another user‘s session, create a new, empty
            # session if the existing session corresponds to a different
            # authenticated user.
            request.session.flush()
    else:
        request.session.cycle_key()
    request.session[SESSION_KEY] = user._meta.pk.value_to_string(user)
    request.session[BACKEND_SESSION_KEY] = user.backend
    request.session[HASH_SESSION_KEY] = session_auth_hash
    if hasattr(request, ‘user‘):
        request.user = user
    rotate_token(request)
    user_logged_in.send(sender=user.__class__, request=request, user=user)

login方法,顾名思义,登录用户,同时设置好session,最后发送登入成功通知

def logout(request):
    """
    Removes the authenticated user‘s ID from the request and flushes their
    session data.
    """
    # Dispatch the signal before the user is logged out so the receivers have a
    # chance to find out *who* logged out.
    user = getattr(request, ‘user‘, None)
    if hasattr(user, ‘is_authenticated‘) and not user.is_authenticated():
        user = None
    user_logged_out.send(sender=user.__class__, request=request, user=user)

    # remember language choice saved to session
    language = request.session.get(LANGUAGE_SESSION_KEY)

    request.session.flush()

    if language is not None:
        request.session[LANGUAGE_SESSION_KEY] = language

    if hasattr(request, ‘user‘):
        from django.contrib.auth.models import AnonymousUser
        request.user = AnonymousUser()

相对的,logout方法,负责登出用户,清理session,最后设置当前用户为匿名用户

def get_user_model():
    """
    Returns the User model that is active in this project.
    """
    try:
        return django_apps.get_model(settings.AUTH_USER_MODEL)
    except ValueError:
        raise ImproperlyConfigured("AUTH_USER_MODEL must be of the form ‘app_label.model_name‘")
    except LookupError:
        raise ImproperlyConfigured(
            "AUTH_USER_MODEL refers to model ‘%s‘ that has not been installed" % settings.AUTH_USER_MODEL
        )

Django不推荐直接使用User class,而是通知get_user_model方法获取当前的用户class(或者使用settins.AUTH_USER_MODEL)。这是为了防止因为开发者使用了自定义用户class而导致的信息错误。

def update_session_auth_hash(request, user):
    """
    Updating a user‘s password logs out all sessions for the user if
    django.contrib.auth.middleware.SessionAuthenticationMiddleware is enabled.

    This function takes the current request and the updated user object from
    which the new session hash will be derived and updates the session hash
    appropriately to prevent a password change from logging out the session
    from which the password was changed.
    """
    if hasattr(user, ‘get_session_auth_hash‘) and request.user == user:
        request.session[HASH_SESSION_KEY] = user.get_session_auth_hash()

最后这个方法的使用场景很少。一般我们更新用户密码时,会在session中清除用户登录信息,导致用户需要重新登录。而使用update_session_auth_hash我们就可以在更新用户密码的同时更新用户的session信息,这样,用户就不需要重新登录了。

回想

擒贼先擒王,以上都是django.contrib.auth包中的__init__.py入口文件中的内容,背后还有很多“能工巧匠”,否则怎么支撑起auth整套权限系统?后续文章会一一介绍。

原文地址:https://www.cnblogs.com/dtstack/p/10065610.html

时间: 2024-11-07 13:42:10

Django源码分析之权限系统_擒贼先擒王的相关文章

django源码分析

原文网址 https://www.jianshu.com/p/17d78b52c732?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation 环境说明 [x] Python 3.5 [x] Django 1.10.4 创建一个django项目 C:\Users\zhengtong>C:\Python35\Scripts\django-admin.exe st

django源码分析——静态文件staticfiles中间件

本文环境python3.5.2,django1.10.x系列 1.在上一篇文章中已经分析过handler的处理过程,其中load_middleware就是将配置的中间件进行初始化,然后调用相应的设置方法. django框架提供的认证,回话保持,静态文件调试处理等都是通过以中间件的形式来处理. 2.本节就分析一下django框架提供的staticfiles中间件,该中间件分别实现了三个框架的命令,分别为collectstatic,findstatic,runserver. 其中,runserver

wifidog源码分析Lighttpd1.4.20源码分析之fdevent系统(1)---fdevents结构体和fdevent系统对外接口

前面讲了lighttpd的插件系统,这一篇将看一看lighttpd中的fdevent系统.fdevent系统主要是处理各种IO事件,在web服务器中,主要就是向socket写数据和从socket读数据.通常,web服务器是IO密集型程序,这就要求在数据的读写上,web服务器必须能够具有很好的性能,不会因为某个socket的阻塞而致使其他socket也被阻塞,否则会大大降低服务器的性能.因此,大部分的web服务器都采用非阻塞IO进行数据的读写.lighttpd通过fdevent系统,采用类似OO中

wifidog源码分析Lighttpd1.4.20源码分析之插件系统(3)---PLUGIN_TO_SLOT宏

前面讲了lighttpd插件系统的加载和初始化,这一篇中,将介绍一下plugin.c中的宏PLUGIN_TO_SLOT.在将PLUGIN_TO_SLOT宏之前,我们先来看看lighttpd中插件系统的对外接口.这个接口所对的“外”指的是lighttpd服务器.前面已经提到,在运行的过程中,lighttpd不知道所加载的插件都是干什么用的,只知道这些插件所实现的接口,也就是在plugin结构体中那些函数指针有哪些对于某个插件是NULL,哪些是具体的函数地址.既然lighttpd只知道这些,那么它又

Django源码分析——shotcuts

1 def render(request, *args, **kwargs): 2 """ 3 Returns a HttpResponse whose content is filled with the result of calling||返回的HttpResponse的内容充满了调用的结果 4 django.template.loader.render_to_string() with the passed arguments. 5 Uses a RequestCon

django源码分析---- Model类型&Field类型

djiango在数据库这方式自己实现了orm(object relationship mapping 对象关系模型映射).这个主要是用到python 元类这一 项python中的高级技术来实现的. class ModelBase(type): def __new__(cls,name,bases,attrs): # ..... pass pass class Model(metaclass=ModelBase): pass # 在这之后所有的用户自定义模型都继承自Model类 class Per

Django源码分析——urlresolvers.py

因为看URL,所以跟到了urlresolvers.py regex是正则表达式 view kwargs name 就是那个 name='blog' prefix 1 class RegexURLResolver(LocaleRegexProvider): 2 def __init__(self, regex, urlconf_name, default_kwargs=None, app_name=None, namespace=None): 3 LocaleRegexProvider.__ini

Django源码分析——response.py

1 class HttpResponse(HttpResponseBase): 2 """ 3 An HTTP response class with a string as content. 4 5 This content that can be read, appended to or replaced. 6 """ 7 8 streaming = False 9 10 def __init__(self, content='', *arg

Django源码分析——URLS(还没弄明白)

1 def url(regex, view, kwargs=None, name=None, prefix=''): 2 if isinstance(view, (list,tuple)): 3 # For include(...) processing. 4 urlconf_module, app_name, namespace = view 5 return RegexURLResolver(regex, urlconf_module, kwargs, app_name=app_name,