csdn中有无用python实现的rbac权限管理的具体例子
时间: 2023-07-23 09:09:05 浏览: 173
是的,CSDN上有很多用Python实现的RBAC权限管理的具体例子,以下是其中的一个例子:
```
from django.contrib.auth.decorators import login_required, permission_required
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.contrib.auth.models import User, Group, Permission
from django.contrib.contenttypes.models import ContentType
@login_required
@permission_required('auth.view_user', raise_exception=True)
def user_list(request):
users = User.objects.all()
return render(request, 'user_list.html', {'users':users})
@login_required
@permission_required('auth.add_user', raise_exception=True)
def user_create(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
email = request.POST.get('email')
user = User.objects.create_user(username, email, password)
user.save()
return HttpResponseRedirect(reverse('user_list'))
else:
return render(request, 'user_create.html')
@login_required
@permission_required('auth.change_user', raise_exception=True)
def user_edit(request, user_id):
user = get_object_or_404(User, id=user_id)
if request.method == 'POST':
username = request.POST.get('username')
email = request.POST.get('email')
user.username = username
user.email = email
user.save()
return HttpResponseRedirect(reverse('user_list'))
else:
return render(request, 'user_edit.html', {'user':user})
@login_required
@permission_required('auth.delete_user', raise_exception=True)
def user_delete(request, user_id):
user = get_object_or_404(User, id=user_id)
user.delete()
return HttpResponseRedirect(reverse('user_list'))
@login_required
@permission_required('auth.view_group', raise_exception=True)
def group_list(request):
groups = Group.objects.all()
return render(request, 'group_list.html', {'groups':groups})
@login_required
@permission_required('auth.add_group', raise_exception=True)
def group_create(request):
if request.method == 'POST':
name = request.POST.get('name')
group = Group(name=name)
group.save()
return HttpResponseRedirect(reverse('group_list'))
else:
return render(request, 'group_create.html')
@login_required
@permission_required('auth.change_group', raise_exception=True)
def group_edit(request, group_id):
group = get_object_or_404(Group, id=group_id)
if request.method == 'POST':
name = request.POST.get('name')
group.name = name
group.save()
return HttpResponseRedirect(reverse('group_list'))
else:
return render(request, 'group_edit.html', {'group':group})
@login_required
@permission_required('auth.delete_group', raise_exception=True)
def group_delete(request, group_id):
group = get_object_or_404(Group, id=group_id)
group.delete()
return HttpResponseRedirect(reverse('group_list'))
@login_required
@permission_required('auth.view_permission', raise_exception=True)
def permission_list(request):
permissions = Permission.objects.all()
return render(request, 'permission_list.html', {'permissions':permissions})
@login_required
@permission_required('auth.add_permission', raise_exception=True)
def permission_create(request):
if request.method == 'POST':
name = request.POST.get('name')
content_type_id = request.POST.get('content_type_id')
codename = request.POST.get('codename')
content_type = ContentType.objects.get(id=content_type_id)
permission = Permission(name=name, content_type=content_type, codename=codename)
permission.save()
return HttpResponseRedirect(reverse('permission_list'))
else:
content_types = ContentType.objects.all()
return render(request, 'permission_create.html', {'content_types':content_types})
@login_required
@permission_required('auth.change_permission', raise_exception=True)
def permission_edit(request, permission_id):
permission = get_object_or_404(Permission, id=permission_id)
if request.method == 'POST':
name = request.POST.get('name')
content_type_id = request.POST.get('content_type_id')
codename = request.POST.get('codename')
content_type = ContentType.objects.get(id=content_type_id)
permission.name = name
permission.content_type = content_type
permission.codename = codename
permission.save()
return HttpResponseRedirect(reverse('permission_list'))
else:
content_types = ContentType.objects.all()
return render(request, 'permission_edit.html', {'permission':permission, 'content_types':content_types})
@login_required
@permission_required('auth.delete_permission', raise_exception=True)
def permission_delete(request, permission_id):
permission = get_object_or_404(Permission, id=permission_id)
permission.delete()
return HttpResponseRedirect(reverse('permission_list'))
```
以上例子使用Django框架实现了RBAC权限管理的基本功能,包括用户管理、组管理、权限管理等。其中,`@permission_required`装饰器用于限制用户访问某些需要权限的视图函数。
阅读全文