From 583fd410f5a126701221cedd06bd5aeec3c9d331 Mon Sep 17 00:00:00 2001 From: BaiJiangJie Date: Fri, 21 Jun 2019 13:34:20 +0800 Subject: [PATCH 1/2] =?UTF-8?q?[Update]=20=E6=B7=BB=E5=8A=A0=E8=BF=81?= =?UTF-8?q?=E7=A7=BB=E6=96=87=E4=BB=B6=EF=BC=8C=E4=BF=AE=E6=94=B9=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E8=B5=84=E4=BA=A7=E5=8F=AF=E8=BF=9E=E6=8E=A5=E6=80=A7?= =?UTF-8?q?=E6=97=B6=E7=AE=A1=E7=90=86=E7=94=A8=E6=88=B7=E4=B8=BANone?= =?UTF-8?q?=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../migrations/0031_auto_20190621_1332.py | 53 +++++++++++++++++++ apps/assets/models/asset.py | 2 + 2 files changed, 55 insertions(+) create mode 100644 apps/assets/migrations/0031_auto_20190621_1332.py diff --git a/apps/assets/migrations/0031_auto_20190621_1332.py b/apps/assets/migrations/0031_auto_20190621_1332.py new file mode 100644 index 000000000..c2e614cbc --- /dev/null +++ b/apps/assets/migrations/0031_auto_20190621_1332.py @@ -0,0 +1,53 @@ +# Generated by Django 2.1.7 on 2019-06-21 05:32 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('assets', '0030_auto_20190619_1135'), + ] + + operations = [ + migrations.AlterField( + model_name='adminuser', + name='date_created', + field=models.DateTimeField(auto_now_add=True, verbose_name='Date created'), + ), + migrations.AlterField( + model_name='adminuser', + name='date_updated', + field=models.DateTimeField(auto_now=True, verbose_name='Date updated'), + ), + migrations.AlterField( + model_name='authbook', + name='date_created', + field=models.DateTimeField(auto_now_add=True, verbose_name='Date created'), + ), + migrations.AlterField( + model_name='authbook', + name='date_updated', + field=models.DateTimeField(auto_now=True, verbose_name='Date updated'), + ), + migrations.AlterField( + model_name='gateway', + name='date_created', + field=models.DateTimeField(auto_now_add=True, verbose_name='Date created'), + ), + migrations.AlterField( + model_name='gateway', + name='date_updated', + field=models.DateTimeField(auto_now=True, verbose_name='Date updated'), + ), + migrations.AlterField( + model_name='systemuser', + name='date_created', + field=models.DateTimeField(auto_now_add=True, verbose_name='Date created'), + ), + migrations.AlterField( + model_name='systemuser', + name='date_updated', + field=models.DateTimeField(auto_now=True, verbose_name='Date updated'), + ), + ] diff --git a/apps/assets/models/asset.py b/apps/assets/models/asset.py index 3b9fef85e..cb094dfeb 100644 --- a/apps/assets/models/asset.py +++ b/apps/assets/models/asset.py @@ -256,6 +256,8 @@ class Asset(OrgModelMixin): @connectivity.setter def connectivity(self, value): + if not self.admin_user: + return self.admin_user.set_connectivity_of(self, value) def get_auth_info(self): From ed8ae300aeb073f43b955dee2056add8ee2fe0ce Mon Sep 17 00:00:00 2001 From: BaiJiangJie <32935519+BaiJiangJie@users.noreply.github.com> Date: Fri, 21 Jun 2019 17:09:18 +0800 Subject: [PATCH 2/2] =?UTF-8?q?[Update]=20=E4=BF=AE=E6=94=B9org.middleware?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=88=87=E6=8D=A2=E7=BB=84=E7=BB=87=E7=9A=84?= =?UTF-8?q?bug=20(#2829)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [Update] 修改org.middleware自动切换组织的bug * [Update] 将切换组织逻辑移动到PermsUtil中 * [Update] 修改首页组织名称显示来源 --- apps/orgs/context_processor.py | 4 ++-- apps/orgs/middleware.py | 2 +- apps/perms/api/user_permission.py | 18 ++++-------------- apps/perms/utils/asset_permission.py | 6 ++++++ apps/perms/utils/remote_app_permission.py | 6 ++++++ 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/apps/orgs/context_processor.py b/apps/orgs/context_processor.py index 8a9111d05..493031bc0 100644 --- a/apps/orgs/context_processor.py +++ b/apps/orgs/context_processor.py @@ -1,14 +1,14 @@ # -*- coding: utf-8 -*- # -from .utils import current_org, get_current_org +from .utils import current_org, get_org_from_request from .models import Organization def org_processor(request): context = { 'ADMIN_ORGS': Organization.get_user_admin_orgs(request.user), - 'CURRENT_ORG': get_current_org(), + 'CURRENT_ORG': get_org_from_request(request), 'HAS_ORG_PERM': current_org.can_admin_by(request.user), } return context diff --git a/apps/orgs/middleware.py b/apps/orgs/middleware.py index 5338735f3..cda25bedc 100644 --- a/apps/orgs/middleware.py +++ b/apps/orgs/middleware.py @@ -11,7 +11,7 @@ class OrgMiddleware: @staticmethod def set_permed_org_if_need(request): - if request.content_type != "text/plain": + if request.path.startswith('/api'): return if not (request.user.is_authenticated and request.user.is_org_admin): return diff --git a/apps/perms/api/user_permission.py b/apps/perms/api/user_permission.py index 26f3a6c52..152e17936 100644 --- a/apps/perms/api/user_permission.py +++ b/apps/perms/api/user_permission.py @@ -26,7 +26,7 @@ from ..hands import ( ) from .. import serializers, const from ..mixins import ( - AssetsFilterMixin, RemoteAppFilterMixin, ChangeOrgIfNeedMixin + AssetsFilterMixin, RemoteAppFilterMixin ) from ..models import Action @@ -48,14 +48,6 @@ class UserPermissionCacheMixin: CACHE_TIME = settings.ASSETS_PERM_CACHE_TIME _object = None - @staticmethod - def change_org_if_need(request, kwargs): - if request.user.is_authenticated and \ - request.user.is_superuser or \ - request.user.is_app or \ - kwargs.get('pk') is None: - set_to_root_org() - def get_object(self): return None @@ -115,7 +107,6 @@ class UserPermissionCacheMixin: cache.set(key, response.data, self.CACHE_TIME) def get(self, request, *args, **kwargs): - self.change_org_if_need(request, kwargs) self.cache_policy = request.GET.get('cache_policy', '0') obj = self._get_object() @@ -461,7 +452,7 @@ class GetUserAssetPermissionActionsApi(UserPermissionCacheMixin, APIView): # RemoteApp permission -class UserGrantedRemoteAppsApi(ChangeOrgIfNeedMixin, RemoteAppFilterMixin, ListAPIView): +class UserGrantedRemoteAppsApi(RemoteAppFilterMixin, ListAPIView): permission_classes = (IsOrgAdminOrAppUser,) serializer_class = RemoteAppSerializer pagination_class = LimitOffsetPagination @@ -486,7 +477,7 @@ class UserGrantedRemoteAppsApi(ChangeOrgIfNeedMixin, RemoteAppFilterMixin, ListA return super().get_permissions() -class UserGrantedRemoteAppsAsTreeApi(ChangeOrgIfNeedMixin, ListAPIView): +class UserGrantedRemoteAppsAsTreeApi(ListAPIView): serializer_class = TreeNodeSerializer permission_classes = (IsOrgAdminOrAppUser,) @@ -518,11 +509,10 @@ class UserGrantedRemoteAppsAsTreeApi(ChangeOrgIfNeedMixin, ListAPIView): return super().get_permissions() -class ValidateUserRemoteAppPermissionApi(ChangeOrgIfNeedMixin, APIView): +class ValidateUserRemoteAppPermissionApi(APIView): permission_classes = (IsOrgAdminOrAppUser,) def get(self, request, *args, **kwargs): - self.change_org_if_need(request, kwargs) user_id = request.query_params.get('user_id', '') remote_app_id = request.query_params.get('remote_app_id', '') user = get_object_or_404(User, id=user_id) diff --git a/apps/perms/utils/asset_permission.py b/apps/perms/utils/asset_permission.py index 924f36675..4fbb136d7 100644 --- a/apps/perms/utils/asset_permission.py +++ b/apps/perms/utils/asset_permission.py @@ -11,6 +11,7 @@ from django.core.cache import cache from django.conf import settings from django.utils.translation import ugettext as _ +from orgs.utils import set_to_root_org from common.utils import get_logger from common.tree import TreeNode from .. import const @@ -162,6 +163,11 @@ class AssetPermissionUtil: self._filter_id = 'None' # 当通过filter更改 permission是标记 self.cache_policy = cache_policy self.tree = GenerateTree() + self.change_org_if_need() + + @staticmethod + def change_org_if_need(): + set_to_root_org() @classmethod def is_not_using_cache(cls, cache_policy): diff --git a/apps/perms/utils/remote_app_permission.py b/apps/perms/utils/remote_app_permission.py index 0f67e32dc..a612b9ffb 100644 --- a/apps/perms/utils/remote_app_permission.py +++ b/apps/perms/utils/remote_app_permission.py @@ -4,6 +4,7 @@ from django.db.models import Q from common.tree import TreeNode +from orgs.utils import set_to_root_org from ..models import RemoteAppPermission @@ -38,6 +39,11 @@ class RemoteAppPermissionUtil: def __init__(self, obj): self.object = obj + self.change_org_if_need() + + @staticmethod + def change_org_if_need(): + set_to_root_org() @property def permissions(self):