diff --git a/arkid/common/arkstore.py b/arkid/common/arkstore.py index 5fe0fafbb..7a64290da 100644 --- a/arkid/common/arkstore.py +++ b/arkid/common/arkstore.py @@ -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 {} \ No newline at end of file + 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 diff --git a/arkid/core/api.py b/arkid/core/api.py index 6f7233920..68d369206 100644 --- a/arkid/core/api.py +++ b/arkid/core/api.py @@ -107,6 +107,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 @@ -122,6 +123,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() @@ -172,6 +174,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): diff --git a/arkid/core/migrations/0032_expiringtoken_active_date.py b/arkid/core/migrations/0032_expiringtoken_active_date.py new file mode 100644 index 000000000..541951d7a --- /dev/null +++ b/arkid/core/migrations/0032_expiringtoken_active_date.py @@ -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'), + ), + ] diff --git a/arkid/core/models.py b/arkid/core/models.py index 19a6d825f..ac3834b49 100644 --- a/arkid/core/models.py +++ b/arkid/core/models.py @@ -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."""