django的权限管理系统permission

来源:互联网 发布:服装尺码软件 编辑:程序博客网 时间:2024/05/01 14:06

1.为model添加权限

[html] view plaincopy
  1. class Task(models.Model):  
  2.     .......  
  3.     class Meta:  
  4.         permissions = (  
  5.             ('oprater_task','can change the tasks'),  
  6.         )  


2.views中可以使用如下方法来操作权限

[html] view plaincopy
  1. print request.user.has_perm('conf.oprater_task')  #conf为应用名,后面的为权限名  
  2. .....  
  3. myuser.user_permissions = [permission_list]   #myuser为通过request.user获取的user对象  
  4. myuser.user_permissions.add(permission, permission, ...)  
  5. myuser.user_permissions.remove(permission, permission, ...)  
  6. myuser.user_permissions.clear()  


3.在template中使用权限方法:

[html] view plaincopy
  1. {% if perms.conf %}  
  2.     <p>You have permission to do something in the foo app.</p>  
  3.     {% if perms.conf.oprater_task %}  
  4.         <p>You can vote!</p>  
  5.     {% endif %}  
  6.     {% if perms.conf.oprater_task %}  
  7.         <p>You can drive!</p>  
  8.     {% endif %}  
  9. {% else %}  
  10.     <p>You don't have permission to do anything in the foo app.</p>  
  11. {% endif %}  


 4.permission提供的装饰器

[python] view plaincopy
  1. from django.contrib.auth.decorators import login_required  
  2.  
  3. @login_required  
  4. def my_view(request):  
  5.     # ...  
  6.   
  7. def user_can_vote(user):  
  8.     return user.is_authenticated() and user.has_perm("polls.can_vote")  
  9.  
  10. @user_passes_test(user_can_vote, login_url="/login/")  
  11. def vote(request):  
  12.     # Code here can assume a logged-in user with the correct permission.  
  13.     ...  
  14.   
  15. from django.contrib.auth.decorators import permission_required  
  16.  
  17. @permission_required('polls.can_vote', login_url="/login/")  
  18. def vote(request):  


 5.user_passes_test的简便用法

 

[python] view plaincopy
  1. @user_passes_test(lambda u: u.is_superuser)  
  2.   
  3. #django源代码中的user_passes_test  
  4. def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):  
  5.     """ 
  6.     注意该函数的第一个参数 
  7.     Decorator for views that checks that the user passes the given test, 
  8.     redirecting to the log-in page if necessary. The test should be a callable 
  9.     that takes the user object and returns True if the user passes. 
  10.     """  
  11.   
  12.     def decorator(view_func):  
  13.         @wraps(view_func, assigned=available_attrs(view_func))  
  14.         def _wrapped_view(request, *args, **kwargs):  
  15.             if test_func(request.user):  
  16.                 return view_func(request, *args, **kwargs)  
  17.             path = request.build_absolute_uri()  
  18.             # If the login url is the same scheme and net location then just  
  19.             # use the path as the "next" url.  
  20.             login_scheme, login_netloc = urlparse.urlparse(login_url or  
  21.                                                         settings.LOGIN_URL)[:2]  
  22.             current_scheme, current_netloc = urlparse.urlparse(path)[:2]  
  23.             if ((not login_scheme or login_scheme == current_scheme) and  
  24.                 (not login_netloc or login_netloc == current_netloc)):  
  25.                 path = request.get_full_path()  
  26.             from django.contrib.auth.views import redirect_to_login  
  27.             return redirect_to_login(path, login_url, redirect_field_name)  
  28.         return _wrapped_view  
  29.     return decorator  

 

6.判断是否为超级管理员的过滤器:

[python] view plaincopy
  1. @user_passes_test(lambda u: u.is_superuser)  


0 0
原创粉丝点击