2018-07-12 16:00:35 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
2019-06-19 08:45:14 +00:00
|
|
|
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
|
|
|
|
|
2019-06-19 08:45:14 +00:00
|
|
|
@staticmethod
|
|
|
|
def set_permed_org_if_need(request):
|
2019-06-21 09:09:18 +00:00
|
|
|
if request.path.startswith('/api'):
|
2019-06-19 08:45:14 +00:00
|
|
|
return
|
2019-09-12 10:56:26 +00:00
|
|
|
if not request.user.is_authenticated:
|
|
|
|
return
|
|
|
|
if request.user.is_common_user:
|
2019-06-19 08:45:14 +00:00
|
|
|
return
|
|
|
|
org = get_org_from_request(request)
|
|
|
|
if org.can_admin_by(request.user):
|
|
|
|
return
|
2019-09-12 10:56:26 +00:00
|
|
|
if org.can_audit_by(request.user):
|
|
|
|
return
|
|
|
|
admin_orgs = request.user.admin_orgs
|
2019-06-19 08:45:14 +00:00
|
|
|
if admin_orgs:
|
|
|
|
request.session['oid'] = str(admin_orgs[0].id)
|
2019-09-12 10:56:26 +00:00
|
|
|
return
|
|
|
|
audit_orgs = request.user.audit_orgs
|
|
|
|
if audit_orgs:
|
|
|
|
request.session['oid'] = str(audit_orgs[0].id)
|
|
|
|
return
|
2019-06-19 08:45:14 +00:00
|
|
|
|
2018-07-12 16:00:35 +00:00
|
|
|
def __call__(self, request):
|
2019-06-19 08:45:14 +00:00
|
|
|
self.set_permed_org_if_need(request)
|
2018-07-12 16:00:35 +00:00
|
|
|
org = get_org_from_request(request)
|
2020-04-13 02:40:13 +00:00
|
|
|
request.current_org = org
|
|
|
|
set_current_org(org)
|
2018-07-12 16:00:35 +00:00
|
|
|
response = self.get_response(request)
|
|
|
|
return response
|