mirror of https://github.com/jumpserver/jumpserver
fix: circle imported for perms-api
parent
3ad64e142e
commit
3ae164d7e0
|
@ -1,6 +1,6 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
#
|
#
|
||||||
from .tree import * # noqa fix circular import
|
|
||||||
from .nodes import *
|
from .nodes import *
|
||||||
from .assets import *
|
from .assets import *
|
||||||
from .accounts import *
|
from .accounts import *
|
||||||
|
from .tree import *
|
||||||
|
|
|
@ -3,13 +3,17 @@
|
||||||
from django.shortcuts import get_object_or_404
|
from django.shortcuts import get_object_or_404
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from rest_framework.request import Request
|
from rest_framework.request import Request
|
||||||
|
from django.core.cache import cache
|
||||||
|
|
||||||
from common.exceptions import JMSObjectDoesNotExist
|
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 rbac.permissions import RBACPermission
|
||||||
from users.models import User
|
from users.models import User
|
||||||
|
from perms.utils import UserPermTreeRefreshUtil
|
||||||
|
|
||||||
__all__ = ['SelfOrPKUserMixin']
|
|
||||||
|
__all__ = ['SelfOrPKUserMixin', 'RebuildTreeMixin']
|
||||||
|
|
||||||
|
|
||||||
class SelfOrPKUserMixin:
|
class SelfOrPKUserMixin:
|
||||||
|
@ -57,3 +61,33 @@ class SelfOrPKUserMixin:
|
||||||
|
|
||||||
def request_user_is_self(self):
|
def request_user_is_self(self):
|
||||||
return self.kwargs.get('user') in ['my', '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
|
||||||
|
|
|
@ -8,8 +8,7 @@ from assets.models import Node
|
||||||
from common.utils import get_logger, lazyproperty
|
from common.utils import get_logger, lazyproperty
|
||||||
from perms import serializers
|
from perms import serializers
|
||||||
from perms.utils import UserPermNodeUtil
|
from perms.utils import UserPermNodeUtil
|
||||||
from .mixin import SelfOrPKUserMixin
|
from .mixin import SelfOrPKUserMixin, RebuildTreeMixin
|
||||||
from .tree.mixin import RebuildTreeMixin
|
|
||||||
|
|
||||||
logger = get_logger(__name__)
|
logger = get_logger(__name__)
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ from rest_framework.response import Response
|
||||||
from assets.api import SerializeToTreeNodeMixin
|
from assets.api import SerializeToTreeNodeMixin
|
||||||
from assets.models import Asset
|
from assets.models import Asset
|
||||||
from common.utils import get_logger
|
from common.utils import get_logger
|
||||||
from .mixin import RebuildTreeMixin
|
from ..mixin import RebuildTreeMixin
|
||||||
from ..assets import UserAllPermedAssetsApi
|
from ..assets import UserAllPermedAssetsApi
|
||||||
|
|
||||||
logger = get_logger(__name__)
|
logger = get_logger(__name__)
|
||||||
|
|
|
@ -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
|
|
|
@ -21,8 +21,7 @@ from perms.hands import Node
|
||||||
from perms.models import PermNode
|
from perms.models import PermNode
|
||||||
from perms.utils import PermAssetDetailUtil, UserPermNodeUtil
|
from perms.utils import PermAssetDetailUtil, UserPermNodeUtil
|
||||||
from perms.utils import UserPermAssetUtil
|
from perms.utils import UserPermAssetUtil
|
||||||
from .mixin import RebuildTreeMixin
|
from ..mixin import SelfOrPKUserMixin, RebuildTreeMixin
|
||||||
from ..mixin import SelfOrPKUserMixin
|
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'UserGrantedK8sAsTreeApi',
|
'UserGrantedK8sAsTreeApi',
|
||||||
|
|
Loading…
Reference in New Issue