2018-07-13 07:05:46 +00:00
|
|
|
from django.shortcuts import redirect, reverse
|
|
|
|
from django.http import HttpResponseForbidden
|
2018-07-12 16:00:35 +00:00
|
|
|
|
2018-07-13 07:05:46 +00:00
|
|
|
from django.views.generic import DetailView, View
|
2018-07-12 16:31:50 +00:00
|
|
|
|
|
|
|
from .models import Organization
|
2019-08-16 08:33:05 +00:00
|
|
|
from common.utils import UUID_PATTERN
|
2018-07-12 16:31:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SwitchOrgView(DetailView):
|
|
|
|
model = Organization
|
|
|
|
object = None
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
pk = kwargs.get('pk')
|
|
|
|
self.object = Organization.get_instance(pk)
|
2019-06-25 12:32:12 +00:00
|
|
|
oid = str(self.object.id)
|
|
|
|
request.session['oid'] = oid
|
2019-03-22 07:18:47 +00:00
|
|
|
host = request.get_host()
|
2019-09-26 02:32:39 +00:00
|
|
|
referer = request.META.get('HTTP_REFERER', '')
|
2019-08-16 08:33:05 +00:00
|
|
|
if referer.find(host) == -1:
|
|
|
|
return redirect(reverse('index'))
|
|
|
|
if UUID_PATTERN.search(referer):
|
|
|
|
return redirect(reverse('index'))
|
2019-09-12 10:56:26 +00:00
|
|
|
# 组织管理员切换到组织审计员时(403)
|
|
|
|
if not self.object.get_org_admins().filter(id=request.user.id):
|
|
|
|
return redirect(reverse('index'))
|
2019-08-16 08:33:05 +00:00
|
|
|
return redirect(referer)
|
2018-07-13 07:05:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SwitchToAOrgView(View):
|
|
|
|
def get(self, request, *args, **kwargs):
|
2019-09-12 10:56:26 +00:00
|
|
|
if request.user.is_common_user:
|
2018-07-13 07:05:46 +00:00
|
|
|
return HttpResponseForbidden()
|
2019-09-12 10:56:26 +00:00
|
|
|
admin_orgs = request.user.admin_orgs
|
|
|
|
audit_orgs = request.user.audit_orgs
|
2018-07-13 07:05:46 +00:00
|
|
|
default_org = Organization.default()
|
2019-09-12 10:56:26 +00:00
|
|
|
if admin_orgs:
|
|
|
|
if default_org in admin_orgs:
|
|
|
|
redirect_org = default_org
|
|
|
|
else:
|
|
|
|
redirect_org = admin_orgs[0]
|
|
|
|
return redirect(reverse('orgs:org-switch', kwargs={'pk': redirect_org.id}))
|
|
|
|
if audit_orgs:
|
|
|
|
if default_org in audit_orgs:
|
|
|
|
redirect_org = default_org
|
|
|
|
else:
|
|
|
|
redirect_org = audit_orgs[0]
|
|
|
|
return redirect(reverse('orgs:org-switch', kwargs={'pk': redirect_org.id}))
|