Browse Source

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

pull/8579/head
Jiangjie.Bai 2 years ago committed by Jiangjie.Bai
parent
commit
b262643f0a
  1. 8
      apps/authentication/models.py
  2. 6
      apps/authentication/serializers/connection_token.py

8
apps/authentication/models.py

@ -114,6 +114,14 @@ class ConnectionToken(OrgModelMixin, models.JMSModel):
def is_expired(self):
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):
self.date_expired = timezone.now()
self.save()

6
apps/authentication/serializers/connection_token.py

@ -20,7 +20,8 @@ __all__ = [
class ConnectionTokenSerializer(OrgResourceModelSerializerMixin):
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:
model = ConnectionToken
@ -35,7 +36,7 @@ class ConnectionTokenSerializer(OrgResourceModelSerializerMixin):
]
read_only_fields = [
# 普通 Token 不支持指定 user
'user', 'validity',
'user', 'is_valid', 'expire_time',
'type_display', 'user_display', 'system_user_display', 'asset_display',
'application_display',
]
@ -184,6 +185,7 @@ class ConnectionTokenSecretSerializer(OrgResourceModelSerializerMixin):
domain = ConnectionTokenDomainSerializer(read_only=True)
cmd_filter_rules = ConnectionTokenCmdFilterRuleSerializer(many=True)
actions = ActionsField()
expired_at = serializers.IntegerField()
class Meta:
model = ConnectionToken

Loading…
Cancel
Save