2016-09-13 32 views
1

我想從一個@ companyname.net電子郵件從電子郵件地址列表只有用戶能夠通過谷歌+ Python的社會身份驗證登錄。我怎麼做到這一點?Django的Python的社會身份驗證只允許某些用戶登錄

SOCIAL_AUTH_GOOGLE_OAUTH2_WHITELISTED_DOMAINS = ['companyname.net'] 

是我目前在settings.py,但只允許@ companyname.net-ERS來登錄。要解決這個壓倒一切的python-社會-auth的管道

+0

這個列表中,您必須是域的依賴?我的意思是,你會允許來自特定域名的所有電子郵件或只是特定的電子郵件地址? –

+0

我想允許所有人**爲@ companyname.net,並且只允許來自其他域的某些特定人。 –

回答

1

的一種方式。

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()是用來作爲一種方法來從文件加載OU中的電子郵件從數據庫:

你可以像覆蓋create_user。它需要返回一個電子郵件列表。

然後,在你的設置中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', 
) 

這樣你就可以保持domais白名單,然後保存您想某處的電子郵件,在那裏你可以加載它們該方法get_list_of_emails()

更多的docs

+0

我沒有測試它,但它應該工作。調試以查看電子郵件是否真的是字段的鍵 –

+0

如果用戶的電子郵件不在allowed_emails中,我將如何將用戶返回到特定頁面? –

+0

只需將settings.py中的SOCIAL_AUTH_LOGIN_ERROR_URL設置爲您希望用戶重定向到的網址 –

相關問題