解决这个问题的一种方法是覆盖 Python-Social-Auth 管道。
您可以使用以下内容覆盖 create_user:
def create_user(strategy, details, user=None, *args, **kwargs):
if user:
return {'is_new': False}
allowed_emails = get_list_of_emails()
fields = dict((name, kwargs.get(name, details.get(name)))
for name in strategy.setting('USER_FIELDS', USER_FIELDS))
if not fields:
return
if fields[email] in allowed_emails:
return {
'is_new': True,
'user': strategy.create_user(**fields)
}
return
这个方法 get_list_of_emails()
的作用是从文件或数据库中加载电子邮件。它需要返回一个电子邮件列表。
然后,在您的设置中的 SOCIAL_AUTH_PIPELINE 中,您需要将 create_user 替换为您自定义的方法:
SOCIAL_AUTH_PIPELINE = (
'social.pipeline.social_auth.social_details',
'social.pipeline.social_auth.social_uid',
'social.pipeline.social_auth.auth_allowed',
'social.pipeline.social_auth.social_user',
'social.pipeline.user.get_username',
'path.to.my.method.create_user',
'social.pipeline.social_auth.associate_user',
'social.pipeline.social_auth.load_extra_data',
'social.pipeline.user.user_details',
)
这样,您可以保持白名单,并将想要的电子邮件存储在可以使用
get_list_of_emails()
方法加载的某个地方。
更多信息请参见
文档。