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: 🎸 修改第三方登录基类生成按钮的逻辑 #1029

Merged
merged 1 commit into from
Jul 14, 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
132 changes: 84 additions & 48 deletions api/v1/views/loginpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@


class ButtonRedirectSchema(Schema):
url: str = Field(title=_('URL','重定向地址'))
params : Optional[dict] = Field(title=_('params','重定向参数'))
url: str = Field(title=_('URL', '重定向地址'))
params: Optional[dict] = Field(title=_('params', '重定向参数'))


class ButtonHttpSchema(Schema):
url: str = Field(title=_('url','http请求地址'))
method: str = Field(title=_('method','http请求方法'))
params: Optional[dict] = Field(title=_('params','http请求参数'))
url: str = Field(title=_('url', 'http请求地址'))
method: str = Field(title=_('method', 'http请求方法'))
params: Optional[dict] = Field(title=_('params', 'http请求参数'))


class ButtonAgreementSchema(Schema):
Expand All @@ -31,37 +31,37 @@ class ButtonAgreementSchema(Schema):


class ButtonSchema(Schema):
prepend: Optional[str] = Field(title=_('prepend','前置文字'))
title: Optional[str] = Field(title=_('title','标签文字'))
tooltip: Optional[str] = Field(title=_('tooltip','提示文字'))
long: Optional[bool] = Field(title=_('long button','是否为长按钮'))
img: Optional[str] = Field(title=_('image url','图片地址'))
gopage: Optional[str] = Field(title=_('gopage','跳转的页面名字'))
redirect: Optional[ButtonRedirectSchema] = Field(title=_('redirect','重定向'))
http: Optional[ButtonHttpSchema] = Field(title=_('http','http请求'))
delay: Optional[int] = Field(title=_('delay','点击后延时(单位:秒)'))
agreement: Optional[ButtonAgreementSchema] = Field(title=_('agreement','隐私声明'))
prepend: Optional[str] = Field(title=_('prepend', '前置文字'))
title: Optional[str] = Field(title=_('title', '标签文字'))
tooltip: Optional[str] = Field(title=_('tooltip', '提示文字'))
long: Optional[bool] = Field(title=_('long button', '是否为长按钮'))
img: Optional[str] = Field(title=_('image url', '图片地址'))
gopage: Optional[str] = Field(title=_('gopage', '跳转的页面名字'))
redirect: Optional[ButtonRedirectSchema] = Field(title=_('redirect', '重定向'))
http: Optional[ButtonHttpSchema] = Field(title=_('http', 'http请求'))
delay: Optional[int] = Field(title=_('delay', '点击后延时(单位:秒)'))
agreement: Optional[ButtonAgreementSchema] = Field(title=_('agreement', '隐私声明'))


class LOGIN_FORM_ITEM_TYPES(str, Enum):
text = _('text', '普通文本框')
password = _('password','密码')
hidden = _('hidden','隐藏')
password = _('password', '密码')
hidden = _('hidden', '隐藏')


class LoginFormItemSchema(Schema):
value: Any
type: LOGIN_FORM_ITEM_TYPES = Field(title=_('type','种类'))
placeholder: Optional[str] = Field(title=_('placeholder','文字提示'))
name: str = Field(title=_('name','名字'))
append: Optional[ButtonSchema] = Field(title=_('append','扩展按钮'))
http: Optional[ButtonHttpSchema] = Field(title=_('http','http请求'))
type: LOGIN_FORM_ITEM_TYPES = Field(title=_('type', '种类'))
placeholder: Optional[str] = Field(title=_('placeholder', '文字提示'))
name: str = Field(title=_('name', '名字'))
append: Optional[ButtonSchema] = Field(title=_('append', '扩展按钮'))
http: Optional[ButtonHttpSchema] = Field(title=_('http', 'http请求'))


class LoginFormSchema(Schema):
label: str = Field(title=_('label', '表单名'))
items: List[LoginFormItemSchema] = Field(title=_('items', '表单项'))
submit: ButtonSchema = Field(title=_('submit','表单提交'))
submit: ButtonSchema = Field(title=_('submit', '表单提交'))


class LoginPageExtendSchema(Schema):
Expand All @@ -70,79 +70,115 @@ class LoginPageExtendSchema(Schema):


class LoginPageSchema(Schema):
name: str = Field(title=_('page name','页面名字'))
name: str = Field(title=_('page name', '页面名字'))
forms: List[LoginFormSchema] = Field(title=_('forms', '表单'))
bottoms: Optional[List[ButtonSchema]] = Field(title=_('bottoms','表单下按钮'))
extend: Optional[LoginPageExtendSchema] = Field(title=_('extend','扩展'))
bottoms: Optional[List[ButtonSchema]] = Field(title=_('bottoms', '表单下按钮'))
extend: Optional[LoginPageExtendSchema] = Field(title=_('extend', '扩展'))


class LoginPageTenantSchema(ModelSchema):
class Config:
model = Tenant
model_fields = ['id', 'name', 'slug', 'icon']
# validate = False


class LoginPageOut(Schema):
data: Dict[str, Optional[LoginPageSchema]]
tenant: LoginPageTenantSchema


@api.get("/tenant/{tenant_id}/login_page/", response=LoginPageOut,tags=['登录与注册'], auth=None)
@api.get(
"/tenant/{tenant_id}/login_page/", response=LoginPageOut, tags=['登录与注册'], auth=None
)
@operation(LoginPageOut)
def login_page(request, tenant_id: str):
tenant = request.tenant
login_pages = []
responses = dispatch_event(Event(tag=CREATE_LOGIN_PAGE_AUTH_FACTOR, tenant=tenant, request=request))
responses = dispatch_event(
Event(tag=CREATE_LOGIN_PAGE_AUTH_FACTOR, tenant=tenant, request=request)
)
for useless, response in responses:
logger.info(response)
login_pages.append(response)

dispatch_event(Event(tag=CREATE_LOGIN_PAGE_RULES, tenant=tenant, request=request, data=login_pages))

dispatch_event(
Event(
tag=CREATE_LOGIN_PAGE_RULES,
tenant=tenant,
request=request,
data=login_pages,
)
)

data = {}
for login_page, ext in login_pages:

if not login_page:
continue

for confg_data in login_page.values():
for k,v in confg_data.items():
for k, v in confg_data.items():
if not data.get(k):
data[k] = v
else:
if not data[k].get('bottoms'):
data[k]['bottoms'] = v.get('bottoms')
data[k]['bottoms'] = v.get('bottoms', [])
else:
data[k]['bottoms'].extend(v.get('bottoms',[]))
data[k]['bottoms'].extend(v.get('bottoms', []))
if not data[k].get('forms'):
data[k]['forms'] = v.get('forms')
data[k]['forms'] = v.get('forms', [])
else:
data[k]['forms'].extend(v.get('forms',[]))
data[k]['forms'].extend(v.get('forms', []))
if not data[k].get('extend'):
data[k]['extend'] = v.get('extend')
data[k]['extend'] = v.get('extend') if v.get('extend') else None
else:
data[k]['extend']['buttons'].extend(v.get('extend', {}).get('buttons', []))
data[k]['extend']['buttons'].extend(
v.get('extend', {}).get('buttons', [])
)
if not data[k].get('name'):
data[k]['name'] = k

if data.get(AuthFactorExtension.RESET_PASSWORD):
if len(data.get(AuthFactorExtension.RESET_PASSWORD)['forms']) > 0:
bottom = {"label": _("Forget Password", "忘记密码"), "gopage": AuthFactorExtension.RESET_PASSWORD}
bottom = {
"label": _("Forget Password", "忘记密码"),
"gopage": AuthFactorExtension.RESET_PASSWORD,
}
data[AuthFactorExtension.LOGIN]['bottoms'].insert(0, bottom)

bottom = {"prepend": _("Existing Account,", "已有账号,"), "label": _("Login Now","立即登录"), "gopage": AuthFactorExtension.LOGIN}

bottom = {
"prepend": _("Existing Account,", "已有账号,"),
"label": _("Login Now", "立即登录"),
"gopage": AuthFactorExtension.LOGIN,
}
data[AuthFactorExtension.RESET_PASSWORD]['bottoms'].insert(0, bottom)

if data.get(AuthFactorExtension.REGISTER):
bottom = {"prepend": _("No Account,","还没有账号,"), "label": _("Register Now","立即注册"), "gopage": AuthFactorExtension.REGISTER}
bottom = {
"prepend": _("No Account,", "还没有账号,"),
"label": _("Register Now", "立即注册"),
"gopage": AuthFactorExtension.REGISTER,
}
data[AuthFactorExtension.LOGIN]['bottoms'].insert(0, bottom)
bottom = {"prepend": _("Existing Account,", "已有账号,"), "label": _("Login Now","立即登录"), "gopage": AuthFactorExtension.LOGIN}
bottom = {
"prepend": _("Existing Account,", "已有账号,"),
"label": _("Login Now", "立即登录"),
"gopage": AuthFactorExtension.LOGIN,
}
data[AuthFactorExtension.REGISTER]['bottoms'].insert(0, bottom)

if data.get(AuthFactorExtension.REGISTER) and data.get(AuthFactorExtension.RESET_PASSWORD):
bottom = {"prepend": _("No Account,","还没有账号,"), "label": _("Register Now","立即注册"), "gopage": AuthFactorExtension.REGISTER}
if data.get(AuthFactorExtension.REGISTER) and data.get(
AuthFactorExtension.RESET_PASSWORD
):
bottom = {
"prepend": _("No Account,", "还没有账号,"),
"label": _("Register Now", "立即注册"),
"gopage": AuthFactorExtension.REGISTER,
}
data[AuthFactorExtension.RESET_PASSWORD]['bottoms'].insert(0, bottom)

return {
'tenant': tenant,
'tenant': tenant,
'data': data,
}
12 changes: 5 additions & 7 deletions arkid/core/extension/external_idp.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,18 +271,16 @@ def create_tenant_config(self, tenant, config, name, type):

def add_idp_login_buttons(self, event, **kwargs):
logger.info(f'{self.package} add idp login buttons start')
buttons = []
data = {}
configs = self.get_tenant_configs(event.tenant)
for config in configs:
img_url, redirect_url = self.get_img_and_redirect_url(config)
if img_url and redirect_url:
buttons.append({"img": img_url, "redirect": {"url": redirect_url}})
logger.info(buttons)
buttons = [{"img": img_url, "redirect": {"url": redirect_url}}]
data[config.id.hex] = {"login": {'extend': {"buttons": buttons}}}

logger.info(f'{self.package} add idp login buttions end')
if not buttons:
return {}
else:
return {"login": {'extend': {"buttons": buttons}}}
return data

@abstractmethod
def get_img_and_redirect_url(self, config):
Expand Down