Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 🎸 添加Token活跃日期 #1380

Merged
merged 1 commit into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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):
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