diff --git a/api/v1/views/loginpage.py b/api/v1/views/loginpage.py index e64581477..e9c79a49d 100644 --- a/api/v1/views/loginpage.py +++ b/api/v1/views/loginpage.py @@ -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): @@ -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): @@ -70,10 +70,11 @@ 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: @@ -81,68 +82,103 @@ class Config: 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, } diff --git a/arkid/core/extension/external_idp.py b/arkid/core/extension/external_idp.py index 161df247f..f1130308d 100644 --- a/arkid/core/extension/external_idp.py +++ b/arkid/core/extension/external_idp.py @@ -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):