diff --git a/apps/authentication/views/base.py b/apps/authentication/views/base.py index e93951e00..825c37619 100644 --- a/apps/authentication/views/base.py +++ b/apps/authentication/views/base.py @@ -2,6 +2,7 @@ from functools import lru_cache from rest_framework.request import Request from django.utils.translation import ugettext_lazy as _ +from django.utils.module_loading import import_string from django.conf import settings from django.db.utils import IntegrityError from django.views import View @@ -20,18 +21,16 @@ logger = get_logger(__file__) class BaseLoginCallbackView(AuthMixin, FlashMessageMixin, View): - def __init__(self): - super().__init__() - self.client_type = None - self.client_auth_params = {} - self.user_type = '' - self.auth_backend = None - self.create_user_if_not_exist_setting = '' - # 提示信息 - self.msg_client_err = _('Error') - self.msg_user_not_bound_err = _('Error') - self.msg_user_need_bound_warning = _('Error') - self.msg_not_found_user_from_client_err = _('Error') + client_type_path = '' + client_auth_params = {} + user_type = '' + auth_backend = None + create_user_if_not_exist_setting = '' + # 提示信息 + msg_client_err = _('Error') + msg_user_not_bound_err = _('Error') + msg_user_need_bound_warning = _('Error') + msg_not_found_user_from_client_err = _('Error') def verify_state(self): raise NotImplementedError @@ -42,10 +41,11 @@ class BaseLoginCallbackView(AuthMixin, FlashMessageMixin, View): @property @lru_cache(maxsize=1) def client(self): - if self.client_type is None or not self.client_auth_params: + if not all([self.client_type_path, self.client_auth_params]): raise NotImplementedError client_init = {k: getattr(settings, v) for k, v in self.client_auth_params.items()} - return self.client_type(**client_init) + client_type = import_string(self.client_type_path) + return client_type(**client_init) def create_user_if_not_exist(self, user_id, **kwargs): user = None diff --git a/apps/authentication/views/dingtalk.py b/apps/authentication/views/dingtalk.py index 34086e040..a49d2a38f 100644 --- a/apps/authentication/views/dingtalk.py +++ b/apps/authentication/views/dingtalk.py @@ -198,18 +198,19 @@ class DingTalkQRLoginView(DingTalkQRMixin, METAMixin, View): class DingTalkQRLoginCallbackView(DingTalkQRMixin, BaseLoginCallbackView): permission_classes = (AllowAny,) - def __init__(self): - super(DingTalkQRLoginCallbackView, self).__init__() - self.client_type = DingTalk - self.client_auth_params = {'appid': 'DINGTALK_APPKEY', 'appsecret': 'DINGTALK_APPSECRET', 'agentid': 'DINGTALK_AGENTID'} - self.user_type = 'dingtalk' - self.auth_backend = 'AUTH_BACKEND_DINGTALK' - self.create_user_if_not_exist_setting = 'DINGTALK_CREATE_USER_IF_NOT_EXIST' + client_type_path = 'common.sdk.im.dingtalk.DingTalk' + client_auth_params = { + 'appid': 'DINGTALK_APPKEY', 'appsecret': 'DINGTALK_APPSECRET', + 'agentid': 'DINGTALK_AGENTID' + } + user_type = 'dingtalk' + auth_backend = 'AUTH_BACKEND_DINGTALK' + create_user_if_not_exist_setting = 'DINGTALK_CREATE_USER_IF_NOT_EXIST' - self.msg_client_err = _('DingTalk Error') - self.msg_user_not_bound_err = _('DingTalk is not bound') - self.msg_user_need_bound_warning = _('Please login with a password and then bind the DingTalk') - self.msg_not_found_user_from_client_err = _('Failed to get user from DingTalk') + msg_client_err = _('DingTalk Error') + msg_user_not_bound_err = _('DingTalk is not bound') + msg_user_need_bound_warning = _('Please login with a password and then bind the DingTalk') + msg_not_found_user_from_client_err = _('Failed to get user from DingTalk') class DingTalkOAuthLoginView(DingTalkOAuthMixin, View): diff --git a/apps/authentication/views/feishu.py b/apps/authentication/views/feishu.py index 7c94e0269..b4c16fb53 100644 --- a/apps/authentication/views/feishu.py +++ b/apps/authentication/views/feishu.py @@ -158,15 +158,14 @@ class FeiShuQRLoginView(FeiShuQRMixin, View): class FeiShuQRLoginCallbackView(FeiShuQRMixin, BaseLoginCallbackView): permission_classes = (AllowAny,) - def __init__(self): - super(FeiShuQRLoginCallbackView, self).__init__() - self.client_type = FeiShu - self.client_auth_params = {'app_id': 'FEISHU_APP_ID', 'app_secret': 'FEISHU_APP_SECRET'} - self.user_type = 'feishu' - self.auth_backend = 'AUTH_BACKEND_FEISHU' - self.create_user_if_not_exist_setting = 'FEISHU_CREATE_USER_IF_NOT_EXIST' + client_type_path = 'common.sdk.im.feishu.FeiShu' + client_auth_params = {'app_id': 'FEISHU_APP_ID', 'app_secret': 'FEISHU_APP_SECRET'} + user_type = 'feishu' + auth_backend = 'AUTH_BACKEND_FEISHU' + create_user_if_not_exist_setting = 'FEISHU_CREATE_USER_IF_NOT_EXIST' + + msg_client_err = _('FeiShu Error') + msg_user_not_bound_err = _('FeiShu is not bound') + msg_user_need_bound_warning = _('Please login with a password and then bind the FeiShu') + msg_not_found_user_from_client_err = _('Failed to get user from FeiShu') - self.msg_client_err = _('FeiShu Error') - self.msg_user_not_bound_err = _('FeiShu is not bound') - self.msg_user_need_bound_warning = _('Please login with a password and then bind the FeiShu') - self.msg_not_found_user_from_client_err = _('Failed to get user from FeiShu') diff --git a/apps/authentication/views/wecom.py b/apps/authentication/views/wecom.py index 1b1a8a3d0..b2061a4d6 100644 --- a/apps/authentication/views/wecom.py +++ b/apps/authentication/views/wecom.py @@ -193,18 +193,16 @@ class WeComQRLoginView(WeComQRMixin, METAMixin, View): class WeComQRLoginCallbackView(WeComQRMixin, BaseLoginCallbackView): permission_classes = (AllowAny,) - def __init__(self): - super(WeComQRLoginCallbackView, self).__init__() - self.client_type = WeCom - self.client_auth_params = {'corpid': 'WECOM_CORPID', 'corpsecret': 'WECOM_SECRET', 'agentid': 'WECOM_AGENTID'} - self.user_type = 'wecom' - self.auth_backend = 'AUTH_BACKEND_WECOM' - self.create_user_if_not_exist_setting = 'WECOM_CREATE_USER_IF_NOT_EXIST' + client_type_path = 'common.sdk.im.wecom.WeCom' + client_auth_params = {'corpid': 'WECOM_CORPID', 'corpsecret': 'WECOM_SECRET', 'agentid': 'WECOM_AGENTID'} + user_type = 'wecom' + auth_backend = 'AUTH_BACKEND_WECOM' + create_user_if_not_exist_setting = 'WECOM_CREATE_USER_IF_NOT_EXIST' - self.msg_client_err = _('WeCom Error') - self.msg_user_not_bound_err = _('WeCom is not bound') - self.msg_user_need_bound_warning = _('Please login with a password and then bind the WeCom') - self.msg_not_found_user_from_client_err = _('Failed to get user from WeCom') + msg_client_err = _('WeCom Error') + msg_user_not_bound_err = _('WeCom is not bound') + msg_user_need_bound_warning = _('Please login with a password and then bind the WeCom') + msg_not_found_user_from_client_err = _('Failed to get user from WeCom') class WeComOAuthLoginView(WeComOAuthMixin, View):