django中介軟體中判斷前端是不是ajax請求,從而判斷是否有許可權

龙岩龙發表於2024-09-15

` def process_view(self, request, view_func, view_args, view_kwargs):
# 許可權判斷
# 獲取當前路由的name 和 settings中的許可權表比較
url_list = []
current_name = request.resolver_match.url_name

    if current_name in NB_ROOT:
        return None
    if current_name not in NB_PERMISSIONS[request.ee["isadmin"]]:
        is_ajax = request.headers.get('X-Requested-With') == 'XMLHttpRequest' 
        if is_ajax:
            return JsonResponse({"code":403, "msg":"沒有許可權"})
        return redirect('no_permission')
    url_list.append(NB_PERMISSIONS[request.ee["isadmin"]].get(current_name).get("text"))`

相關文章