.1.首先在permission表中添加一个资关联pid字段。
1 class Permission(models.Model): 2 """ 3 权限表 4 """ 5 title = models.CharField(max_length=32, verbose_name='标题') 6 url = models.CharField(max_length=32, verbose_name='权限') 7 menu = models.ForeignKey("Menu",on_delete=models.CASCADE,null=True) 8 # is_menu = models.BooleanField(default=False, verbose_name='是否是菜单') 9 # icon = models.CharField(max_length=32, verbose_name='图标', null=True, blank=True) 10 name = models.CharField(max_length=32,verbose_name="url别名",default="") 11 pid = models.ForeignKey("Permission",verbose_name="父权限",null=True,on_delete=models.CASCADE) 12 13 class Meta: 14 verbose_name_plural = '权限表' 15 verbose_name = '权限表' 16 17 def __str__(self): 18 return self.title
2.在middlewares文件中修改数据结构
1 import re 2 3 from django.utils.deprecation import MiddlewareMixin 4 from django.shortcuts import HttpResponse, redirect 5 6 from rbac.models import Permission 7 8 9 class PermissionMiddleWare(MiddlewareMixin): 10 11 def process_request(self, request): 12 print("permission_list", request.session.get("permission_list")) 13 current_path = request.path 14 15 # 设置白名单放行 16 for reg in ["/login/", "/admin/*"]: 17 ret = re.search(reg, current_path) 18 if ret: 19 return None 20 # /customers/edit/1 21 22 # 校验是否登录 23 24 user_id = request.session.get("user_id") 25 if not user_id: 26 return redirect("/login/") 27 28 # 校验权限 29 permission_list = request.session.get("permission_list") 30 31 # 路径导航列表 32 request.breadcrumb = [ 33 { 34 "title": "首页", 35 "url": "/" 36 } 37 ] 38 for item in permission_list: 39 reg = "^%s$" % item["url"] 40 ret = re.search(reg, current_path) 41 if ret: 42 show_id = item["pid"] or item["id"] 43 request.show_id = show_id 44 # 确定面包屑列表 45 ppermission = Permission.objects.filter(pk=item["pid"]).first() 46 if item["pid"]: 47 request.breadcrumb.extend([{ 48 "title": ppermission.title, 49 "url": ppermission.url 50 }, { 51 "title": item["title"], 52 "url": request.path 53 }] 54 ) 55 else: 56 request.breadcrumb.append( 57 { 58 "title":item["title"], 59 "url":item["url"] 60 } 61 ) 62 63 return None 64 65 return HttpResponse("无访问权限!")
3.登录成功之后的initial_sesson方法中修改
1 from rbac.models import Role 2 3 4 def initial_sesson(user, request): 5 """ 6 功能:将当前登录人的所有权限录入session中 7 :param user: 当前登录人 8 """ 9 # 查询当前登录人的所有权限列表 10 # 查看当前登录人的所有角色 11 # ret=Role.objects.filter(user=user) 12 permissions = Role.objects.filter(user=user).values("permissions__url", 13 "permissions__title", 14 "permissions__name", 15 "permissions__pk", 16 "permissions__pid", 17 "permissions__menu__pk", 18 "permissions__menu__title", 19 "permissions__menu__icon", 20 ).distinct() 21 22 permission_list = [] 23 permission_menu_dict = {} 24 permissions_names = [] 25 26 for item in permissions: 27 # 构建权限列表 28 permission_list.append({ 29 "url": item["permissions__url"], 30 "title": item["permissions__title"], 31 "id": item["permissions__pk"], 32 "pid": item["permissions__pid"] 33 }) 34 35 # 构建别名列表 36 permissions_names.append(item["permissions__name"]) 37 38 menu_pk = item["permissions__menu__pk"] 39 # 构建菜单权限列表 40 if menu_pk: 41 if menu_pk not in permission_menu_dict: 42 permission_menu_dict[menu_pk] = { 43 "menu_title": item["permissions__menu__title"], 44 "menu_icon": item["permissions__menu__icon"], 45 "children": [ 46 { 47 "title": item["permissions__title"], 48 "url": item["permissions__url"], 49 "pk": item["permissions__pk"] 50 } 51 ] 52 } 53 else: 54 permission_menu_dict[menu_pk]["children"].append({ 55 "title": item["permissions__title"], 56 "url": item["permissions__url"], 57 "pk": item["permissions__pk"] 58 }) 59 60 # 将当前登录人的权限列表注入session中 61 request.session["permission_list"] = permission_list 62 request.session["permissions_names"] = permissions_names 63 # 将当前登录人的菜单权限列表注入session中 64 request.session["permission_menu_dict"] = permission_menu_dict
4.在母版中的自定义标签
1 from django.template import Library 2 3 register =Library() 4 5 @register.inclusion_tag("rbac/menu.html") 6 def get_menu_styles(request): 7 permission_menu_dict = request.session.get("permission_menu_dict") 8 for val in permission_menu_dict.values(): 9 for item in val["children"]: 10 val["class"] = "hide" 11 if request.show_id == item["pk"]: 12 val["class"] = "" 13 return {"permission_menu_dict":permission_menu_dict}
5.menu.html
<div class="multi-menu"> {% for item in permission_menu_dict.values %} <div class="item"> <div class="title"><i class="{{ item.menu_icon }}"></i>{{ item.menu_title }}</div> <div class="body {{ item.class }}"> {% for foo in item.children %} <a href="{{ foo.url }}">{{ foo.title }}</a> {% endfor %} </div> </div> {% endfor %} </div>