From 1051c6af04e0876314a9e3a03494f9cb25560496 Mon Sep 17 00:00:00 2001 From: Bai Date: Mon, 5 Feb 2024 16:04:48 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E5=90=8E=E4=BB=AA=E8=A1=A8=E7=9B=98=E6=98=BE?= =?UTF-8?q?=E7=A4=BA403=E7=9A=84=E9=97=AE=E9=A2=98(=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E5=9C=A8=E9=9D=9EDefault=E7=BB=84=E7=BB=87=E4=B8=8B=E6=98=AF?= =?UTF-8?q?=E7=BB=84=E7=BB=87=E7=AE=A1=E7=90=86=E5=91=98=E6=9D=83=E9=99=90?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/orgs/models.py | 3 +++ apps/orgs/utils.py | 35 +++++++++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/apps/orgs/models.py b/apps/orgs/models.py index e667ac4d7..223e41e38 100644 --- a/apps/orgs/models.py +++ b/apps/orgs/models.py @@ -173,6 +173,9 @@ class Organization(OrgRoleMixin, JMSBaseModel): def is_default(self): return str(self.id) == self.DEFAULT_ID + def is_system(self): + return str(self.id) == self.SYSTEM_ID + @property def internal(self): return str(self.id) in self.INTERNAL_IDS diff --git a/apps/orgs/utils.py b/apps/orgs/utils.py index 583bd2e7c..0efdcc2a7 100644 --- a/apps/orgs/utils.py +++ b/apps/orgs/utils.py @@ -6,6 +6,7 @@ from functools import wraps from inspect import signature from werkzeug.local import LocalProxy +from django.conf import settings from common.local import thread_local from .models import Organization @@ -14,7 +15,6 @@ from .models import Organization def get_org_from_request(request): # query中优先级最高 oid = request.GET.get("oid") - # 其次header if not oid: oid = request.META.get("HTTP_X_JMS_ORG") @@ -24,14 +24,33 @@ def get_org_from_request(request): # 其次session if not oid: oid = request.session.get("oid") + + if oid and oid.lower() == 'default': + return Organization.default() + + if oid and oid.lower() == 'root': + return Organization.root() + + if oid and oid.lower() == 'system': + return Organization.system() + + org = Organization.get_instance(oid) + + if org and org.internal: + # 内置组织直接返回 + return org + + if not settings.XPACK_ENABLED: + # 社区版用户只能使用默认组织 + return Organization.default() + + if not org and request.user.is_authenticated: + # 企业版用户优先从自己有权限的组织中获取 + org = request.user.orgs.first() + + if not org: + org = Organization.default() - if not oid: - oid = Organization.DEFAULT_ID - if oid.lower() == "default": - oid = Organization.DEFAULT_ID - elif oid.lower() == "root": - oid = Organization.ROOT_ID - org = Organization.get_instance(oid, default=Organization.default()) return org