fix: 连接令牌添加 expire_time 和 is_valid 字段

pull/8579/head
Jiangjie.Bai 2 years ago committed by Jiangjie.Bai
parent cd119a2999
commit b262643f0a

@ -114,6 +114,14 @@ class ConnectionToken(OrgModelMixin, models.JMSModel):
def is_expired(self): def is_expired(self):
return self.date_expired < timezone.now() return self.date_expired < timezone.now()
@property
def expire_time(self):
interval = self.date_expired - timezone.now()
seconds = interval.total_seconds()
if seconds < 0:
seconds = 0
return int(seconds)
def expire(self): def expire(self):
self.date_expired = timezone.now() self.date_expired = timezone.now()
self.save() self.save()

@ -20,7 +20,8 @@ __all__ = [
class ConnectionTokenSerializer(OrgResourceModelSerializerMixin): class ConnectionTokenSerializer(OrgResourceModelSerializerMixin):
type_display = serializers.ReadOnlyField(source='get_type_display', label=_("Type display")) type_display = serializers.ReadOnlyField(source='get_type_display', label=_("Type display"))
validity = serializers.BooleanField(source='is_valid', read_only=True, label=_('Validity')) is_valid = serializers.BooleanField(read_only=True, label=_('Validity'))
expire_time = serializers.IntegerField(read_only=True, label=_('Expired time'))
class Meta: class Meta:
model = ConnectionToken model = ConnectionToken
@ -35,7 +36,7 @@ class ConnectionTokenSerializer(OrgResourceModelSerializerMixin):
] ]
read_only_fields = [ read_only_fields = [
# 普通 Token 不支持指定 user # 普通 Token 不支持指定 user
'user', 'validity', 'user', 'is_valid', 'expire_time',
'type_display', 'user_display', 'system_user_display', 'asset_display', 'type_display', 'user_display', 'system_user_display', 'asset_display',
'application_display', 'application_display',
] ]
@ -184,6 +185,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()
class Meta: class Meta:
model = ConnectionToken model = ConnectionToken

Loading…
Cancel
Save