Skip to content

Commit

Permalink
feat: 🎸 添加Token活跃日期 (#1380)
Browse files Browse the repository at this point in the history
  • Loading branch information
luolu-lg authored Oct 28, 2022
1 parent 3a15ab5 commit 1844830
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
22 changes: 21 additions & 1 deletion arkid/common/arkstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,4 +734,24 @@ def get_app_config_from_arkstore(request, arkstore_app_id):
if res['type'] == 'auto_form_fill':
app = get_arkid_saas_app_detail(tenant, token, arkstore_app_id)
return app.get('config', {}).get('config', {})
return {}
return {}


def get_admin_user_token():
from django.core.cache import cache

key = "ADMIN_USER_TOKEN"
value = refresh_admin_uesr_token
timeout = 60*30
token = cache.get_or_set(key, value, timeout=timeout)
return token


def refresh_admin_uesr_token():
from arkid.core.token import refresh_token
platform_tenant = Tenant.platform_tenant()
admin_user = User.objects.filter(
username='admin', tenant=platform_tenant
).first()
token = refresh_token(admin_user)
return token
10 changes: 10 additions & 0 deletions arkid/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def authenticate(self, request, token, app_id, app_secret):
token = ExpiringToken.objects.filter(user=request.user).first()
if not token:
token = ExpiringToken.objects.create(user=request.user, token=generate_token())
self.refresh_token_active_date(token)
tenant = request.tenant
# 获取操作id查询用户权限
operation_id = request.operation_id
Expand All @@ -125,6 +126,7 @@ def authenticate(self, request, token, app_id, app_secret):
if token:
# 使用传统的token访问
token = ExpiringToken.objects.get(token=token)
self.refresh_token_active_date(token)
if not token.user.is_active:
raise HttpError(401, _('User inactive or deleted','用户无效或被删除'))
tenant = request.tenant or Tenant.platform_tenant()
Expand Down Expand Up @@ -175,6 +177,14 @@ def authenticate(self, request, token, app_id, app_secret):
# logger.error(err)
# return

@staticmethod
def refresh_token_active_date(token):
from django.utils import timezone
local_date = timezone.localdate()
if not token.active_date or token.active_date < local_date:
token.active_date = local_date
token.save()


class ArkidApi(NinjaAPI):
def create_response(self, request, *args, **kwargs):
Expand Down
18 changes: 18 additions & 0 deletions arkid/core/migrations/0032_expiringtoken_active_date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.0.6 on 2022-10-26 13:29

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('core', '0031_alter_tenant_slug'),
]

operations = [
migrations.AddField(
model_name='expiringtoken',
name='active_date',
field=models.DateField(blank=True, default=None, null=True, verbose_name='Active Date'),
),
]
1 change: 1 addition & 0 deletions arkid/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ class Meta(object):
default=generate_token,
)
created = models.DateTimeField(_("Created", '创建时间'), auto_now=True)
active_date = models.DateField(_("Active Date", '活跃日期'), blank=True, null=True, default=None)

def expired(self, tenant):
"""Return boolean indicating token expiration."""
Expand Down

0 comments on commit 1844830

Please sign in to comment.