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

登录默认路径 #1346

Merged
merged 4 commits into from
Oct 13, 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
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,4 @@ arkid_celery_tasks
.devcontainer/
storage/
results.sqlite

tasks
~
tasks/
4 changes: 3 additions & 1 deletion api/v1/schema/mine.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ class MessageSenderItemOut(Schema):
)

class MessageSenderOut(ResponseSchema):
data:List[MessageSenderItemOut]
data:List[MessageSenderItemOut] =Field(
default=[]
)

class MineMessageListItemOut(ModelSchema):
class Config:
Expand Down
35 changes: 29 additions & 6 deletions arkid/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
from django.utils.translation import gettext_lazy as _
from random import SystemRandom

CHARS_COMMON = ('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
CHARS_COMMON = (
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')


def deep_merge(*dicts: List[Dict], update=False) -> Dict:
"""
Expand Down Expand Up @@ -90,7 +92,8 @@ def i18n_mobile_verify(mobile, state_code, state=None):
from inventory.models import I18NMobileConfig

# 验证区号是否匹配
config = I18NMobileConfig.valid_objects.filter(state_code=state_code).first()
config = I18NMobileConfig.valid_objects.filter(
state_code=state_code).first()
if not config: # 未匹配任何配置
return False
if not config.is_active: # 配置未启用
Expand Down Expand Up @@ -239,13 +242,14 @@ def verify_token(request):

return token.user


def data_to_simplenamespace(data):
if isinstance(data, Schema):
data = data.dict()
elif isinstance(data, Model):
data = model_to_dict(data)
elif isinstance(data,dict):
for k,v in data.items():
elif isinstance(data, dict):
for k, v in data.items():
data[k] = data_to_simplenamespace(v)
else:
return data
Expand All @@ -257,10 +261,29 @@ def generate_secret(chars=None, length=128):
生成指定位数的字符串
'''
if chars is None:
chars= CHARS_COMMON
chars = CHARS_COMMON
rand = SystemRandom()
return ''.join(rand.choice(chars) for x in range(length))


def generate_md5_secret(secret):
import hashlib
return hashlib.md5(secret.encode('utf-8')).hexdigest()
return hashlib.md5(secret.encode('utf-8')).hexdigest()

def get_remote_addr_from_xforward(request):
remote_addr = request.headers.get("X-Forwarded-For", None)
if isinstance(remote_addr,str):
remote_addr = remote_addr.split(",")[0]
return remote_addr

def get_remote_addr(request):

remote_addr = get_remote_addr_from_xforward(request) or request.headers.get(
"X-Real-IP", None) or request.headers.get(
"Proxy-Client-IP", None) or request.headers.get(
"Proxy-Client-IP", None) or request.headers.get(
"WL-Proxy-Client-IP", None) or request.headers.get(
"HTTP_CLIENT_IP", None) or request.headers.get(
"HTTP_X_FORWARDED_FOR", None) or request.META.get(
"REMOTE_ADDR")
return remote_addr
18 changes: 9 additions & 9 deletions arkid/core/expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,16 @@ def save(self, *args, **kwargs):

extension_tables = {}
for table, field,extension_name,extension_model_cls,extension_table,extension_field in field_expands:
# if hasattr(self, field):
if extension_table not in extension_tables:
extension_model_obj = getattr(self, extension_table, None) # 判断是否已经关联OneToOne对象
if not extension_model_obj:
extension_model_obj = extension_model_cls()
extension_model_obj.target = self
extension_tables[extension_table] = extension_model_obj
else:
extension_model_obj = extension_tables[extension_table]
if hasattr(self, field):
if extension_table not in extension_tables:
extension_model_obj = getattr(self, extension_table, None) # 判断是否已经关联OneToOne对象
if not extension_model_obj:
extension_model_obj = extension_model_cls()
extension_model_obj.target = self
extension_tables[extension_table] = extension_model_obj
else:
extension_model_obj = extension_tables[extension_table]

setattr(extension_model_obj, extension_field, getattr(self, field))

for extension_model_obj in extension_tables.values():
Expand Down
5 changes: 3 additions & 2 deletions extension_root/com_longgui_auth_rule_retry_times/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Optional
import uuid
from api.v1.views.loginpage import login_page
from arkid.common.utils import get_remote_addr
from arkid.core import actions, pages
from arkid.core.event import AUTH_FAIL, Event, dispatch_event,BEFORE_AUTH
from arkid.core.extension import create_extension_schema
Expand Down Expand Up @@ -104,7 +105,7 @@ def auth_fail(self, event, **kwargs):
# 检查是否存在满足条件的配置
for config in self.get_tenant_configs(event.tenant):
if uuid.UUID(config.config["main_auth_factor"]["id"]).hex == event.data["auth_factor_config_id"]:
host = event.request.META.get("REMOTE_ADDR")
host = get_remote_addr(event.request)
key = self.gen_key(host,config.id.hex)
try_times = int(cache.get(event.tenant,key) or 0)
cache.set(event.tenant,key,try_times+1,expired=config.config.get("expired",30)*60)
Expand All @@ -117,7 +118,7 @@ def auth_fail(self, event, **kwargs):
def check_rule(self, event, config):
login_pages = event.data

if self.check_retry_times(event.tenant,event.request.META.get("REMOTE_ADDR"),config.id.hex,config.config.get("try_times",0)):
if self.check_retry_times(event.tenant,get_remote_addr(event.request),config.id.hex,config.config.get("try_times",0)):
dispatch_event(
Event(
core_event.AUTHRULE_FIX_LOGIN_PAGE,
Expand Down
15 changes: 9 additions & 6 deletions extension_root/com_longgui_default_desktop/__init__.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
from arkid.core import extension
from arkid.core import extension
from arkid.core.translation import gettext_default as _
from .models import DefaultDesktop
from typing import List, Optional
from pydantic import Field


class DefaultDesktopExtension(extension.Extension):

def load(self):
super().load()
self.register_extend_field(DefaultDesktop, 'default_desktop')
from api.v1.schema.tenant import TenantConfigItemOut,TenantConfigUpdateIn,TenantItemOut,DefaultTenantItemOut
from api.v1.schema.tenant import TenantConfigItemOut, TenantConfigUpdateIn, TenantItemOut, DefaultTenantItemOut
from api.v1.views.loginpage import LoginPageTenantSchema
self.register_extend_api(
TenantConfigItemOut,
TenantConfigUpdateIn,
TenantItemOut,
DefaultTenantItemOut,
LoginPageTenantSchema,
default_desktop=(Optional[str],Field(title=_("默认桌面路径")))
default_desktop=(Optional[str], Field(
title=_("默认桌面路径")))
)

extension = DefaultDesktopExtension()


extension = DefaultDesktopExtension()
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.13 on 2022-10-13 09:51

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('com_longgui_default_desktop', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='defaultdesktop',
name='default_desktop',
field=models.CharField(blank=True, default='/desktop/', max_length=1024, null=True, verbose_name='Default Desktop'),
),
]
3 changes: 2 additions & 1 deletion extension_root/com_longgui_default_desktop/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from email.policy import default
from django.db import models
from arkid.core.translation import gettext_default as _
from django.apps import AppConfig
Expand All @@ -12,6 +13,6 @@ class DefaultDesktop(TenantExpandAbstract):
class Meta:
app_label = app_label

default_desktop = models.CharField(verbose_name=_('Default Desktop', '默认桌面'),blank=True,null=True, max_length=1024)
default_desktop = models.CharField(verbose_name=_('Default Desktop', '默认桌面'),blank=True,null=True, max_length=1024,default="/desktop/")