django-访问控制

django自带的用户认证系统提供了访问控制的的功能。
 
1.只容许登陆的用户登陆
 
django的用户可分为两类,一是可认证的用户,也就是在django.contrib.auth.models.User中注册了的;另外一种是匿名用户 django.contrib.auth.models.AnonymousUser,每一个访问的未登陆的用户都是该类的一个实例,而匿名用户是没法认证的,即 is_authenticated 方法永远返回 False,或者 is_anonymous返回True,咱们能够在代码逻辑中实现对匿名用户进行判断,而后拒绝其访问(403),或者重定向到登陆页面等。
 
from django.conf import settings
from django.shortcuts import redirect

def my_view(request):
    if not request.user.is_authenticated:
        return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
    # ...

 

以上就是在代码中重定向到登陆页面的作法,也能够返回错误信息:
 
from django.shortcuts import render

def my_view(request):
    if not request.user.is_authenticated:
        return render(request, 'myapp/login_error.html')
    # ...

 

因为这是一个常见的需求,因此django提供了一个装饰器来实现这个功能。
 
from django.contrib.auth.decorators import login_required

@login_required
def my_view(request):
    ...

 

 
这样大大的简化了工做。
 
  login_required(redirect_field_name='next', login_url=None) 
 
若用户没有登陆的话, 其将没法访问被装饰的视图,而是重定向到登陆页面,这个登陆页面可使用login_url参数指定,而且能够接受命名url的方式,如:@login_required(login_url='login'),固然前提是要有相应的命名的url。若是没有给定这个参数,将使用 settings.LOGIN_URL的值。
 
 
LOGIN_URL
Default: '/accounts/login/'
 
The URL where requests are redirected for login, especially when using the  login_required() decorator.
 
This setting also accepts  named URL patterns which can be used to reduce configuration duplication since you don’t have to define the URL in two places (settings and URLconf).
 
能够看见其有默认值,若是你不给参数,且登陆地址不是这个默认值,将触发404错误,也就是仍是重定向到那个页面去了,只是找不到而已。若强制重写为None,其会触发TypeError,若DEBUG=False,则应该是500系列的服务器错误。
 
可是,在重定向的时候,并非单纯跳转,而是会带一个next查询参数例如:
 
 
 
这样你就能够获取登陆前想要访问的页面(这里就是/account/change_info页面了),而后在登陆后重定向回去,而不用盲目重定向到首页,用户体验会更好点。
 
下面就是登陆视图中所作的配合:
 
next_to = request.GET.get('next', None)  # 获取是否有next的重定向,是一个相对路径,不含方案和域名
if next_to:
    return redirect(next_to)

 

固然你也能够改变redirect_field_name来改变next这个名称,固然在登陆视图里也要作想要的调整,也能够将其设为None来取消附带参数的行为。
 
 
注意:login_required 并不会检查用户是否处于活跃状态(is_active),而处理用户登陆的默认后台模板在1.10以前并不会拒绝非活跃用户登陆,而1.10版本就会。这意味着若是你使用的版本低于1.10,你必须在代码逻辑中拒绝非活跃用户登陆。
 
if user.is_active:  # 若用户是活跃的,即未冻结的,在1.10以前冻结用户默认也能登陆,因此须要本身认证
    login(request, user)    # 登陆
......    #其余处理
else:
    return HttpResponse('用户被冻结')

 

 
2.只容许staff身份的用户访问某个视图
 
django一样提供了一个便捷的装饰器来实现这个功能:
 
  staff_member_required(redirect_field_name='next', login_url='admin:login') 
 
 
能够看到其和上述的 login_required 参数上几乎同样,只是默认值有些许不一样,而在用法上也是同样的,但并无 settings.LOGIN_URL之类的设置层面上的退路。要注意一点,由于其默认是重定向至admin的登陆页面,若要经过复杂化url的方式来隐藏入口的时候,要当心其会暴露该url。
 
This decorator is used on the admin views that require authorization. A view decorated with this function will having the following behavior:
这个装饰器已经被admin的视图所使用了,其行为以下:
 
 
 
  • If the user is logged in, is a staff member (User.is_staff=True), and is active (User.is_active=True), execute the view normally.
若是用户已经登陆,且为staff身份,而且是活跃的,则正常执行所装饰的视图。
 
  • Otherwise, the request will be redirected to the URL specified by the login_url parameter, with the originally requested path in a query string variable specified by redirect_field_name. For example: /admin/login/?next=/admin/polls/question/3/.
不然,将会重定向到login_url,并带查询参数。
 
Example usage:
用例:
 
 
from django.contrib.admin.views.decorators import staff_member_required

@staff_member_required
def my_view(request):
    ...

 

能够看到其使用方法和 login_required()基本是同样的。
 

3.简单的函数测试验证法
 
django提供了一个装饰器,让咱们能够自定义一个简单的验证函数,只有经过该验证函数以后才能访问指定的视图。
 
  user_passes_test(func[, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME]) 
 
能够看到其比上面的两个装饰器多了一个参数,这个参数就是用指定验证函数的。
 
例如,我想要验证某个用户的邮箱是否符合我想要的格式:
 
原始的验证法:
 
from django.shortcuts import redirect

def my_view(request):
    if not request.user.email.endswith('@example.com'):
        return redirect('/login/?next=%s' % request.path)
    # ...
 
在视图函数中验证其是否以@example.com结尾。
 
下面是装饰器验证法:
 
from django.contrib.auth.decorators import user_passes_test

def email_check(user):
    return user.email.endswith('@example.com')

@user_passes_test(email_check)
def my_view(request):
    ...

 

注意:email_check函数其本质上也是返回布尔值,因此在自定义函数的时候,返回True表示经过,False表示不经过。不经过则重定向到登陆页面,其余细节和以前的两个装饰器同样。
 
 

基于类的视图的实现
 
基于类的视图在实现login_required的行为时,须要继承LoginRequiredMixin这个类,并将其放在继承树的最左边,同时相应的实如今类中书写,例如:
 
from django.contrib.auth.mixins import LoginRequiredMixin

class MyView(LoginRequiredMixin, View):
    login_url = '/login/'
    redirect_field_name = 'redirect_to'

 

 
注:这里是View是通用视图,这里并没要导入的代码,详情请参考基于类的视图。
 
还有更多的属性可供使用,可参考  AccessMixin 这个类。
 
 
实现user_passes_test行为也是相似的,首先继承UserPassesTestMixin这个类(在最左边),而后在类的里面重写test_func方法,该方法也是返回布尔值。
 
New in Django 1.9.
 
from django.contrib.auth.mixins import UserPassesTestMixin

class MyView(UserPassesTestMixin, View):

    def test_func(self):
        return self.request.user.email.endswith('@example.com')

 

注:上述的两个也是不验证是否为活跃用户的,因此使用的时候要当心。
相关文章
相关标签/搜索