您好,登錄后才能下訂單哦!
在上一篇已經(jīng)是學(xué)習(xí)如何搭建一個公共組件,可以拷貝到任何項目里面,實現(xiàn)權(quán)限的管理工作,今天再次學(xué)習(xí)下公共組件的使用
在中間件中,通過導(dǎo)入項目的setting文件,從里面導(dǎo)入變量信息,所以我們在setting里面設(shè)置了如下變量信息:
# ############################## RBAC權(quán)限相關(guān)配置開始 ##############################
# # 無需權(quán)限控制的URL
RBAC_NO_AUTH_URL = [
'/login.html',
'/index.html',
'/register.html',
'/admin.*',
'/rbac.*',
]
# session中保存權(quán)限信息的Key
RBAC_PERMISSION_SESSION_KEY = "rbac_permission_session_key"
# Http請求中傳入的參數(shù),根據(jù)其獲取GET、POST、EDIT等檢測用戶是否具有相應(yīng)權(quán)限
# 例如:
# http://www.example.com?md=get 表示獲取
# http://www.example.com?md=post 表示添加
# http://www.example.com?md=del 表示刪除
RBAC_QUERY_KEY = "md"
RBAC_DEFAULT_QUERY_VALUE = "look"
# 無權(quán)訪問時,頁面提示信息
RBAC_PERMISSION_MSG = "無權(quán)限訪問"
# Session中保存菜單和權(quán)限信息的Key
RBAC_MENU_PERMISSION_SESSION_KEY = "rbac_menu_permission_session_key"
RBAC_MENU_KEY = "rbac_menu_key"
RBAC_MENU_PERMISSION_KEY = "rbac_menu_permission_key"
# 菜單主題
RBAC_THEME = "default"
# ############################## RBAC權(quán)限相關(guān)配置結(jié)束 ##############################
程序如果使用的話,是需要先登陸的,RBAC_NO_AUTH_URL
這個里面設(shè)置了登陸的白名單。下面第一步先設(shè)置一個登陸頁面
templates
下面新建一個login.html頁面
from django.shortcuts import render,HttpResponse,redirect
from web import models
def login(request):
if request.method == "GET":
return render(request,'login.html')
else:
u = request.POST.get('username')
p = request.POST.get('password')
obj = models.UserInfo.objects.filter(user__username=u,user__password=p).first()#當(dāng)前用戶的對象這個對象里面有obj.id ,obj_nickname,obj.user_id
if obj:
#獲取當(dāng)前用戶的權(quán)限
#獲取當(dāng)前用戶的菜單
#去配置文件中獲取key,寫入session中
from rbac.service import initial_permission
initial_permission(request,obj.user_id)
#也可以自定義session
request.session['user_info'] = {'username':u,'nickname':obj.nickname,'uid':obj.id}
return redirect('/bgindex.html')
else:
return render(request,'login.html')
>上面導(dǎo)入了一個模塊,這service.py模塊中封裝了權(quán)限和菜單,其中的代碼如下:
#!/usr/bin/env python
import re
from django.conf import settings
from . import models
def initial_permission(request, user_id):
"""
初始化權(quán)限,獲取當(dāng)前用戶權(quán)限并添加到session中
將當(dāng)前用戶權(quán)限信息轉(zhuǎn)換為以下格式,并將其添加到Session中
{
'/index.html': ['GET','POST','DEL','EDIT],
'/detail-(\d+).html': ['GET','POST','DEL','EDIT],
}
:param request: 請求對象
:param user_id: 當(dāng)前用戶id
:return:
"""
"""初始化權(quán)限信息"""
roles = models.Role.objects.filter(users__user_id=user_id)
p2a = models.Permission2Action2Role.objects.filter(role__in=roles).values('permission__url',
"action__code").distinct()
user_permission_dict = {}
for item in p2a:
if item['permission__url'] in user_permission_dict:
user_permission_dict[item['permission__url']].append(item['action__code'])
else:
user_permission_dict[item['permission__url']] = [item['action__code'], ]
request.session[settings.RBAC_PERMISSION_SESSION_KEY] = user_permission_dict
"""初始化菜單信息,將菜單信息和權(quán)限信息添加到session中"""
menu_list = list(models.Menu.objects.values('id', 'caption', 'parent_id'))
menu_permission_list = list(models.Permission2Action2Role.objects.filter(role__in=roles,
permission__menu__isnull=False).values(
'permission_id',
'permission__url',
'permission__caption',
'permission__menu_id').distinct())
request.session[settings.RBAC_MENU_PERMISSION_SESSION_KEY] = {
settings.RBAC_MENU_KEY: menu_list,
settings.RBAC_MENU_PERMISSION_KEY: menu_permission_list
}
def fetch_permission_code(request, url):
"""
根據(jù)URL獲取該URL擁有的權(quán)限,如:["GET","POST"]
:param request:
:param url:
:return:
"""
user_permission_dict = request.session.get(settings.RBAC_PERMISSION_SESSION_KEY)
if not user_permission_dict:
return []
for pattern, code_list in user_permission_dict.items():
if re.match(pattern, url):
return code_list
return []
- ** bgindex**頁面搭建
bgindex是后臺的管理頁面,這個頁面是根據(jù)登陸用戶的權(quán)限來顯示對應(yīng)的內(nèi)容。這里是走中間件了,所以中間件會執(zhí)行篩查的工作,中間件中的代碼如下:
#!/usr/bin/env python
import re
from django.conf import settings
from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import HttpResponse
class RbacMiddleware(MiddlewareMixin):
def process_request(self, request, *args, **kwargs):
"""
檢查用戶是否具有權(quán)限訪問當(dāng)前URL
:param request:
:param args:
:param kwargs:
:return:
"""
"""跳過無需權(quán)限訪問的URL"""
for pattern in settings.RBAC_NO_AUTH_URL:
if re.match(pattern, request.path_info):
return None
"""獲取當(dāng)前用戶session中的權(quán)限信息"""
permission_dict = request.session.get(settings.RBAC_PERMISSION_SESSION_KEY)
if not permission_dict:
return HttpResponse(settings.RBAC_PERMISSION_MSG)
"""當(dāng)前URL和session中的權(quán)限進(jìn)行匹配"""
flag = False
for pattern, code_list in permission_dict.items():
upper_code_list = [item.upper() for item in code_list]
if re.match(pattern, request.path_info):
request_permission_code = request.GET.get(settings.RBAC_QUERY_KEY, settings.RBAC_DEFAULT_QUERY_VALUE).upper()
if request_permission_code in upper_code_list:
request.permission_code = request_permission_code
request.permission_code_list = upper_code_list
flag = True
break
if not flag:
return HttpResponse(settings.RBAC_PERMISSION_MSG)
通過中間件的篩查后,我們可以得到值,然后對值進(jìn)行判斷來操作,如下:
上面是FBV模式,我們也可以做成CBV模式如下:
dispatch是這樣的工作模式:
![](https://s1.51cto.com/images/blog/201805/17/d3b613c271249192498f222e6980b84f.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
我們自己寫一個,讓dispatch自己走咱們自己寫的規(guī)則,多繼承,會執(zhí)行前面的繼承,前面執(zhí)行了,后面的里面的dispatch就不會執(zhí)行
![](https://s1.51cto.com/images/blog/201805/17/1c5ddfdb763e7cbd45c710aa2248f5a8.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
![](https://s1.51cto.com/images/blog/201805/17/d429cda4c38f86fb3276e2ec04b266ed.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
首先在web的數(shù)據(jù)庫里面創(chuàng)建表
from django.db import models
from rbac.models import User as RbacUser
import datetime
class UserInfo(models.Model):
nickname = models.CharField(max_length=16)
user = models.OneToOneField(RbacUser,on_delete=models.CASCADE)
def __str__(self):
return self.nickname
class Order(models.Model):
"""
保障單
"""
title = models.CharField(max_length=32,verbose_name='保障標(biāo)題')
detail = models.TextField(verbose_name='保障詳細(xì)信息')
create_user = models.ForeignKey(UserInfo,related_name='aaa',on_delete=models.CASCADE)
ctime = models.DateTimeField(verbose_name='創(chuàng)建時間')
status_choice=(
(1,'未處理'),
(2,'處理中'),
(3,'已處理')
)
status = models.IntegerField(choices=status_choice,default=1)
processor = models.ForeignKey(UserInfo,related_name='bbb',null=True,blank=True,on_delete=models.CASCADE)
solution = models.TextField(null=True,blank=True)
ptime = models.DateTimeField(null=True,blank=True)
def __str__(self):
return self.title
然后在setting里面注冊web,
注冊成功后,生成數(shù)據(jù)庫表
>python3 manage.py makemigrations
>python3 manage.py migrate
通過admin 來后臺添加數(shù)據(jù)
數(shù)據(jù)的添加-------------------------------------------
下面是simple_tag 代碼:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import re
import os
from django import template
from django.utils.safestring import mark_safe
from django.conf import settings
register = template.Library()
def process_menu_tree_data(request):
"""
根據(jù)Session中獲取的菜單以及權(quán)限信息,結(jié)構(gòu)化數(shù)據(jù),生成特殊數(shù)據(jù)結(jié)構(gòu),如:
[
{id:1,caption:'菜單標(biāo)題',parent_id:None,status:False,opened:False,child:[...]},
]
PS: 最后一層的權(quán)限會有url,即:菜單跳轉(zhuǎn)的地址
:param request:
:return:
"""
menu_permission_dict = request.session.get(settings.RBAC_MENU_PERMISSION_SESSION_KEY)
if not menu_permission_dict:
raise Exception('Session中未保存當(dāng)前用戶菜單以及權(quán)限信息,請登錄后初始化權(quán)限信息!')
""" session中獲取菜單和權(quán)限信息 """
all_menu_list = menu_permission_dict[settings.RBAC_MENU_KEY]
menu_permission_list = menu_permission_dict[settings.RBAC_MENU_PERMISSION_KEY]
all_menu_dict = {}
for row in all_menu_list:
row['opened'] = False
row['status'] = False
row['child'] = []
all_menu_dict[row['id']] = row
""" 將權(quán)限信息掛靠在菜單上,并設(shè)置是否默認(rèn)打開,以及默認(rèn)顯示 """
for per in menu_permission_list:
item = {'id': per['permission_id'], 'caption': per['permission__caption'], 'url': per['permission__url'],
'parent_id': per['permission__menu_id'],
'opened': False,
'status': True}
menu_id = item['parent_id']
all_menu_dict[menu_id]['child'].append(item)
# 將當(dāng)前URL和權(quán)限正則進(jìn)行匹配,用于指示是否默認(rèn)打開菜單
if re.match(item['url'], request.path_info):
item['opened'] = True
if item['opened']:
pid = menu_id
while not all_menu_dict[pid]['opened']:
all_menu_dict[pid]['opened'] = True
pid = all_menu_dict[pid]['parent_id']
if not pid:
break
if item['status']:
pid = menu_id
while not all_menu_dict[pid]['status']:
all_menu_dict[pid]['status'] = True
pid = all_menu_dict[pid]['parent_id']
if not pid:
break
result = []
for row in all_menu_list:
pid = row['parent_id']
if pid:
all_menu_dict[pid]['child'].append(row)
else:
result.append(row)
return result
def build_menu_tree_html(menu_list):
tpl1 = """
<div class='rbac-menu-item'>
<div class='rbac-menu-header'>{0}</div>
<div class='rbac-menu-body {2}'>{1}</div>
</div>
"""
tpl2 = """
<a href='{0}' class='{1}'>{2}</a>
"""
menu_str = ""
for menu in menu_list:
if not menu['status']:
continue
if menu.get('url'):
menu_str += tpl2.format(menu['url'], "" if menu['opened'] else 'rbac-active', menu['caption'])
else:
if menu.get('child'):
child = build_menu_tree_html(menu.get('child'))
else:
child = ""
menu_str += tpl1.format(menu['caption'], child, "" if menu['opened'] else 'rbac-hide')
return menu_str
@register.simple_tag
def rbac_menu(request):
"""
根據(jù)Session中當(dāng)前用戶的菜單信息以及當(dāng)前URL生成菜單
:param request: 請求對象
:return:
"""
menu_tree_list = process_menu_tree_data(request)
return mark_safe(build_menu_tree_html(menu_tree_list))
@register.simple_tag
def rbac_css():
file_path = os.path.join('rbac', 'theme', settings.RBAC_THEME, 'rbac.css')
if os.path.exists(file_path):
return mark_safe(open(file_path, 'r', encoding='utf-8').read())
else:
raise Exception('rbac主題CSS文件不存在')
@register.simple_tag
def rbac_js():
file_path = os.path.join('rbac', 'theme', settings.RBAC_THEME, 'rbac.js')
if os.path.exists(file_path):
return mark_safe(open(file_path, 'r', encoding='utf-8').read())
else:
raise Exception('rbac主題JavaScript文件不存在')
當(dāng)點擊保障單的時候,會跳轉(zhuǎn)到新的保障單的頁面,
但是這里要帶參數(shù)url md的方法,上面代碼 我們在中間件里面的默認(rèn)值是GET,我們可以通過設(shè)置配置文件和中間件來修改默認(rèn)值
配置文件修改:
中間件修改
當(dāng)點擊了保障單后,跳轉(zhuǎn)到新的url,這里需要創(chuàng)建新的url路由。
新的頁面通過模板語言,導(dǎo)入來實現(xiàn)格式
在后臺給order表添加數(shù)據(jù)后,前端訪問會出現(xiàn)數(shù)據(jù)
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。