perf: 优化 Connection Token API 逻辑处理

pull/9029/head
Jiangjie.Bai 2022-11-08 14:30:07 +08:00
parent df14d01859
commit cd93de4c00
5 changed files with 11 additions and 11 deletions

View File

@ -201,7 +201,7 @@ class CommandFilterRule(OrgModelMixin):
q |= Q(user_groups__in=set(user_groups)) q |= Q(user_groups__in=set(user_groups))
if account: if account:
org_id = account.org_id org_id = account.org_id
q |= Q(accounts__contains=list(account)) |\ q |= Q(accounts__contains=account.username) | \
Q(accounts__contains=SpecialAccount.ALL.value) Q(accounts__contains=SpecialAccount.ALL.value)
if asset: if asset:
org_id = asset.org_id org_id = asset.org_id

View File

@ -178,8 +178,6 @@ class ExtraActionApiMixin(RDPFileClientProtocolURLMixin):
get_object: callable get_object: callable
get_serializer: callable get_serializer: callable
perform_create: callable perform_create: callable
check_token_permission: callable
create_connection_token: callable
@action(methods=['POST'], detail=False, url_path='secret-info/detail') @action(methods=['POST'], detail=False, url_path='secret-info/detail')
def get_secret_detail(self, request, *args, **kwargs): def get_secret_detail(self, request, *args, **kwargs):
@ -277,10 +275,10 @@ class ConnectionTokenViewSet(ExtraActionApiMixin, RootOrgViewMixin, JMSModelView
from perms.utils.account import PermAccountUtil from perms.utils.account import PermAccountUtil
actions, expire_at = PermAccountUtil().validate_permission(user, asset, account_username) actions, expire_at = PermAccountUtil().validate_permission(user, asset, account_username)
if not actions: if not actions:
error = '' error = 'No actions'
raise PermissionDenied(error) raise PermissionDenied(error)
if expire_at < time.time(): if expire_at < time.time():
error = '' error = 'Expired'
raise PermissionDenied(error) raise PermissionDenied(error)

View File

@ -85,7 +85,7 @@ class ConnectionToken(OrgModelMixin, JMSBaseModel):
is_valid = False is_valid = False
error = _('No user or invalid user') error = _('No user or invalid user')
return is_valid, error return is_valid, error
if not self.asset or self.asset.is_active: if not self.asset or not self.asset.is_active:
is_valid = False is_valid = False
error = _('No asset or inactive asset') error = _('No asset or inactive asset')
return is_valid, error return is_valid, error

View File

@ -159,7 +159,7 @@ class ConnectionTokenSecretSerializer(OrgResourceModelSerializerMixin):
domain = ConnectionTokenDomainSerializer(read_only=True) domain = ConnectionTokenDomainSerializer(read_only=True)
cmd_filter_rules = ConnectionTokenCmdFilterRuleSerializer(many=True) cmd_filter_rules = ConnectionTokenCmdFilterRuleSerializer(many=True)
actions = ActionsField() actions = ActionsField()
expired_at = serializers.IntegerField() expire_at = serializers.IntegerField()
class Meta: class Meta:
model = ConnectionToken model = ConnectionToken
@ -167,5 +167,5 @@ class ConnectionTokenSecretSerializer(OrgResourceModelSerializerMixin):
'id', 'secret', 'id', 'secret',
'user', 'asset', 'account_username', 'account', 'protocol', 'user', 'asset', 'account_username', 'account', 'protocol',
'domain', 'gateway', 'cmd_filter_rules', 'domain', 'gateway', 'cmd_filter_rules',
'actions', 'expired_at', 'actions', 'expire_at',
] ]

View File

@ -53,7 +53,9 @@ class PermAccountUtil(AssetPermissionUtil):
user, asset, with_actions=True, with_perms=True user, asset, with_actions=True, with_perms=True
) )
perm = perms.first() perm = perms.first()
account = accounts.filter(username=account_username).first() actions = []
actions = account.actions if account else [] for account in accounts:
expire_at = perm.date_expired if perm else time.time() if account.username == account_username:
actions = account.actions
expire_at = perm.date_expired.timestamp() if perm else time.time()
return actions, expire_at return actions, expire_at