diff --git a/apps/perms/api/user_permission/__init__.py b/apps/perms/api/user_permission/__init__.py index 917693371..dc67ce7f2 100644 --- a/apps/perms/api/user_permission/__init__.py +++ b/apps/perms/api/user_permission/__init__.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -from .tree import * # noqa fix circular import from .nodes import * from .assets import * from .accounts import * +from .tree import * diff --git a/apps/perms/api/user_permission/mixin.py b/apps/perms/api/user_permission/mixin.py index 24ed7fbc1..fad2dfb38 100644 --- a/apps/perms/api/user_permission/mixin.py +++ b/apps/perms/api/user_permission/mixin.py @@ -3,13 +3,17 @@ from django.shortcuts import get_object_or_404 from django.utils.translation import gettext_lazy as _ from rest_framework.request import Request +from django.core.cache import cache from common.exceptions import JMSObjectDoesNotExist -from common.utils import is_uuid +from common.utils.http import is_true +from common.utils import lazyproperty, is_uuid from rbac.permissions import RBACPermission from users.models import User +from perms.utils import UserPermTreeRefreshUtil -__all__ = ['SelfOrPKUserMixin'] + +__all__ = ['SelfOrPKUserMixin', 'RebuildTreeMixin'] class SelfOrPKUserMixin: @@ -57,3 +61,33 @@ class SelfOrPKUserMixin: def request_user_is_self(self): return self.kwargs.get('user') in ['my', 'self'] + + +class RebuildTreeMixin: + user: User + request: Request + + def get(self, request, *args, **kwargs): + UserPermTreeRefreshUtil(self.user).refresh_if_need(force=self.is_force_refresh_tree) + return super().get(request, *args, **kwargs) + + @lazyproperty + def is_force_refresh_tree(self): + force = is_true(self.request.query_params.get('rebuild_tree')) + if not force: + force = self.compute_is_force_refresh() + return force + + def compute_is_force_refresh(self): + """ 5s 内连续刷新三次转为强制刷新 """ + force_timeout = 5 + force_max_count = 3 + force_cache_key = '{user_id}:{path}'.format(user_id=self.user.id, path=self.request.path) + count = cache.get(force_cache_key, 1) + if count >= force_max_count: + force = True + cache.delete(force_cache_key) + else: + force = False + cache.set(force_cache_key, count + 1, force_timeout) + return force diff --git a/apps/perms/api/user_permission/nodes.py b/apps/perms/api/user_permission/nodes.py index ccd6afbbd..ca1c2cfbd 100644 --- a/apps/perms/api/user_permission/nodes.py +++ b/apps/perms/api/user_permission/nodes.py @@ -8,8 +8,7 @@ from assets.models import Node from common.utils import get_logger, lazyproperty from perms import serializers from perms.utils import UserPermNodeUtil -from .mixin import SelfOrPKUserMixin -from .tree.mixin import RebuildTreeMixin +from .mixin import SelfOrPKUserMixin, RebuildTreeMixin logger = get_logger(__name__) diff --git a/apps/perms/api/user_permission/tree/asset.py b/apps/perms/api/user_permission/tree/asset.py index ee0cb72ff..a20c131f9 100644 --- a/apps/perms/api/user_permission/tree/asset.py +++ b/apps/perms/api/user_permission/tree/asset.py @@ -4,7 +4,7 @@ from rest_framework.response import Response from assets.api import SerializeToTreeNodeMixin from assets.models import Asset from common.utils import get_logger -from .mixin import RebuildTreeMixin +from ..mixin import RebuildTreeMixin from ..assets import UserAllPermedAssetsApi logger = get_logger(__name__) diff --git a/apps/perms/api/user_permission/tree/mixin.py b/apps/perms/api/user_permission/tree/mixin.py deleted file mode 100644 index 46714ce05..000000000 --- a/apps/perms/api/user_permission/tree/mixin.py +++ /dev/null @@ -1,40 +0,0 @@ -from django.core.cache import cache - -from rest_framework.request import Request - -from common.utils.http import is_true -from common.utils import lazyproperty -from perms.utils import UserPermTreeRefreshUtil -from users.models import User - -__all__ = ['RebuildTreeMixin'] - - -class RebuildTreeMixin: - user: User - request: Request - - def get(self, request, *args, **kwargs): - UserPermTreeRefreshUtil(self.user).refresh_if_need(force=self.is_force_refresh_tree) - return super().get(request, *args, **kwargs) - - @lazyproperty - def is_force_refresh_tree(self): - force = is_true(self.request.query_params.get('rebuild_tree')) - if not force: - force = self.compute_is_force_refresh() - return force - - def compute_is_force_refresh(self): - """ 5s 内连续刷新三次转为强制刷新 """ - force_timeout = 5 - force_max_count = 3 - force_cache_key = '{user_id}:{path}'.format(user_id=self.user.id, path=self.request.path) - count = cache.get(force_cache_key, 1) - if count >= force_max_count: - force = True - cache.delete(force_cache_key) - else: - force = False - cache.set(force_cache_key, count + 1, force_timeout) - return force diff --git a/apps/perms/api/user_permission/tree/node_with_asset.py b/apps/perms/api/user_permission/tree/node_with_asset.py index 114c5475e..6ea22fc69 100644 --- a/apps/perms/api/user_permission/tree/node_with_asset.py +++ b/apps/perms/api/user_permission/tree/node_with_asset.py @@ -21,8 +21,7 @@ from perms.hands import Node from perms.models import PermNode from perms.utils import PermAssetDetailUtil, UserPermNodeUtil from perms.utils import UserPermAssetUtil -from .mixin import RebuildTreeMixin -from ..mixin import SelfOrPKUserMixin +from ..mixin import SelfOrPKUserMixin, RebuildTreeMixin __all__ = [ 'UserGrantedK8sAsTreeApi',