class Server(models.Model):
...
class Meta:
permissions = (
("view_server", "can view server"),
("change_server_status", "Can change the status of server"),
)
#codename == view_server权限验证项
#name == can view server 可读的名称
content_type反应了permission属于哪个model, codename 如上面的view_server,代码逻辑中检查权限时要用, name是permission的描述,将permission打印到屏幕或页面时默认显示的就是name
user = User.objects.get(username="rock")
user.user_permissions = [permission_list]
user.user_permissions.add(permission, permission, …) #增加权限
user.user_permissions.remove(permission, permission, …) #删除权限
user.user_permissions.clear() #清空权限
user.get_all_permissions() #列出用户的所有权限
user.get_group_permissions() # 列出用户所属group的权限
In [1]: from django.contrib.auth.models import Group,User,Permission
In [3]: user = User.objects.get(username='rock-1')
In [4]: user.groups.all
Out[4]: <bound method BaseManager.all of <django.db.models.fields.related_descriptors.create_forward_many_to_many_manager.<locals>.ManyRelatedManager object at 0x7fd86cf49ef0>>
In [5]: user.groups.all()
Out[5]: <QuerySet [<Group: 51reboot>]>
In [6]: user.user_permissions.all()
Out[6]: <QuerySet []>
In [7]: per = Permission.objects.get(id=21)
In [8]: per.codename
Out[8]: 'delete_idc'
In [9]: user.user_permissions.add(per)
In [10]: user.user_permissions.all()
Out[10]: <QuerySet [<Permission: resources | idc | Can delete idc>]>
In [11]: user.user_permissions.remove(per)
In [12]: user.user_permissions.all()
Out[12]: <QuerySet []>
In [13]: user.user_permissions.add(per)
In [14]: user.user_permissions.clear()
In [15]: user.user_permissions.add(per)
In [16]: user.get_all_permissions()
Out[16]:
{'admin.add_logentry',
'admin.change_logentry',
'admin.delete_logentry',
'auth.add_group',
'auth.add_permission',
'auth.add_user',
'auth.change_group',
'auth.change_permission',
'auth.change_user',
'auth.delete_group',
'auth.delete_permission',
'auth.delete_user',
'contenttypes.add_contenttype',
'contenttypes.change_contenttype',
'contenttypes.delete_contenttype',
'resources.add_idc',
'resources.change_idc',
'resources.delete_idc',
'sessions.add_session',
'sessions.change_session',
'sessions.delete_session'}
In [17]: user.groups.clear()
In [18]: user.get_all_permissions()
Out[18]:
{'admin.add_logentry',
'admin.change_logentry',
'admin.delete_logentry',
'auth.add_group',
'auth.add_permission',
'auth.add_user',
'auth.change_group',
'auth.change_permission',
'auth.change_user',
'auth.delete_group',
'auth.delete_permission',
'auth.delete_user',
'contenttypes.add_contenttype',
'contenttypes.change_contenttype',
'contenttypes.delete_contenttype',
'resources.add_idc',
'resources.change_idc',
'resources.delete_idc',
'sessions.add_session',
'sessions.change_session',
'sessions.delete_session'}
In [19]: user.get_group_permissions()
Out[19]:
{'admin.add_logentry',
'admin.change_logentry',
'admin.delete_logentry',
'auth.add_group',
'auth.add_permission',
'auth.add_user',
'auth.change_group',
'auth.change_permission',
'auth.change_user',
'auth.delete_group',
'auth.delete_permission',
'auth.delete_user',
'contenttypes.add_contenttype',
'contenttypes.change_contenttype',
'contenttypes.delete_contenttype',
'resources.add_idc',
'resources.change_idc',
'resources.delete_idc',
'sessions.add_session',
'sessions.change_session',
'sessions.delete_session'}
In [20]: user.groups.all()
Out[20]: <QuerySet []>
group.permissions.set([permission_list])#设置权限
group.permissions.add(permission, permission, …)#添加权限
group.permissions.remove(permission, permission, …)#删除权限
group.permissions.clear()#情况权限
In [40]: group = Group.objects.get(name='51reboot')#取出一个组
In [41]: group.permissions.all()#列出组所有权限
Out[41]: <QuerySet [<Permission: admin | log entry | Can add log entry>, <Permission: admin | log entry | Can change log entry>, <Permission: admin | log entry | Can delete log entry>, <Permission: auth | group | Can add group>, <Permission: auth | group | Can change group>, <Permission: auth | group | Can delete group>, <Permission: auth | permission | Can add permission>, <Permission: auth | permission | Can change permission>, <Permission: auth | permission | Can delete permission>, <Permission: auth | user | Can add user>, <Permission: auth | user | Can change user>, <Permission: auth | user | Can delete user>, <Permission: contenttypes | content type | Can add content type>, <Permission: contenttypes | content type | Can change content type>, <Permission: contenttypes | content type | Can delete content type>, <Permission: resources | idc | Can add idc>, <Permission: resources | idc | Can change idc>, <Permission: resources | idc | Can delete idc>, <Permission: sessions | session | Can add session>, <Permission: sessions | session | Can change session>, '...(remaining elements truncated)...']>
In [42]: permission = Permission.objects.get(id=20)#先取出一个权限(Can change idc)
In [43]: group.permissions.remove(permission)#从组里删除这个权限
In [44]: group.permissions.all()#再次查看权限
Out[44]: <QuerySet [<Permission: admin | log entry | Can add log entry>, <Permission: admin | log entry | Can change log entry>, <Permission: admin | log entry | Can delete log entry>, <Permission: auth | group | Can add group>, <Permission: auth | group | Can change group>, <Permission: auth | group | Can delete group>, <Permission: auth | permission | Can add permission>, <Permission: auth | permission | Can change permission>, <Permission: auth | permission | Can delete permission>, <Permission: auth | user | Can add user>, <Permission: auth | user | Can change user>, <Permission: auth | user | Can delete user>, <Permission: contenttypes | content type | Can add content type>, <Permission: contenttypes | content type | Can change content type>, <Permission: contenttypes | content type | Can delete content type>, <Permission: resources | idc | Can add idc>, <Permission: resources | idc | Can delete idc>, <Permission: sessions | session | Can add session>, <Permission: sessions | session | Can change session>, <Permission: sessions | session | Can delete session>]>
In [45]: group.permissions.add(permission)添加权限
In [46]: group.permissions.all()#再次查看权限
Out[46]: <QuerySet [<Permission: admin | log entry | Can add log entry>, <Permission: admin | log entry | Can change log entry>, <Permission: admin | log entry | Can delete log entry>, <Permission: auth | group | Can add group>, <Permission: auth | group | Can change group>, <Permission: auth | group | Can delete group>, <Permission: auth | permission | Can add permission>, <Permission: auth | permission | Can change permission>, <Permission: auth | permission | Can delete permission>, <Permission: auth | user | Can add user>, <Permission: auth | user | Can change user>, <Permission: auth | user | Can delete user>, <Permission: contenttypes | content type | Can add content type>, <Permission: contenttypes | content type | Can change content type>, <Permission: contenttypes | content type | Can delete content type>, <Permission: resources | idc | Can add idc>, <Permission: resources | idc | Can change idc>, <Permission: resources | idc | Can delete idc>, <Permission: sessions | session | Can add session>, <Permission: sessions | session | Can change session>, '...(remaining elements truncated)...']>
In [48]: group.permissions.set([permission])#设置权限,会清空之前的所有权限,传入一个权限列表
In [49]: group.permissions.all()#再次查看权限
Out[49]: <QuerySet [<Permission: resources | idc | Can change idc>]>
In [50]: group.permissions.clear()#清空所有权限
In [51]: group.permissions.all()#再次查看权限
Out[51]: <QuerySet []>
from django.contrib.auth.decorators import login_required, permission_required
@login_required
@permission_required(’dashboard.view_server')
def my_view(request,*args,**kwargs):
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required, permission_required
class ServerView(TemplateView):
@method_decorator(login_required)
@method_decorator(permission_required(“dashboard.view_server”)
def get(self, request, *args, **kwargs):
...
if not request.user.has_perm(’dashboard.view_server')
return HttpResponse('Forbidden')
{% if user.is_authenticated %}
<p>Welcome, {{ user.username }}. Thanks for logging in.</p>
{% else %}
<p>Welcome, new user. Please log in.</p>
{% endif %}
{% if perms.dashboard.view_server %}
有权限
{% endif %}
from django.contrib.auth.mixins import PermissionRequiredMixin
class IndexView(LoginRequiredMixin,PermissionRequiredMixin,TemplateView):
template_name = 'index.html'
class Meta:
permissions = (
("modify_user_status", "修改用户状态"),
("modify_user_passwd", "修改用户密码"),
)
from resources.models import Idc
from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType
content_type = ContentType.objects.get_for_model(Idc)
permission = Permission.objects.create(codename='can_view',
name='Can view Idc',
content_type=content_type)