jumpserver/apps/orgs/middleware.py

32 lines
951 B
Python
Raw Normal View History

2018-07-12 16:00:35 +00:00
# -*- coding: utf-8 -*-
#
from .models import Organization
2018-07-13 07:05:46 +00:00
from .utils import get_org_from_request, set_current_org
2018-07-12 16:00:35 +00:00
class OrgMiddleware:
def __init__(self, get_response):
self.get_response = get_response
@staticmethod
def set_permed_org_if_need(request):
if request.path.startswith('/api'):
return
if not (request.user.is_authenticated and request.user.is_org_admin):
return
org = get_org_from_request(request)
if org.can_admin_by(request.user):
return
admin_orgs = Organization.get_user_admin_orgs(request.user)
if admin_orgs:
request.session['oid'] = str(admin_orgs[0].id)
2018-07-12 16:00:35 +00:00
def __call__(self, request):
self.set_permed_org_if_need(request)
2018-07-12 16:00:35 +00:00
org = get_org_from_request(request)
request.current_org = org
2018-07-13 07:05:46 +00:00
set_current_org(org)
2018-07-12 16:00:35 +00:00
response = self.get_response(request)
return response